Tuesday, September 26, 2023

How to add destination Tab in sys operation batch job in D365FO

I have a requirement to generate multiple reports within a batch job. The user would like the option to specify the destination tab in the system operation dialog. Based on their inputs, the system will generate the selected report.

I have followed the standard class "AssetTransferMassController".

In the controller class, I have written the below code. 

class MyController extends SysOperationServiceController implements BatchRetryable
{
    #AssetTransfer

    public static void main(Args args)
    {
        MyController      controller =  new MyController();

        controller.parmExecutionMode(SysOperationExecutionMode::Synchronous);
        controller.startOperation();

        if (!controller.isDialogCancelled())
        {
            MyContract contract = controller.getDataContractObject('_contract') as MyContract;
            
            // This code I have written If user want to open report in sreen or file.
            if (controller.parmExecutionMode() != SysOperationExecutionMode::ScheduledBatch)
            {
                sRSPrintDestinationSettings sRSPrintDestinationSettings = 
                                                controller.getDataContractObject('SRSPrintDestinationSettings') 
                                                                                as SRSPrintDestinationSettings;
            
                MyService::loopRecords(contract, sRSPrintDestinationSettings);
            }
        }
    }  

    public void new()
    {
        super(classStr(MyService), methodStr(MyService, process));

        this.parmDialogCaption("Test multiple reports");
    }

    protected void dialogPostRun()
    {
        super();

        this.updateDesign();
    }

    public boolean isDialogCancelled()
    {
        return dialogCanceled;
    }

    protected boolean canRunInNewSession()
    {
        return true;
    }

    public boolean showPrintSettings()
    {
        return true;
    }
    
    // with this method we are enable the group.
    public void updateDesign()
    {
        FormRun                     formRun;
        FormBuildGroupControl       groupBuildControl;
        FormGroupControl            groupControl;
        SysOperationDialog          sysOperationDialog;

        sysOperationDialog  = dialog as SysOperationDialog;
        formRun             = sysOperationDialog.formRun();

        groupBuildControl   = formRun.form().design().control(#CurrentPrintDestinationControlName);
        groupControl        = formRun.design().control(groupBuildControl.id());
        groupControl.visible(true);
    }

    protected boolean validate()
    {
        boolean                     isValid;

        isValid = super() && this.validateReportPrintSetting();

        return isValid;
    }

    private boolean validateReportPrintSetting()
    {
        boolean                     isValid = true;
        SRSPrintDestinationSettings printSettings;

        if (this.batchInfo() && this.batchInfo().parmBatchExecute())
        {
            printSettings = this.getDataContractObject('SRSPrintDestinationSettings') as SRSPrintDestinationSettings;

            if (printSettings && printSettings.printMediumType() == SRSPrintMediumType::Screen)
            {
                isValid = checkFailed("@SYS329665");
            }
        }

        return isValid;
    }

    [Hookable(false)]
    final boolean isRetryable()
    {
        return true;
    }

}


In the service class's process method, I have added an input parameter SRSPrintDestinationSettings. When generating the report, I am assigning the print destination settings.

class MyService extends SysOperationServiceBase
{    
    public void process(MyContract _contract, SRSPrintDestinationSettings sRSPrintDestinationSettings)
    {
        if (this.isExecutingInBatch())
        {
            MyService::loopRecords(_contract, sRSPrintDestinationSettings);
        }
    }

    public static void loopRecords(MyContract _contract, SRSPrintDestinationSettings sRSPrintDestinationSettings)
    {
        System.Exception        ex;
        QueryRun                queryRun;

        queryRun = new queryRun(_contract.getQuery());

        while(queryRun.next())
        {
            try
            {
                // generate code
                Common  myTable = queryRun.get(tableNum(Common));
                
                MyService::generateReport(myTable, sRSPrintDestinationSettings);
            }
            catch
            {
                ex = CLRInterop::getLastException().GetBaseException();

                continue;
            }
        }
    }

    public static void generateReport(Common _table, SRSPrintDestinationSettings _sRSPrintDestinationSettings)
    {
        SRSPrintDestinationSettings settings;
        Filename                    fileName    = strFmt("TestReport");
        SrsReportRunController      controller  = new SrsReportRunController();
        MyReportContract            contract    = new MyReportContract();

        controller.parmArgs(new Args());
        controller.parmReportName('report name');
        controller.parmShowDialog(false);
        controller.parmLoadFromSysLastValue(false);
        controller.parmReportContract().parmRdpContract(contract);

        //controller.parmReportContract().parmPrintSettings(_sRSPrintDestinationSettings);
        // --- or----
        settings = controller.parmReportContract().parmPrintSettings();
        settings.printMediumType(_sRSPrintDestinationSettings.printMediumType());
        settings.fileName(fileName);
        settings.fileFormat(_sRSPrintDestinationSettings.fileFormat());
        settings.overwriteFile(true);

        controller.startOperation();
    }
}


Keep daxing!!





No comments:

Post a Comment