web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

Running Batch Job from X++ with parameters

(0) ShareShare
ReportReport
Posted on by 1,271

Hello,

This is an example of running batch job from X

static void TID_ExecuteBatchJobDistribution(Args _args)
{
    BatchHeader header;
    SysRecurrenceData sysRecurrenceData;
    Batch batch;
    BatchJob batchJob;
    RetailConnScheduleRunner _RetailConnScheduleRunner; // Class extends RunBaseBatch
    BatchInfo processBatchInfo;
    BatchRetries noOfRetriesOnFailure = 4;
    ;

    // Setup the RunBaseBatch Job
    header = BatchHeader::construct();
    _RetailConnScheduleRunner = new RetailConnScheduleRunner();
    processBatchInfo = _RetailConnScheduleRunner.batchInfo();
    processBatchInfo.parmRetriesOnFailure(noOfRetriesOnFailure);
    processBatchInfo.parmCaption(_RetailConnSchedule.Name); // Description Batch Job
    processBatchInfo.parmGroupId('RTL'); // Batch Gorup
    processBatchInfo.parmBatchExecute(NoYes::Yes);
    header.addTask(_RetailConnScheduleRunner);

    // Set the recurrence data
    sysRecurrenceData = SysRecurrence::defaultRecurrence();
    SysRecurrence::setRecurrenceStartDateTime(sysRecurrenceData, DateTimeUtil::addSeconds(DateTimeUtil::utcNow(), 20)); // Set range of recurrence
    SysRecurrence::setRecurrenceNoEnd(sysRecurrenceData);
    SysRecurrence::setRecurrenceUnit(sysRecurrenceData, SysRecurrenceUnit::Minute); // Set reccurence pattern
    header.parmRecurrenceData(sysRecurrenceData);
    // Set the batch alert configurations
    header.parmAlerts(NoYes::No, NoYes::Yes, NoYes::No, NoYes::Yes, NoYes::Yes);
    header.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(RetailConnScheduleRunner);

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

I woul dlike to pass some parameters to the class  _RetailConnScheduleRunner so that they are visible in run() method. Should be done through parm() methods? something like that

header = BatchHeader::construct();
    _RetailConnScheduleRunner = new RetailConnScheduleRunner();
    _RetailConnScheduleRunner.parmFirstParameter(_parameter1);
    _RetailConnScheduleRunner.parmSecondParameter(_parameter2);
    processBatchInfo = _RetailConnScheduleRunner.batchInfo();
    processBatchInfo.parmRetriesOnFailure(noOfRetriesOnFailure);
    processBatchInfo.parmCaption(_RetailConnSchedule.Name); // Description Batch Job
    processBatchInfo.parmGroupId('RTL'); // Batch Gorup
    processBatchInfo.parmBatchExecute(NoYes::Yes);
    header.addTask(_RetailConnScheduleRunner);

Or I should stick to another approach?

Thanks.

I have the same question (0)
  • Gunjan Bhattachayya Profile Picture
    35,429 on at

    Hi Azat,

    Is there any particular reason you are adding this batch job record from a runnable class?

  • dark_knight Profile Picture
    1,271 on at

    yes. there is. usually the parameters are addedd through dialog in runbasebatch framework but how should I add parameters in that case? when I don't have a dialog and runbasebatch should be scheduled in non interactive manner?

  • Gunjan Bhattachayya Profile Picture
    35,429 on at

    Hi Azat,

    In case you need to add such a class to a batch job, you can create a new record in Batch jobs form (Navigation : System administration > Inquiries). You can add the class under Batch tasks and save the record, If you click on the Parameters button, you can set the parameters as per the class you choose.

    pastedimage1648555045329v2.png 

    This will be easier to configure in any Tier-2 / Prod environment as you could do this from the UI only, rather then using a runnable class.

    If you still want to use a runnable class for testing, using parm methods should be the right way. Have you tried to see if that works for you?

  • dark_knight Profile Picture
    1,271 on at

    But parameters should be packed. if i pass them as parmMethods will it work? Will they be packed? I can't check it right now. But I will search for the opportunity. So when I use a dialog parameters from dialog are got in getFromDialogMethod() but in that case what should be the approach in order to pack the parameters? As I understand main() method won't be run. It is run only if I launch the class manually, right? Only the run() method will be run, right?

  • Gunjan Bhattachayya Profile Picture
    35,429 on at

    I have never tried to schedule a batch job using X++ and so I am not sure if the parm methods will be packed. I can try that and confirm later. This post has sample code to schedule a batch job along with parameters. You can give this a try.

    As for the setting the parameters from the batch tasks in the form I mentioned, the parameters will be packed, since dialog and getFromDialog methods will be executed. The main method not being run in this scenario does not have effect on the values being packed.

  • dark_knight Profile Picture
    1,271 on at

    objBatchheader = Batchheader::construct();
            objRetailSchedule = new RetailCDXScheduleRunner();
            objBatchInfo = objRetailSchedule.batchInfo();
            objBatchInfo.parmRetriesOnFailure(noOfRetriesOnFailure);
            objBatchInfo.parmCaption("Description should be here"); // Description Batch Job
            objBatchInfo.parmGroupId('YourBatchGroup'); // Batch Gorup
            objBatchInfo.parmBatchExecute(NoYes::Yes);
            objBatchheader.addTask(objRetailSchedule);

    here class RetailCDXScheduleRunner() is actually is added as a task of the newly created batch job. This class RetailCDXScheduleRunner () I'd like to pass parameters to. In the code from the link it's also isn't done. The parameters are for the different class. objBatchINfo()

  • Verified answer
    Gunjan Bhattachayya Profile Picture
    35,429 on at

    You're right. I thought they were passing parameters using the parm methods. Let me give it a try later so that I can confirm here.

    In the meantime, you could try setting up the batch job from the Batch jobs form and set the parameters as per my screenshot from earlier and make sure it works as expected.

  • dark_knight Profile Picture
    1,271 on at

    Thanks. But setting up the batch job from the Batch jobs form is pointless for me, isn't it? My requirement is to schedule batchjob many times with different dynamic parameters. If I go to the form you mentioned that will be only one schedule iteration with only predetermind bunch of parameters, isn't it?

  • dark_knight Profile Picture
    1,271 on at

    I have resolved my problem. Please take a look at the code below

     BatchHeader header;
                    SysRecurrenceData sysRecurrenceData;
                    Batch batch;
                    BatchJob batchJob;
                    BatchInfo processBatchInfo;
                    BatchRetries noOfRetriesOnFailure = 1;
                    ;
    
    
                    str sParmCaption = strFmt("Quotation %1 Lost", salesQuotationTable.QuotationId);
    
                    header = BatchHeader::construct();
                    SalesQuotationRejectTask SalesQuotationRejectTask = HDCSalesQuotationRejectTask::construct();
                    SalesQuotationRejectTask.parmSalesQuotationTableRecId(salesQuotationTable.RecId);
                    SalesQuotationRejectTask.pack();
    
                    processBatchInfo = HDCSalesQuotationRejectTask.batchInfo();
                    processBatchInfo.parmRetriesOnFailure(noOfRetriesOnFailure);
                    processBatchInfo.parmCaption(sParmCaption); // Description Batch Job
                    processBatchInfo.parmGroupId(''); // Batch Gorup
                    processBatchInfo.parmBatchExecute(NoYes::Yes);
                    header.addTask(SalesQuotationRejectTask);
                    header.save();

    this worked fine for me. we need to use pack() method explicitly. Thank you for all your help.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the June Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 445 Super User 2026 Season 1

#2
Subra Profile Picture

Subra 407

#3
Martin Dráb Profile Picture

Martin Dráb 274 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans