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, ...
Unanswered

How to update the InventDimId in PurchTable form on the update of InventDim batch id field?

(3) ShareShare
ReportReport
Posted on by 201
Hi ,
I am trying to update the batch number of a item id while adding the itemid on purchase line.  i checked that the batch number is getting updated on invent dim but the value is not being seen . Also if i check on sql, the value is not updated. Kindly help - Here is the code on purchase the modified field of purchase  price of form. 
[ExtensionOf(formDataFieldStr(PurchTable, PurchLine, PurchPrice))]
final class PurchTable_PurchLinePurchPrice_Extension
{
    public void modified()
    {
        FormDataObject formDataObject = any2Object(this) as FormDataObject;
        FormDataSource formDataSource = formDataObject.datasource();
        PurchLine purchLine;

        next modified();

        purchLine = formDataSource.cursor();
        if (purchLine)
        {
            // Log current inventDimId
            info(strFmt("Before update: InventDimId = %1", purchLine.InventDimId));

            // Call the PurchLine method that updates batch number (should update purchLine.InventDimId)
            purchLine.updateBatchNumber();

            // Ensure the PurchLine buffer's inventDim values are applied
            purchLine.modifyInventDim(purchLine.inventDim(), fieldNum(InventDim, InventBatchId), false);

            // Option B: explicitly set the InventDim datasource on the form to the updated invent dim record
            InventDim invDimRec = InventDim::find(purchLine.InventDimId);
            FormRun formRun = formDataSource.formRun();
            FormDataSource inventDimDs;

            if (formRun && invDimRec)
            {
                // Get the InventDim datasource from the PurchTable form
                inventDimDs = formRun.dataSource(formDataSourceStr(PurchTable, InventDim));
                if (inventDimDs)
                {
                    // Set the datasource record to the found InventDim, make it current and refresh UI
                    inventDimDs.setRecord(invDimRec);
                    inventDimDs.setCurrent();
                    inventDimDs.refresh();
                }

                // Also refresh the PurchLine datasource so UI shows any other updated fields
                formDataSource.refresh();
            }

            info(strFmt("After update: InventDimId = %1", purchLine.InventDimId));
        }

    }

}
[ExtensionOf(tableStr(PurchLine))]
public final class PurchLine_Extension
{
public boolean shouldRetrieveBatchNumber()
{
    PurchParameters purchParameters = PurchParameters::find();
    PurchTable      purchTable      = this.purchTable();

    return purchParameters.RetrieveBatchNum == NoYes::Yes
    && (purchTable.PurchaseType == PurchaseType::ReturnItem
        || purchTable.PurchaseType == PurchaseType::Purch)
    && this.LineAmount < 0;
}

public void updateBatchNumber()
{
    EcoResTrackingDimensionGroupItem    ecoResTrackingDimensionGroupItem;
    EcoResTrackingDimensionGroup        ecoResTrackingDimensionGroup;
    InventTable                         inventTable;
    InventNumGroup                      inventBatchNumGroup;
    InventDim                           inventDim;

    ecoResTrackingDimensionGroupItem    = EcoResTrackingDimensionGroupItem::findByItem(this.DataAreaId,this.ItemId);
    inventTable					        = InventTable::find(this.ItemId);
    inventBatchNumGroup                 = InventNumGroup::find(inventTable.BatchNumGroupId);
    inventDim                           = this.inventDim();

    select firstonly ecoResTrackingDimensionGroup
        where ecoResTrackingDimensionGroup.RecId == ecoResTrackingDimensionGroupItem.TrackingDimensionGroup;


    if(this.shouldRetrieveBatchNumber() && inventBatchNumGroup.FixedValue == NoYes::Yes && inventBatchNumGroup.InclExpectedDate == NoYes::No && ecoResTrackingDimensionGroup.Name == 'Batch')
    {
        inventDim.inventBatchId = inventTable.BatchNumGroupId;

        // Find or create the dimension combination
        this.InventDimId = InventDim::findOrCreate(inventDim).InventDimId;
        ///this.modifyInventDim(inventDim,fieldNum(InventDim, inventBatchId), false);

    }
}
}
Please let me know where i am wrong or how can i flow the batch id. I also wrote the code on the table modified field but again the inventdimId is not getting updated.
Categories:
I have the same question (0)
  • Sohaib Cheema Profile Picture
    49,677 Super User 2026 Season 1 on at
    Hi,
    I see an issue in your solution design.
    As soon as an item is selected in Purchase Line, system invokes the method that indicates that item has been modified. This also brings default inventory dimensions values (for the selected item). 
    You are trying to update the inventory dimension, at data source field level. You should check the logic in x++ classes that can override your logic. 
  • spidey1010 Profile Picture
    201 on at
    Hi Sohaib, 
    I already checked but couldn't find it. Can you please help /check. 

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!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

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

#1
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 617

#2
André Arnaud de Calavon Profile Picture

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

#3
Syed Haris Shah Profile Picture

Syed Haris Shah 298 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans