Skip to main content
Dynamics 365 Community / Forums / Finance forum / How to save the SalesI...
Finance forum
Suggested answer

How to save the SalesId into my Custom table when the Sales Order is created

editSubscribe (1) ShareShare
ReportReport
Posted on by 68
I have a question regarding the customization in D365FO.
 
I have a requirement to create two checkboxes field in a custom table, named ADS_SalesOrderCBTable. The table is added as the formDataSource in the SalesTable form.
//
 
The table contains one field which needs to store the SalesId. I have created the foreign key relation to the custom SalesId field.
 
////
 
However, I find that when I create the sales order and check the checkbox field, the SalesId will not the stored in the custom table. There is only the checkbox value recorded.
////
 
So I would like to ask to get the SalesId value into my custom table, ADS_SalesOrderCBTable, is it I need to write some code such as initFrom in the table to get the SalesId from my sales order? If yes, the initFrom method should be called in the form class, am I right?
 
Kindly please correct me if I get anything wrong.

Thank you.

Regards,
Yue Zhen
Attachments
  • Yue Zhen Profile Picture
    Yue Zhen 68 on at
    How to save the SalesId into my Custom table when the Sales Order is created
    Hi Sir Martin,
     
    Thank you for the comment. I have changed the ads_SalesId field to mandatory. And Yes, my code create a record in the database and form knows nothing about. So, I am trying to write something on the form when the CustAccount field is modified, I should set the checkbox value based on the selected CustAccount. I am still stuck in the stage setting the checkbox to checked status. Below is my current code and output.
     
    [ExtensionOf(formDataFieldStr(SalesCreateOrder, SalesTable, CustAccount))]
    final class SalesCreateOrder_ADSEInvoice_Extension
    {
        public void modified()
        {
            FormDataObject custAcc = any2Object(this) as FormDataObject;
            FormDataSource fds = custAcc.datasource();
            SalesTable salesTable = fds.cursor();
            CustTable custtable;
            ads_SalesOrderCBTable salesOrderTable;
    
            next modified();
    
            select * from custtable where custtable.AccountNum == salesTable.CustAccount;
            salesOrderTable.ads_EInvCheckbox = custtable.ads_EInvCheckbox;
            salesOrderTable.ads_ConsoEInvCheckbox = custtable.ads_ConsolidateEInvCheckbox;
            
            info(strFmt("%1", salesOrderTable.ads_EInvCheckbox));
        }
    }
     
     
    Previously, I managed to set the checkbox value to YES, but it was through the table's initValue() method. This approach seems cannot work for this situation because I haven't create the sales order at this stage and no SalesId record in my ADS_SalesOrderCBTable.
     
  • Martin Dráb Profile Picture
    Martin Dráb 222,733 Super User on at
    How to save the SalesId into my Custom table when the Sales Order is created
    The first thing I would do is fixing the bug in your data model. I don't believe that creating a record without any value of ads_SalesId makes sense, therefore you should make it mandatory. It'll also make the problem more visible, because the logic will fail instead of silently creating invalid records in database.
     
    One problem I see is that your code creates a record in database, but the form knows nothing about. You'll need to research the data source.
  • Yue Zhen Profile Picture
    Yue Zhen 68 on at
    How to save the SalesId into my Custom table when the Sales Order is created
    Hi Sir Martin,
     
    I add the table as datasource to the SalesTable form. I select SalesTable as the join source and "Delayed" as its link type.
     
    And I am sorry that I have another question which is related to the changed requirement from the functional team. They would like to have the checkboxes in the SalesOrderCreate form as below.
    When I add in the checkbox and try to create the sales order, I find that the data for the checkbox is not sync after the sales order is created. So, for this business requirement, I guess I shall change my way of developing it, am I right?

    The checkbox is from the ADS_SalesOrderCBTable as well.
  • Martin Dráb Profile Picture
    Martin Dráb 222,733 Super User on at
    How to save the SalesId into my Custom table when the Sales Order is created
    Yue Zhen, please tell us how you configured your data source. Have you selected SalesTable as the parent data source? What LinkType have you used?
  • Suggested answer
    Nisha Soni 2709 Profile Picture
    Nisha Soni 2709 48 on at
    How to save the SalesId into my Custom table when the Sales Order is created
    You can try this below code to pass your salesid from saletable to your custom table
     
    [ExtensionOf(tableStr(SalesTable))]
    final class SalesTable_ADSEInvoice_Extension
    {
        void insert()
        {
            next insert();
            if(this.EinvoiceCheckbox == Noyes::Yes)
            {
                    ads_SalesOrderCBTable salesOrderCBTable;
                    salesOrderCBTable.clear();
                    salesOrderCBTable.ads_SalesId = this.SalesId;
                    salesOrderCBTable.insert();
            }
        }
    }
  • Yue Zhen Profile Picture
    Yue Zhen 68 on at
    How to save the SalesId into my Custom table when the Sales Order is created
    Hi Sir Amine,
     
    Based on my latest SQL screenshot, I think I have get the thing correct. I changed my form datasource's link type from Outer Join to Delayed. After that, it seems like it can update checkbox value in the line with SalesID. Hmm, I did it but seems I am not really understand. 
  • Suggested answer
    Mohamed Amine Mahmoudi Profile Picture
    Mohamed Amine Mahmoudi 3,905 User Group Leader on at
    How to save the SalesId into my Custom table when the Sales Order is created
    Hi @Yue Zhen ,
     
    you forgot to delete or comment out the old code in the initFromSalesTable method.
     
    Best regards,
    Mohamed Amine Mahmoudi
  • Yue Zhen Profile Picture
    Yue Zhen 68 on at
    How to save the SalesId into my Custom table when the Sales Order is created
    .[ExtensionOf(tableStr(SalesTable))]
    final class SalesTable_ADSEInvoice_Extension
    {
        void insert(boolean _skipMarkup)
        {
            next insert(_skipMarkup);
            ads_SalesOrderCBTable salesOrderCBTable;
            salesOrderCBTable.clear();
            
            salesOrderCBTable.ads_SalesId = this.SalesId;
            salesOrderCBTable.insert();
            
        }
    }

    Currently, I am using this code. When I do testing, I find out that there is one line record with the salesID created in ADS_SalesOrderCBTable, with the initial both checkbox values = 0.
     
    After that, I try to check the consolidate e-invoice checkbox in the form.
     
    I find out it create a new line of record without salesID but with consolidate e-invoice checkbox value = 1.
     
    Then I refresh the form and the Consolidate e-invoice checkbox is back to uncheck.
     
    I check the same checkbox again and save. I run the SQL and notice that the record line with SalesID = 'MYMF-000766' is updated.
  • Martin Dráb Profile Picture
    Martin Dráb 222,733 Super User on at
    How to save the SalesId into my Custom table when the Sales Order is created
    Please show us your current code and tell us what you found when you run your code in debugger.
  • Yue Zhen Profile Picture
    Yue Zhen 68 on at
    How to save the SalesId into my Custom table when the Sales Order is created
    This reply is to clarify my business requirement.

    I need to create two checkboxes and other fields that related to E-Invoice. According to the business requirement, these custom fields should be able to be edited even after the sales order is posted. (A subform that contains only the custom fields will be created later to allow users to modify the custom fields values after invoice posted.)
     
    So, I create the fields in an custom table named, ADS_SalesOrderCBTable. Currently, there are three fields in this table which are SalesId, EInvoiceCheckbox and ConsoEInvCheckbox. The SalesId in the custom table will play the role to identify which sales order the record is referring to.

    For now, I can see a record is created in the ADS_SalesOrderCBTable when I clicked save on the SalesTable form. However, the SalesId field of ADS_SalesOrderCBTable table is still empty.

    Yet, my question is how can I pass the SalesId value into my ADS_SalesOrderCBTable table when I save the Sales Order record? 
     
    I try to refer to how MS did for SalesLine record, but I find it is complicated and I cannot understand. 

Helpful resources

Quick Links

New Blog Features Released!

Check out the new community blog features for viewers and authors…

Setting Up Knowledge Sources for Copilot…

Look at how configuring a comprehensive knowledge base is crucial…

Demystifying Copilot with Georg Glantschnig…

Industry experts answer burning questions directly from our amazing Community…

Leaderboard

#1
Andre Arnaud de Calavon Profile Picture

Andre Arnaud de Cal... 283,183 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 222,733 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,138

Featured topics

Product updates

Dynamics 365 release plans