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();
        }
    }

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s