Saturday, August 27, 2022

Running x++ scripts and jobs in an environment with 0 downtime

 

This blog post is about exploring the new Microsoft feature that allows you to run X++ jobs on PROD and non-PROD environments without release and with 0 downtime.

In 10.0.25 you can run simple X++ scripts on a production environment without any downtime. This feature lets you run custom X++ scripts without having to go through Dynamics LCS or suspend your system. Therefore, you can correct minor data inconsistencies without causing any disruptive downtime.


To be able to use this feature 
you need to create a deployable package with ONLY ONE runnable class in it. To do this, 
 you can create a new model and reference the models which object you will use in the X++ script.
 
Dynamics365->Model Management -> Create Model Ex: CutomModel

 Then create the runnable class and generate a deployable package from Visual Studio only with your new model that has only one runnable X++ class.


class SLWrongVendorRunnableClass
{
    
    /// <summary>
    /// Class entry point. The system will call this method when a designated menu 
    /// is selected or when execution starts and this class is set as the startup class.
    /// </summary>
    /// <param name = "_args">The specified arguments.</param>
    public static void main(Args _args)
    {

        VENDINVOICEJOUR Vendjounr;
        PURCHTABLE purchtable;
        if (curExt() != 'XXX')
        {
            throw error("This script must run in the XXX company!");
        }

        ttsbegin;
        select forUpdate Vendjounr
            where   Vendjounr.PURCHID =='PO/000/000XXX' && Vendjounr.InvoiceAccount =='64';
        if (Vendjounr)
        {
            Vendjounr.OrderAccount ='64';
            Vendjounr.doUpdate();
        }

        select forUpdate purchtable
            where  purchtable.PURCHID =='PO/000/000XXX'&& purchtable.InvoiceAccount =='64';
        if (purchtable)
        {
            purchtable.OrderAccount ='64';
            purchtable.doUpdate();
        }
        if (Vendjounr.RowCount() != 1)
        {
            throw error("Not  ORDERACCOUNT updating the expected row in VENDINVOICEJOUR!");
        }
        
        if (PURCHTABLE.RowCount() != 1)
        {
            throw error("Not ORDERACCOUNT updating the expected row in PURCHTABLE !");
        }

        info("Success");
  
        ttscommit;
    
    }

}

After generating the deployable package you can go to System Administration – Periodic tasks – Database – Custom scripts.
Here you can upload your deployable package.

If your deployable package has not consisted of only one runnable job you won’t be able to upload it and a warning message will be displayed.
After successfully uploading the deployable package, another user (different from the one who uploaded it) must Approve the script in the process workflow.

After the approval, you should follow the workflow and Run a test.

If the test log is okay and is without any errors, you need to Accept the test log and then you will be able to Run the script.


After the execution of the script is completed, the log will be updated and you need to end the workflow by confirming that the purpose is resolved, the purpose is unresolved, or abandon.

This is a helpful feature by Microsoft if you need to do some small data corrections or delete dates from certain tables. This way you don’t have to raise a Microsoft ticket to do small data corrections on PROD DB or connect to the ACC or other T2 environments databases.
Also, runnable classes with dialogs can be imported here not just simple data correction scripts.












No comments:

Post a Comment