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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

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

I set batch class that extends from runbasebatch but when I used this.isinbatch always return false and I don't know why ??

(0) ShareShare
ReportReport
Posted on by 10,280

Please advise as I need to set run ()method to be run only inside the batch but it isn't working and the method this.isinbatch return false always and I don't know why.

I have the same question (0)
  • Martin Dráb Profile Picture
    239,069 Most Valuable Professional on at

    That the class can run in batch doesn't mean it's actually executed in batch. How are you running it?

    By the way, is this thread about AX 2012 or D365FO?

    And why don't you use SysOperation framework instead of RunBase?

  • Codehunter Profile Picture
    10,280 on at

    It is AX 2012 r2 and I created new class that extends from runbasbatch but when I use this.isbatch() in run method , always return false besides I run the class as server not client.

  • Codehunter Profile Picture
    10,280 on at

    I need to get this.isinbatch() true but it keeps returning false in run method() so please anyone stuck with such a case, please advise

  • Martin Dráb Profile Picture
    239,069 Most Valuable Professional on at

    That the class can run in batch doesn't mean it's actually executed in batch. How are you running it?

    By the way, I changed the version tag from Microsoft Dynamics AX (current version) to Microsoft Dynamics AX 2012. Please use it next time for questions about AX 2012.

  • Codehunter Profile Picture
    10,280 on at

    here is the code

    Class batch extends runbasebatch

    {

     public  void run()

       {

      try

       {

           ttsbegin;

    if(this.isinbatch) ///always return false ************************

    /// whatever the implementation is

           ttsCommit;

           }

            catch(Exception::Error)

       {

           ttsabort;

       }

       catch(Exception::Deadlock)

       {

           retry;

       }

       }

    public void BatchStarted()

    {

        BatchJob batchJob;

        BatchRetries noOfRetriesOnFailure = 2;

           BatchHeader header;

         Batch batch;

        batch _FixedAssetsBatch; // Class extends RunBaseBatch

       BatchInfo processBatchInfo;

       SysRecurrenceData sysRecurrenceData;

         header = BatchHeader::construct();

       select forupdate batch

           join batchJob

           where batchJob.RecId == batch.BatchJobId

                   && batch.ClassNumber == classnum(FixedAssetsBatch)

                   && batchJob.Status == BatchStatus::Waiting

                   && batch.Company == curext();

       if(!batch)

       {

       _FixedAssetsBatch = new FixedAssetsBatch();

       processBatchInfo = _FixedAssetsBatch.batchInfo();

       processBatchInfo.parmRetriesOnFailure(noOfRetriesOnFailure);

       processBatchInfo.parmCaption(_FixedAssetsBatch.name()); // Description Batch Job

       processBatchInfo.parmGroupId('FABatch'); // Batch Gorup

       processBatchInfo.parmBatchExecute(NoYes::Yes);

       header.addTask(_FixedAssetsBatch);

       // Set the recurrence data

       sysRecurrenceData = SysRecurrence::defaultRecurrence();

      sysRecurrenceData= SysRecurrence::setRecurrenceStartDate(sysRecurrenceData,today());

     SysRecurrence::setRecurrenceStartDateTime(sysRecurrenceData,DateTimeUtil::addSeconds(DateTimeUtil::utcNow(),30)); // Set range of recurrence

       sysRecurrenceData=SysRecurrence::setRecurrenceEndAfter(sysRecurrenceData,1);

     //  SysRecurrence::setRecurrenceUnit(sysRecurrenceData, SysRecurrenceUnit::); // 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();

       }

        ttsbegin;

       select forupdate batchJob

       join batch

       where batchJob.RecId == batch.BatchJobId

       && batch.ClassNumber == classnum(FixedAssetsBatch);

       sysRecurrenceData = batchJob.RecurrenceData;

       sysRecurrenceData = conpoke(sysRecurrenceData, 8, [1]);

       batchJob.RecurrenceData = sysRecurrenceData;

       batchJob.update();

           ttscommit;

    }

     public static void main(Args _args)

       {

           batch_FixedAssetsBatch = Batch::construct();

           _FixedAssetsBatch.BatchStarted();

           _FixedAssetsBatch.init();

          // _FixedAssetsBatch.parmBatch(_args);

           _FixedAssetsBatch.run();

       }

    }

  • Martin Dráb Profile Picture
    239,069 Most Valuable Professional on at

    Please always use Insert > Insert Code (in the rich-formatting view) to paste source code. It'll make it much easier to read.

    Your code is very strange. You should never do anything like updating BatchJob table directly.

    First of all, think about if you don't want to use SysOperation framework rather than RunBaseBatch. You ignored my question on this topic...

    If you want to use RunBaseBatch, the first step is implementing the class correctly. Your current code won't even compile, because you forgot to implement pack() and unpack() and the name (Batch) is already taken. (You wouldn't have to implement pack()/unpack() if you used SysOperation.)

    Then implement main() like this:

    public static void main(Args _args)
    {
    	MyClass myClass = new MyClass();
    	if (myClass.prompt())
    	{
    		myClass.run();
    	}
    }

    Compile your code and generate CIL.

    Then run your class, choose batch processing in the dialog and confirm it.

    By the way, what business requirement are you to fulfill by checking if a piece code is running in batch?

  • Codehunter Profile Picture
    10,280 on at

    I set all the pack and unpack methods in the batch but I didn't mention it in the previous post but my problem is that why this.isinbatch() returns false .

    Please advise.

  • Suggested answer
    Alfasith AX Profile Picture
    120 User Group Leader on at

    Hi,

    Correct me if I'm wrong.

    If the code runs in batch that means it runs in server if its not in batch then its running in client.

    So you can validate using below statement and can proceed.

    if (isRunningOnServer())

    {

    XXXXX

    }

    else

    {

    YYYYY

    }

  • Suggested answer
    nmaenpaa Profile Picture
    101,166 Moderator on at

    Hi Codehunter,

    as Martin already mentioned, your code looks very strange and it's almost certainly not the correct solution for your business requirement. I ask you to go back to the drawing board, most likely it will save your time, and also save time and money in the future when this code is maintained.

  • Codehunter Profile Picture
    10,280 on at

    I want to use this.isinbatch() and it always return false besides, I set the class to run on server .

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

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

#1
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 658

#2
André Arnaud de Calavon Profile Picture

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

#3
Syed Haris Shah Profile Picture

Syed Haris Shah 315 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans