Monday, October 2, 2023

Schedule a Batch Job Through x++ , D365

 You can schedule a batch job from front end as well as through coding 

This Post provides you the code for scheduling Batch Job through X++



class KA_SMSBatchSchedular
{        

    // <summary>
    // Runs the class with the specified arguments.
    // </summary>
    // <param name = "_args">The specified arguments.</param>
    // public static void main(Args _args)
    // {        
    //    BatchHeader batHeader;
    //    BatchInfo batInfo;
    //    RunBaseBatch rbbTask;
    //    str sParmCaption = "My Demonstration (b351)";
    //    ;
    //    rbbTask = new KA_SendSMSBatchJob();
    //    batInfo = rbbTask .batchInfo();
    //    batInfo .parmCaption(sParmCaption);
    //    batInfo .parmGroupId(""); // The "Empty batch group".
    //    batHeader = BatchHeader ::construct();
    //    batHeader .addTask(rbbTask);
    //    batHeader .save();
    //    info(strFmt("'%1' batch has been scheduled.", sParmCaption));
    // }

   // static void batchJobSchedule(Args _args)
    public static void main(Args _args)
    {
        BatchHeader objBatchheader;
        SysRecurrenceData sysRecurrenceData;
        Batch batch;
        BatchJob batchJob;
        KA_SendSMSBatchJob smsBatcjJob;
        BatchInfo objBatchInfo;
        BatchRetries noOfRetriesOnFailure = 0;
        ;

        // Setup the RunBaseBatch Job
        objBatchheader = Batchheader::construct();
        smsBatcjJob = new KA_SendSMSBatchJob();
        objBatchInfo = smsBatcjJob.batchInfo();
        objBatchInfo.parmRetriesOnFailure(noOfRetriesOnFailure);
        objBatchInfo.parmCaption("SMS sending batch job!"); // Description Batch Job
        objBatchInfo.parmGroupId(''); // Batch Gorup
        objBatchInfo.parmBatchExecute(NoYes::Yes);
        objBatchheader.addTask(smsBatcjJob);

        // Set the recurrence data
        sysRecurrenceData = SysRecurrence::defaultRecurrence();
        SysRecurrence::setRecurrenceStartDateTime(sysRecurrenceData, DateTimeUtil::addSeconds(DateTimeUtil::applyTimeZoneOffset(DateTimeUtil::getSystemDateTime() , DateTimeUtil::getUserPreferredTimeZone()), 2)); // Set range of recurrence
        SysRecurrence::setRecurrenceNoEnd(sysRecurrenceData);
        SysRecurrence::setRecurrenceUnit(sysRecurrenceData, SysRecurrenceUnit::Minute); // Set reccurence pattern
        objBatchheader.parmRecurrenceData(sysRecurrenceData);
        // Set the batch alert configurations
     //   objBatchheader.parmAlerts(NoYes::No, NoYes::Yes, NoYes::No, NoYes::Yes, NoYes::Yes);
        objBatchheader.parmAlerts(NoYes::Yes, NoYes::Yes, NoYes::No, NoYes::Yes, NoYes::Yes);
        objBatchheader.save();

        // Update the frequency to run the job to every two minutes
        ttsbegin;
        select forupdate batchJob
            join batch
            where batchJob.RecId == batch.BatchJobId
            && batch.ClassNumber == classnum(KA_SendSMSBatchJob);

        sysRecurrenceData = batchJob.RecurrenceData;
        sysRecurrenceData = conpoke(sysRecurrenceData, 8, [10]);
        batchJob.RecurrenceData = sysRecurrenceData;
        batchJob.update();
        ttscommit;
    }

}


just call this class to schedule your Batch job . you can create a action menuitem and give object of this class in menuItem .

No comments:

Post a Comment

Change SSRS Report Design Based On Condition , D365 ,X++

 To change Sales Invoice control document's design based on condition use following code. Here I have created COC of salesInvoiceControl...