web
You’re offline. This is a read only version of the page.
close
Skip to main content
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?

(1) ShareShare
ReportReport
Posted on by 179
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,232 User Group Leader on at
    How to update the inventdimId in purchTable form on the update of inventDim batch id field?
    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
    179 on at
    How to update the inventdimId in purchTable form on the update of inventDim batch id field?
    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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Pallavi Phade – Community Spotlight

We are honored to recognize Pallavi Phade as our Community Spotlight honoree for…

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

#1
Martin Dráb Profile Picture

Martin Dráb 719 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 650 Super User 2025 Season 2

#3
CU05031448-0 Profile Picture

CU05031448-0 536

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans