How to create InventBatchId for an item by Code.

Hi everybody,

I have table VTVSheetingOrderLine that has 2 fields InventBatchID & ItemId.

And table VTVSheetingOrderLine related to InventDim, InventTrans, InventBatch.

The relations will be like:

InventDim.InventDimId = InventTrans.InventDimId

InventTrans.InventTransId = VTVSheetingOrderLine .InventTransId

InventDim.InventBatchId = InventBatch.InventBatchId.

I create form B from table VTVSheetingOrderLine . Add a button “Create Batch Id” on that form.

I wrote codes but it seem not enough, because I don’t know how to create the relations between those table

void clicked()
InventNumGroup inventNumGroup;
Num _num;
int increment;
SalesTable _salesTable;
InventTable _inventTable;
InventNumGroup _inventNumGroup;
NumberSequenceTable _numberSequenceTable;
VTVSheetingOrderLine _vtvSheetingOrderLine;
increment =1;
_inventTable = InventTable::find(vtvSheetingOrderLine.ItemId);
_inventNumGroup = InventNumGroup::find(_inventTable.BatchNumGroupId);
_numberSequenceTable = NumberSequenceTable::find(_inventNumGroup.NumberSequenceId);
if(vtvSheetingOrderLine.ReamId ==’’)
_num = NumberSeq::numInsertFormat((_numberSequenceTable.NextRec + increment - 1),_numberSequenceTable.Format);
select forupdate _numberSequenceTable where _numberSequenceTable.NumberSequence == _inventNumGroup.NumberSequenceId;
if (_numberSequenceTable.InUse == NoYes::No)
_numberSequenceTable.InUse = NoYes::Yes;
_numberSequenceTable.NextRec += increment;
vtvSheetingOrderLine.InventBatchId= _num;
// super();

I don’t know how to filled almost fields of InventTrans, InventDim,InventBatch.

How can I create InventBatchID by Code with the relation structure like above following to the standard way that Microsoft Dynamic AX has done - the way AX create InventBatchID for an Item:

Purchase Order Details → create Purchase Lines → Inventory → Registration → click Auto create → Post all → inventBatchId of an Item is created.

Appreciate & thanks in advance for your help [:)]


InventBatchId in InventDim table gets updated through -

  1. InventUpdate class → writeInventTransAutoDim method and
  2. Classes extended from InventUpdate (example - InventUpd_Arrived, InventUpd_Estimated etc)

Essentially InventUpdate class is the engine which updates inventory transactions. More info is in developers help.

Hope this helps.


Hi Harish Mohanbabu,

Thanks for your help,

But what you suggest is so large to use. What I mean is the way to create InventBatchId for an item & also create each record to insert to InventBatch, InventDim,InventTrans table correlatively & create the relations between them.

Thanks again Mr.Harish, anyone have any suggestion? Appreciate for your all help !

Anyone give me any other suggestion please?

Thanks and regards,

Phuong Tran.

Hi Phuong,

Sorry I misinterpreted your query. If you want to create inventBatchId by code, you can follow any approach from below -

  1. InventDim table → findOrCreate method. Basically here, you create a buffer of InventDim table and call this method. If contents in this buffer doesn’t exist, new record will be created.

  2. InventDim table → find method. This is similar to the above. But here you check with InventDim id. If it doesn’t exist, then you will have to manually call ‘insert’ method in ‘InventDim’ table. For example - see ‘prepareForSave’ method in ‘AxdItem’ class.

I hope this is the info you are looking for.


Hi Phuong,

Just curious, what is the purpose of creating batch number for an item? In other word, what is the requirement?

Just in case they can use the inherent standard functionality? [:D]

Hi Harish Mohanbabu,

Thanks for your help. And what you told me meet my first requirement “create InventBatchId for an item”. And “also create each record to insert to InventBatch, InventDim, InventTrans correlatively & create the relations between them”, still is difficult.

How about the way to create InventTransId? And some fields of those tables I don’t know how to fill.

I want InventTrans, InventDim table created like the standard way of Microsoft Dynamics AX has done.

For ex:

When you open Purchase Order Details form → create PO with Purchase type: Purchase Order or Order Type != Journal → create Purchase Order Lines.

I see at that time, a transaction is created & you can see when you open

→ Inventory → Transactions.

Microsoft AX has created a transaction by creating a record to insert to InventTrans, inventDim & the relations between them.

After that InventBatch will created by register () - the way of register is clear like the way you tell me how to create InventBatchId.

Thanks Mr.Harish

I don’t know how to create a transaction like above although I debuged to seek the way of Microsoft.

Any help, highly appreciate [:)]

Hi Mr Khue,

I would like to do task like what Microsoft has done to take over (inherit) all functions of Microsoft.

Here are my descriptions:

I would like to create an InventBatchId for an Item of my table: VTVSheetingOrderLine & VTVSheetingOrderLine has relations with SalesLines like the relations between SalesLine & SalesTable with some diffrences: the relation between VTVSheetingOrderLine & SalesLines is the relation n-n (many to many):

VTVSheetingOrderLine.InventTransId = SalesLines.InventTransId

One SalesLines record may has a lot of VTVSheetingOrderLine records.

The relation between SalesLines & SalesTable is created with relation 1-1 ( 1 to 1):

SalesTable.InventTransId = SalesLine.InventTransId .

And each records of VTVSheetingOrderLine has the relations with InventDim, InventBatch record, the relation 1-1 ( 1 to 1):

VTVSheetingOrderLine.InventTransId = InventTrans.InventTransId

InventDim.InventDimId = InventTrans.InventDimId

InventBatch.InventBatchId = InventDim.InventBatchId.

I imitate the way of Microsoft when they create PO → create PO Lines → a transaction is automatically created.

And we can see a record is created in InventTrans table, a record is created in InventDim table, the relation between them is created.

After that, when I register that record by choose - Inventory → Registration → Post all

That record will be registered with an InventBatchId is created ( a record is created in InventBatch table & the relations between InventBatch and the rest of InventDim, InventTrans are created ).

I still don’t know how to create a transaction like that.


Best regards,

Yeah AdamRoue,

But I still don’t know how. Anyone help me please.

Thanks all of you!

Hi Phuong,

Thanks for your long description [:)]. Let’s take it step by step and I would like to explain to you standard AX first.

Firstly the InventDim this is a table to store a combination of all “inventory dimensions”. So instead of storing multiple fields BatchId, Site, Location, Warehouse… in transactional tables like salesline or purchaseline or inventtrans, AX use InventDim table to store the combination at one place, and link InventDim record to the transactional tables. Main purpose of this is to save programming effort and database space.

Therefore, whenever you create a new transaction, AX will check whether the “combination” of the dimensions in your transaction has been created before in InventDim table, if not AX will create new record in InventDim table for you and link it to your transaction using InventDimId.

In your scenario, you actually need to create a new record in InventBatch table and use the method InventDim::findOrCreate to create a new record in InventDim table, then use the InventDimId of the new record to link to your transaction table.

This is exactly what you descrived above with the purchase order. When you create a PO line → AX create new inventTrans record, then when you register new batch number, it create InventDim record.

Secondly, to know how AX create record in InventTransTable, take a look into method createLine() of SalesLine table or PurchLine table.

Once you are clear with this, let’s discuss about your customisations [:D]

I am not a developer, but I presume you have looked at the batch number group and how this works to automatically create a batch related to teh inventory item when it is purchased/manufactured?

Hi Mr Khue,

Thanks for your ideal & your help. But with createLine() method, I still can’t search the way AX create a journal in InventTrans table.

When I desrible details what I’m doning for you, I understand the supply chain they did (functional), but I still don’t know the way how they did (technical).

