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 :
Microsoft Dynamics AX (Archived)

Unable to Access Batch Class variable in Run method

(0) ShareShare
ReportReport
Posted on by 2,334

Hi,

I'm trying to access a class variable in run() method of a Class which extends RunBaseBatch. I'm trying to create a BatchJob with multiple tasks and each task must have a unique value in a class variable to be separately identifiable in further code.

Here's my Class:

class ParallelBatchTest extends RunBaseBatch
{
    #define.CurrentVersion(1)
    int batchNo;
}

public void new()
{
        super();
}

public container pack()
{
    return conNull();

}

private void parmBatchNo(int _batchNo)
{
        BatchNo = _batchNo;
}

void run()
{
    batchtest batchtest;

    batchtest.BatchNo=BatchNo; //BatchNo is not accessible at runtime.
    batchtest.insert();

}

public boolean unpack(container packedClass)
{
    return true;
}

public static ParallelBatchTest construct(int _batchNo)
{
    ParallelBatchTest  c;
    c = new ParallelBatchTest();
    c.parmBatchNo(_batchNo);
    return c;
}

Job to schedule BatchJob with multiple tasks:

static void batchTest(Args _args)
{
    BatchHeader                     batchHeader;
    ParallelBatchTest               parallelBatchTest;
    BatchInfo                       batchInfo;
    int                             i,taskCount=5;


    batchHeader = BatchHeader::construct();
    batchHeader.parmCaption(strFmt('Batch JOB 1'));

    for (i=1; i <= taskCount; i++)
    {
        parallelBatchTest = ParallelBatchTest::construct(i);
        batchInfo = parallelBatchTest.batchInfo();
        BatchInfo.parmCaption('BatchTest'+int2str(i));
        batchHeader.addTask(parallelBatchTest);
    }
    batchHeader.save();

    info('Done');
}

Result after Batch Job completes:

5353.parallel_5F00_batch.png

Issue:

The BatchNo columns should have been 1,2,3,4,5 instead of 0,0,0,0,0. Which means the class variable BatchNo was not accessible in the run() method.

I have run the Incremental CIL. So the class is fully compiled to CIL.

How can I correct this?

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Mea_ Profile Picture
    60,286 on at

    Hi  MYGz,

    That's because you completely ignored pack\unpack. Instead of returning connull in pack you need to return [batchNo] and then use it in unpack, look at any class derived from RunBaseBatch to see how pack\unpack should be implemented.

  • MYGz Profile Picture
    2,334 on at

    Aha! TIL actual usage of pack/unpack. Thank You Ievgen!

    I had earlier read pack/unpack when I just started AX, but I didn't implement it so didn't knew the actual usage.

    And lately I have been using some BatchJobs that didn't require pack, unpack implementation and they were running fine, so didn't get to learn.

    Now after implementing pack/unpack my batch job is running perfect.

    I read this to understand the pack/unpack concept:

    community.dynamics.com/.../67248

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 > 🔒一 Microsoft Dynamics AX (Archived)

#1
CP04-islander Profile Picture

CP04-islander 16

#2
GiacomoRovai Profile Picture

GiacomoRovai 4

#3
Douglas Noel Profile Picture

Douglas Noel 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans