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

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

  • Suggested answer
    Mohit Rampal Profile Picture
    12,573 Moderator on at

    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;
    }

  • Rahul Dahiya Profile Picture
    72 on at

    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

  • Verified answer
    André Arnaud de Calavon Profile Picture
    307,691 Super User 2026 Season 2 on at

    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
    72 on at

    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

  • Suggested answer
    Mohit Rampal Profile Picture
    12,573 Moderator on at

    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
    72 on at

    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
    12,573 Moderator on at

    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
    72 on at

    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

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the August Top 10 Community Leaders

These are the community rock stars!

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

#1
Martin Dráb Profile Picture

Martin Dráb 507 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 419 Super User 2026 Season 2

#3
CU-1234529-002 Profile Picture

CU-1234529-002 155

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans