Export all shared projects as XPOs

We all know that if we’re using Microsoft Dynamics AX 2012, we should hang to models. They have way more features and can be very helpful managing our AX enviroments. But occasionally, we have to use the good ol’ XPO files for various reasons.

I once wanted to export all my shared projects from a virtual machine that I was about to delete, but since they were many, I thought would be good to create some sort of job to help me out.

This was the outcome:

static void EDC_exportAllProjects(Args _args)
{
    ProjectNode sharedProject = Infolog.projectRootNode().AOTfindChild("Shared");
    TreeNodeIterator treeIterator;
    TreeNode childNode;
    str folderPath = @"C:\\AX\\Projects\\";
    str folderName;
    System.DateTime dateTime = DateTimeUtil::newDateTime(systemDateGet(), timeNow());

    void exportProject(TreeNode _projectNode) {
        FileIoPermission perm;
        str exportFileName;

        if (_projectNode != null)
        {
            exportFileName = folderPath + folderName + "\\" + _projectNode.AOTname() + ".xpo";
            perm = new FileIoPermission(exportFileName, "W");
            perm.assert();
            _projectNode.treeNodeExport(exportFileName);
            CodeAccessPermission::revertAssert();
        }
    }

    folderName = dateTime.ToString("yyyyMMdd_HHmmss");

    if (sharedProject)
    {
        treeIterator = sharedProject.AOTiterator();

        if (treeIterator)
        {
            if (!WinAPI::pathExists(folderPath + folderName))
            {
                info(strFmt('Creating the folder %1', folderPath + folderName));
                WinAPI::createDirectoryPath(folderPath + folderName);
            }
            childNode = treeIterator.next();

            while (childNode)
            {
                info(strFmt('Exporting %1', childNode.AOTname()));
                exportProject(childNode);

                childNode = treeIterator.next();
            }
        }
        sharedProject.treeNodeRelease();
    }
}

Surely it’s possible to add some dialogs and progress bars to make it better. But it was enough for the purpose I had.

Hopefully it can be useful for someone!

Number of intervals between two dates using intvNo function

Some developers might have some trouble finding it out how to pick the number of intervals between two dates. That’s when this built-in function come in handy.

Microsoft Dynamics AX has a function called “intvNo”. Plain and simple, it lets you choose an interval scale and gives you the result. Let’s take a look at an example:

static void EDC_intvBetweenMth(Args _args)
{
    date fromDate = str2Date("10/20/2015", 213);
    date toDate = str2Date("12/15/2015", 213);
    
    info(strFmt("%1", intvNo(toDate, fromDate, intvScale::YearMonth)));
}

That will give us the interval counted in months:

You can also choose another scales:

static void EDC_intvBetweenMth(Args _args)
{
    date fromDate = str2Date("10/20/2014", 213);
    date toDate = str2Date("12/15/2015", 213);
    
    // Days
    info(strFmt("Day(s): %1", intvNo(toDate, fromDate, intvScale::YearMonthDay)));
    // Months
    info(strFmt("Month(s): %1", intvNo(toDate, fromDate, intvScale::YearMonth)));
    // Years
    info(strFmt("Year(s): %1", intvNo(toDate, fromDate, intvScale::Year)));
}

intvno2

Please check MSDN for another scales, parameters descriptions and full reference:
https://msdn.microsoft.com/en-us/library/aa886774.aspx