AX – D365FO – Change Parent Project of a given project

If you want to change a parent project to a given project follow this code

It will create a RunBase class with two fields :

  • Child Project
  • Parent Project

In Child project you will put the project id you want to change the Parent.

In Parent project you will put the project id that will be the new Parent of the child Project.

The 2 fields are mandatory, so if they will be left blank a warning messagge will appear in the browser

When you click on OK the update will start.

If the new parent project is not closely related to the child project the update cannot be done and thi message will appear

Thi is the Runbase class code

class Al0ChangeParentProject extends RunBase
{
    PayrollPayStatement payrollPayStatement;
    ProjId projId;
    ProjParentId projParentId;
    DialogField dialogprojId;
    DialogField dialogprojParentId;

    public container pack()
    {
        return conNull();
    }

    public boolean unpack(container packedClass)
    {
        return true;
    }

    public Object dialog()
    {
        Dialog dialog = super();

        dialog.caption("Change Parent Project");
        dialogprojId = dialog.addField(extendedTypeStr(projId));
        dialogprojParentId = dialog.addField(extendedTypeStr(ProjParentId));
        return dialog;
    }

    public boolean getFromDialog()
    {
        projId = dialogprojId.value();
        projParentId = dialogprojParentId.value();
        return super();
    }

    public boolean validate(Object calledFrom = null)
    {
        boolean ret;

        ret = super(calledFrom);

        if(projId == "")
        {
            ret = checkFailed("Project is mandatory");
        }

        if(projParentId == "")
        {
            ret = checkFailed("Parent Project is mandatory");
        }

        return ret;
    }

    public static List getChildProjectsFromRootProject(ProjId _projId, List _projList)
    {
        ProjTable   projTable;
        _projList.addEnd(projTable::find(_projId));

        while select projTable
              where projTable.ParentId == _projId
        {
            _projList.addEnd(projTable);

            if (ProjTable::child(projTable.ProjId))
            {
                ProjTable::getChildProjectsFromRootProject(projTable.ProjId, _projList);
            }
        }

        return _projList;
    }

    public void run()
    {

     
        container con = projTable::ancestors(projId);

        ProjId rootProj = projTable::getRootProjId(projId);
        List _projList = new List(Types::Record);

        List list = Al0ChangeParentProject::getChildProjectsFromRootProject(rootProj, _projList);

        Enumerator en;
        en = list.getEnumerator();
        boolean found = false;

        while (en.moveNext())
        {
            if(en.current() is ProjTable)
            {
                ProjTable projTable = en.current() as ProjTable;

                if (projParentId == projTable.ProjId)
                {
                    found = true;
                }
            } 
        }
        
        if(found)
        {
            ttsbegin;
            Projtable projTable = ProjTable::find(projId, true);
            projTable.parentId = projParentId;
            projTable.update();
            ttscommit;
            
            Info(projParentId + " is the new Parent Project of " + projId);
        }
        else
        {
            Info("Parent Project " + projParentId + " cannot be assigned to the Project " + projId + " because they are not linked");
        }




    }

    public static void main(Args _args)
    {
        Al0ChangeParentProject al0ChangeParentProject = new Al0ChangeParentProject();

        if (al0ChangeParentProject.prompt())
        {
            al0ChangeParentProject.run();
        }
    }

}

Rispondi

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo di WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione /  Modifica )

Google photo

Stai commentando usando il tuo account Google. Chiudi sessione /  Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione /  Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione /  Modifica )

Connessione a %s...