Skip to main content

Notifications

Finance | Project Operations, Human Resources, ...
Answered

Modifying/Merging Default Dimension for a free Text Invoice based on Default Dimension of a given InventSiteId field

(0) ShareShare
ReportReport
Posted on by 54

Hi,

while creating a free text invoice, when I choose a customer account, it will update the default Dimension (Business unit) in the free text invoice Header. I have created a custom field HSHTSiteId in CustInvoiceTable which has InventSiteID as EDT. 

My requirement is when I modify HSHTSiteId & choose some other value then default Dimension in free text invoice Header must be updated to the default dimension related to HSHTSiteId as InventSiteID has default Dimension related to it.

I have write onModified Event Handler on CustInvoiceTable: 

    public static void CustInvoiceTable_HSHTSiteId_OnModified(FormControl sender, FormControlEventArgs e)
    {
        CustInvoiceTable custInvoiceTable;

        FormRun element = sender.FormRun();
        FormStringControl siteID = element.design().controlName(formControlStr(CustFreeInvoice,CustInvoiceTable_HSHTSiteId));

        select custInvoiceTable;

        if (siteID)
        {
            custInvoiceTable.DefaultDimension = LedgerDimensionDefaultFacade::serviceMergeDefaultDimensions(custInvoiceTable.DefaultDimension, InventSite::find
                (custInvoiceTable.HSHTSiteId).DefaultDimension);
        }

    }

But the Default Dimension value is not updating in Freetextinvoice Header. I debug the code & found getting 0 value returned.

HSHTSiteIDDField.png

CustInvoiceTableDD.png

Can anyone suggest How can I do this.

Thanks 

Rahul

  • Rahul Dahiya Profile Picture
    Rahul Dahiya 54 on at
    RE: Modifying/Merging Default Dimension for a free Text Invoice based on Default Dimension of a given InventSiteId field

    Hi Mohit,

    Thanks for the reply.

    I used this.HSHTSiteId to pass into find method, now I am able to fetch updated value without even saving. It gets worked perfectly.

    Thanks,

    Rahul

  • Suggested answer
    Mohit Rampal Profile Picture
    Mohit Rampal 12,552 Super User 2024 Season 1 on at
    RE: Modifying/Merging Default Dimension for a free Text Invoice based on Default Dimension of a given InventSiteId field

    Hi Rahul, Have you tried the code I suggested above? You are using modifiedField method and checking HSHTSiteId field, so when user add/modify value in HSHTSiteId field, you can get it without even saving record first. Please comment Line#14 and replace it with below code and see if you get the value without saving record.

    inventSiteId = this.HSHTSiteId;

  • Rahul Dahiya Profile Picture
    Rahul Dahiya 54 on at
    RE: Modifying/Merging Default Dimension for a free Text Invoice based on Default Dimension of a given InventSiteId field

    Hi Mohit,

    Below Code is working fine but firstly I need to save the record after selecting a siteId in dropdown then only I am able to get the defaultDimension related to that siteId.

    Is their any way to save the record in code itself as soon as I select a siteId in dropdown. after that I can get the reference of SiteId selected.

    [ExtensionOf(tableStr(CustInvoiceTable))]
    final class HSHTCustInvoiceTable_Extension
    {
        public void modifiedField(FieldId _fieldId)
        {
            CustInvoiceTable custInvoiceTable;
            InventSiteId     inventSiteId;
        
            next modifiedField(_fieldId);
            
            switch(_fieldId)
            { 
                case fieldNum(CustInvoiceTable,HSHTSiteId) :
                    inventSiteId = CustInvoiceTable::findRecId(this.RecId).HSHTSiteId;
    
                    this.DefaultDimension = DimensionDefaultFacade::serviceMergeDefaultDimensions(custInvoiceTable.DefaultDimension, InventSite::find
                    (inventSiteId).DefaultDimension);
            }
        }
    
    }

  • Suggested answer
    Mohit Rampal Profile Picture
    Mohit Rampal 12,552 Super User 2024 Season 1 on at
    RE: Modifying/Merging Default Dimension for a free Text Invoice based on Default Dimension of a given InventSiteId field

    Select statement will not work for table buffer if the record is not yet saved. Can you comment line#12 and try using this.HSHTSiteId instead of custInvoiceTable.HSHTSiteId in Line#15 in CoC method.

  • Rahul Dahiya Profile Picture
    Rahul Dahiya 54 on at
    RE: Modifying/Merging Default Dimension for a free Text Invoice based on Default Dimension of a given InventSiteId field

    Hi Andre,

    I have wrote Onmodifiedfield event handler on table  as you suggested:

       [DataEventHandler(tableStr(CustInvoiceTable), DataEventType::ModifiedField)]
        public static void CustInvoiceTable_onModifiedField(Common sender, DataEventArgs e)
        {
            ModifyFieldEventArgs event = e as ModifyFieldEventArgs;
            CustInvoiceTable custInvoiceTable = sender as CustInvoiceTable;
            FieldId _fieldId = event.parmFieldId();
    
            switch(_fieldId)
            {
                case fieldNum(CustInvoiceTable,HSHTSiteId):
                   
                    custInvoiceTable.DefaultDimension = DimensionDefaultFacade::serviceMergeDefaultDimensions(custInvoiceTable.DefaultDimension, InventSite::find
                    (custInvoiceTable.HSHTSiteId).DefaultDimension);
            }
        } 

    I am passing custInvoiceTable.HSHTSiteId in find method in 13th line to find corresponding defaultDimension, but this field is null.

    How can I fetch the value selected in HSHTSiteId lookup field using select statement in Event Handler to pass it in find method. I used Select custInvoiceTable; but while creating a new freetextInvoice, this HSHTSiteId value I selected is not stored in database yet, then how it will fetch using select statement.

    I tried COC for modifiedfield method as well:

    [ExtensionOf(tableStr(CustInvoiceTable))]
    final class HSHTCustInvoiceTable_Extension
    {
        public void modifiedField(FieldId _fieldId)
        {
            CustInvoiceTable custInvoiceTable;
            next modifiedField(_fieldId);
    
            switch(_fieldId)
            {
                case fieldNum(CustInvoiceTable,HSHTSiteId) :
                    select firstonly custInvoiceTable where custInvoiceTable.OrderAccount == this.OrderAccount;
                    info(strFmt("SiteId : %1",custInvoiceTable.HSHTSiteId));
                    this.DefaultDimension = DimensionDefaultFacade::serviceMergeDefaultDimensions(this.DefaultDimension, InventSite::find
                    (custInvoiceTable.HSHTSiteId).DefaultDimension);
            }
        }
    
    }

    but this info returns nothing, how can we fetch the value selected in HSHTSiteId field.

    Thanks, 

    Rahul

  • Verified answer
    André Arnaud de Calavon Profile Picture
    André Arnaud de Cal... 291,965 Super User 2025 Season 1 on at
    RE: Modifying/Merging Default Dimension for a free Text Invoice based on Default Dimension of a given InventSiteId field

    Hi Rahul,

    I would suggest writing an event handler on the table instead of acting on the form control. In case a user would add controls as a personalization, it will not execute the coding.

    Besides, the coding you used now will not work anyway.

    1) The sender is the form control. Now with coding, you try to get the formRun and then again the formcontrol SiteID.

    2) With the newly found form control, you are not checking if a value is provided, but if the formstring control existst or not.

    3) You are selecting a CustInvoiceTable, but you are not providing any ranges to retrieve which exact record. For sure this is not the record which is active on the form.

    The best would be creating a event handler method for the onModifiedField event on the table CustInvoiceTable.

  • Rahul Dahiya Profile Picture
    Rahul Dahiya 54 on at
    RE: Modifying/Merging Default Dimension for a free Text Invoice based on Default Dimension of a given InventSiteId field

    Hi Mohit,

    I have tried above code you suggested but not working, I debug the code & found that the value I am passing inside find method (InventSite::find(custInvoiceTable.HSHTSiteId).DefaultDimension;) is actually null. before this I have used select custInvoiceTable but the value for HSHTSiteid is initially null at the time of creating the free text Invoice.

    then How can I fetch the HSHTSiteId value for passing in find method bcz value is null in database itself.

    Thanks

    Rahul

  • Suggested answer
    Mohit Rampal Profile Picture
    Mohit Rampal 12,552 Super User 2024 Season 1 on at
    RE: Modifying/Merging Default Dimension for a free Text Invoice based on Default Dimension of a given InventSiteId field

    Hi Rahul, If I understood your requirement correctly, you want to replace Default Dimension not merge it. Please try below code.

    if (siteID)
    {
        custInvoiceTable.DefaultDimension = InventSite::find(custInvoiceTable.HSHTSiteId).DefaultDimension;
    }

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,965 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,836 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans