Copying Fixed Assets in Dynamics AX 2012 using X++ code


In this post, we will take a look how we can copy fixed assets in Microsoft Dynamics AX 2012 programmatically using X++ code.

You need a job to execute the code.

In the AOT (Application Object Server), right-click on the Jobs node and click New Job.

Write the following code:

/// <summary>
///     This AOT Job copies a specified fixed asset  a certain number of times within the specified company.
///         Param "numberOfCopies" tells how many times the fixed asset will be copied.
///         Param "assetId" tells what fixed asset is to be copied.
/// </summary>
static void copyAsset()
{
    int counter;
    int numberOfCopies = 10; // Specify how many copies do you want

    AssetTable              assetTable;
    AssetCopy               assetCopy;
    AssetId                 assetId = 'BUIL-001'; // Specify the Fixed Asset Id
    SysOperationProgress    progress = new SysOperationProgress();

    #AviFiles

    assetTable = AssetTable::find(assetId);

    if(assetTable)
    {
        counter = numberOfCopies;

        try
        {
            progress.hide();
            progress.setCaption(strFmt('Copying Fixed Asset "%1"', assetId));
            progress.setAnimation(#AviUpdate);
            progress.setTotal(numberOfCopies);

            while(counter>0)
            {
                assetCopy = new AssetCopy(assetTable);
                assetCopy.run();

                counter--;
                progress.setText(strfmt('%1 copies of fixed asset "%2" has been created.', numberOfCopies-counter, assetId));
                progress.setCount(numberOfCopies-counter, 1);
            }
        }
        catch(Exception::Error)
        {
            error('Operation failed to completion.');
        }

        if((numberOfCopies-counter)>0)
        {
            info(strFmt('Fixed Asset "%1" has been copied "%2" times sucessfully.', assetId, numberOfCopies-counter));
        }
        else
        {
            error(strFmt('No copies wrere created for fixed asset "%1".', assetId));
        }
    }
    else
    {
        error(strFmt('Fixed Asset "%1" cannot be found.', assetId));
    }
}

Run the job. Assets Copied.

Advertisements

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 )

Google+ photo

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

Connecting to %s