Friday 29 July 2016

Using/Extending SysOperationFramework instead of RunBaseFramework in AX 2012

Below are the Classes created to Demonstrate
 

1.  ESS_DemoSysOperationController Class which extends SysOperationServiceController – (For Batch Dialog)
2.  ESS_DemoSysOperationService Class which extends SysOperationServiceBase – (For multitask Batch Initiation)
3.  ESS_DemoOperationDataContract Class – (For parameter values)
4.  ESS_DemoSysOperationTaskService Class – (For actual logic to execute in Batch)

1.   Create a class extends SysOperationServiceController
    class ESS_DemoSysOperationController extends SysOperationServiceController
    {

}

    a.   Override new method
       protected void new()
       {
            super(classStr(ESS_DemoSysOperationService), methodStr(ESS_DemoSysOperationService, update),   SysOperationExecutionMode::Synchronous);
       }

b.   Create  Construct method
        server static ESS_DemoSysOperationController construct()
        {

    ESS_DemoSysOperationController      controller; 

    controller = new ESS_DemoSysOperationController ();

    controller.parmShowDialog(true); // Actually the default value

    controller.parmShowProgressForm(false);

    return controller;
        }

c.   Create main method

static void main(Args args)
        {

    ESS_DemoSysOperationController operation;
            operation = new ESS_DemoSysOperationController ();
            operation.startOperation();
        }

2.   Create a class extends SysOperationServiceBase

class ESS_DemoSysOperationService extends SysOperationServiceBase
    {

    BatchHeader                         batchHeader;
        SysOperationServiceController       controller;
        ESS_DemoSysOperationDataContract    contract;
    }

 

a.   Create update method

[SysEntryPointAttribute]
    public void update()
    {

    RefRecId minRecId, maxRecId, fromRecId, toRecId;
        minRecId =  5637775244;
        maxRecId =  5648390004;
        fromRecId = minRecId;

    if(this.isExecutingInBatch())
        {
            if(!batchHeader)
            {
                 batchHeader = BatchHeader::getCurrentBatchHeader();
            }

        while (toRecId < maxRecId)
            {
                 toRecId = fromRecId + 100000;

            // Create a service controller to run the task
            controller = new SysOperationServiceController(classStr(ESS_DemoSysOperationTaskService), methodStr(ESS_DemoSysOperationTaskService, insertRecords),SysOperationExecutionMode::Synchronous);

        contract = controller.getDataContractObject();
            contract.parmValues(["value01", "value02",fromRecId,toRecId]);
            batchHeader.addRuntimeTask(controller, this.getCurrentBatchTask().RecId); // Create a runTimeTask within the current batch job
           fromRecId = toRecId;
        }
    }
    else
    {
        // Create a service controller to run the task
        controller = new SysOperationServiceController(classStr(ESS_DemoSysOperationTaskService), methodStr(ESS_DemoSysOperationTaskService, insertRecords),SysOperationExecutionMode::Synchronous);

        contract = controller.getDataContractObject();
        contract.parmValues(["value01", "value02",minRecId,maxRecId]);
        controller.run();
  
    }
    // If we're processing in batch, then save the batch header
    if(batchHeader)
    {
        batchHeader.save();
    }
}

3.   Create a Class ESS_DemoSysOperationDataContract

[DataContractAttribute]
    public class ESS_DemoSysOperationDataContract
    {
         container   conValues;
    }

a.   Create parmValues method

[DataMemberAttribute]
        public container parmValues(container _values = conValues)
        {
             conValues = _values;
         
             return conValues;
        }

 
4.   Create a Class ESS_DemoSysOperationTaskService
 
     public class ESS_DemoSysOperationTaskService
     {
         ESS_SysOperationDemoTable       ess_SysOperationDemoTable;
     }

a.   Create insertRecords method

[SysEntryPointAttribute]
        public insertRecords(ESS_DemoSysOperationDataContract _contract)
        {
            str                         value01, value02;
            RefRecId            fromRecId, toRecId;    
            RecordInsertList     recordInsertList = new RecordInsertList(tableNum(ESS_SysOperationDemoTable));

    [value01, value02, fromRecId, toRecId] = _contract.parmValues();

    While (condition)
            {
                 ess_SysOperationDemoTable.clear();
                 ess_SysOperationDemoTable.Field01 = 1
                 ess_SysOperationDemoTable.Field02 = 2;
                 recordInsertList.add(ess_SysOperationDemoTable);
            }
       
            recordInsertList.insertDatabase();
        }

Older SQL Backup files deletion through batch


To keep backup files up to 30 days and delete rest of the files in Windows Batch:

Create a batch file using the below command:

forfiles /M *.bak /P "Backup Folder" /S /D -NoOfDays /C "cmd /c del /F /Q @path"

forfiles /M *.bak /P "E:\SQL Backup" /S /D -30 /C "cmd /c del /F /Q @path"

Copy paste the below command in notepad and change your folder path and Days then Save as .bak file.

Refer below link for further:


Use the Windows Task Scheduler to call the batch script:
https://technet.microsoft.com/en-us/library/cc748993(v=ws.11).aspx