Anyother ideals, please help me. I got stuck this problem for a week.[:(]


HI AdamRoue,

Create Batch number for an item, just only create a batch, but also create a below AX transaction.

I know how to create just a batch number, but still don’t know how to create a below AX transaction.

Thanks for your advice.

Hi Phuong,

Please explain to me why you want to create inventory transaction in your customisation (functionally).

You said you would like to create record in InventTrans table from a customised table? And this table is linked to SalesLine table which also creates InventTrans record?

Hi Mr Khue,

Like I store a lots of rolls of papers in store. When some customers want to buy some papers, and they require me to cut them into some sheets and they will get those sheets as my productions.

When they say that I want to buy roll A, roll B, please cut it into 100 sheets (200x300) with each rolls.

The data will be stored like:

On SO → create SO → create line with data of roll A, roll B (those lines are like SO line, but It’s stored in another table)-> customer click “splitted sheets” button ->new splitted sheets form is created store data of 200 sheets.

Each sheet I want to have an inventBatchId for and create an transaction for each sheet.

That’s its functional & the way I want to store its data.

Thanks for your interest & suggestion before [:)]

Best regards,

Phuong Tran.

You problem is very common with companies have complex unit conversion and inventory unit is actually greater than sales unit.

What is the INVENTORY UNIT of the item? ROLL or SHEET? You case will be impossible if the inventory unit is ROLL.

Sorry, I have a mistake, Inventory unit is SHEET & ROLL. But in this context, we use SHEET.



Phuong Tran.

Ok. That’s better otherwise you should push the problem to the consultant [:P] … As long as the inventory unit on the item master is the “smallest unit”, it’s fine.

Then I guess, when user enters sales order line, the Unit is ROLL, system will automatically generate inventory transaction with SHEET as the unit. Following the example, let say you have a complex scenario like this:

  • Item XXX, On hand: 2,500 SHEET: this is conposed of 2 Rolls A and B (1,000 sheets each) and 500 Sheets (already cut).

  • Sales order line: user wants to sell 1.8 ROLL. Standard AX system will create an inventory transaction in InventTrans table with 1,800 sheets.

AX can’t magically assign a batch number for each roll to be “broken down”, you need your code to create an inventory journal (probably P&L journal) to decrease and increase your SHEETS with batch number. And let AX batch number group to take care of the batch number generation. All you need to do is, after generating the Batch number, write your code to do reservation from sales line to the available batches which have been generated by the P&L journal. I can guide you to the code after this.

Please confirm this is what you need.

Note: this very depends to the consultant who consulted the customer to use which Unit. Some customer only want to store the ROL**L because they only see the “ROLL” in their stock. I think your issue is more with the consultant and needs to be revisited by the consultant rather than just simple customisation. The simplest way to resolve this is to use SHEET as a different item number. Than you can setup a simple production order or BOM journal to convert ROLL into SHEET. But again, this is very essential from the consultant point of view. BTW, you can drop me an email if you want to take this offline.

Hi Phuong,

I am no expert, but may be the info will be helpful to you.

To create the inventTrans records, check the methods insert()(for ex. SalesLineType, which is responsible for actions on salesLine).

Ax use the InventUpd related classes to create, update the inventTransactions.

U can check the methods like , updateNow() of InventUpd related classes.

Check the class InventMovement also, how it works.

Please take some technical help to better understand the classes.

But, to be frank, I still have doubt, how u r going to manage inventTrans in salesline and ur customised table.

May be it helps u…

That’s why I kept dragging on the requirement instead of coding [:D][;)]

Trying to generate records and and control the InventTrans table from customised tables which are derrived from salesLine is definitely not an option.

Basically, you have inventory transactions generated from sales lines. These transactions will be controlling sales order line status from an “inventory poitn of view”. Thus there is no point to generate inventory transactions from another customised table.

And actually, the problem here is the unit conversion, where the inventory unit is greater than the sales unit [:)]