web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)
Suggested Answer

Create Sales orders with lines by using OData and using numbersequence for Sales order number

(0) ShareShare
ReportReport
Posted on by 5

I want to create with OData a Sales order with lines within one OData batch. In the example on GitHub, the sales order number is provided from the code.

I want to generate the number in Operations. How do I connect the sales lines to the order, because the number is not yet known.

In the current situtation, the validation for the sales line fails.

See sample code:

                SalesOrderHeader salesOrder = new SalesOrderHeader();
                DataServiceCollection<SalesOrderHeader> salesOrderCollection = new DataServiceCollection<SalesOrderHeader>(context);
                salesOrderCollection.Add(salesOrder);

                salesOrder.OrderingCustomerAccountNumber = "DE-010";
                salesOrder.DataAreaId = "DEMF";

                SalesOrderLine salesOrderLine = new SalesOrderLine();
                DataServiceCollection<SalesOrderLine> salesOrderLineCollection = new DataServiceCollection<SalesOrderLine>(context);
                salesOrderLineCollection.Add(salesOrderLine);

                //salesOrderLine.SalesOrderNumber = salesOrder.SalesOrderNumber;
                SalesOrderLine.ItemNumber = "D0001";
                salesOrderLine.OrderedSalesQuantity = 2;
                salesOrderLine.DataAreaId = "DEMF";
                
                context.SaveChanges(SaveChangesOptions.PostOnlySetProperties | SaveChangesOptions.BatchWithSingleChangeset); // Batch with Single Changeset ensure the saved changed runs in all-or-nothing mode.


*This post is locked for comments

I have the same question (0)
  • JL-18061441-1 Profile Picture
    5 on at

    Hi Sukrut,

    But the issue is that I don't have the SalesOrderNumber because that one will be generated in Operations by a number sequence.

    Regards, Jeroen

  • Ingo_Jansen Profile Picture
    15 on at

    Hi Jeroen,

    currently i have exactly the same problem as you mentioned. Did you find a solution in the meantime to get it done?

    Regards,

    Ingo

  • Suggested answer
    Ingo_Jansen Profile Picture
    15 on at

    Hi Jeroen,

    finally I found a solution (D365FO) to save a SalesOrder including lines using the sales order number generated in Operations.

    SalesOrderHeader salesOrder = new SalesOrderHeader(); 

    DataServiceCollection<SalesOrderHeader> salesOrderCollection = new DataServiceCollection<SalesOrderHeader>(context);

    salesOrderCollection.Add(salesOrder);

    salesOrder.OrderingCustomerAccountNumber = "DE-010";

    salesOrder.DataAreaId = "DEMF";

    //Context default MergeOption is set to AppendOnly, causing no update on your already existing object in the context

    context.MergeOption = MergeOption.OverwriteChanges;

    //i didn't try to use SaceChanges() only at the end :-(, so it is base now on 2 save actions.

    Context.SaveChanges(SaveChangesOptions.PostOnlySetProperties | SaveChangesOptions.BatchWithSingleChangeset);

    SalesOrderLine salesOrderLine = new SalesOrderLine();

    DataServiceCollection<SalesOrderLine> salesOrderLineCollection = new DataServiceCollection<SalesOrderLine>(context);

    salesOrderLineCollection.Add(salesOrderLine);

    //salesOrder is updated through save operation before, so i can assign the correct Operations generated SalesOrderNumber to the line

    salesOrderLine.SalesOrderNumber = salesOrder.SalesOrderNumber;

    SalesOrderLine.ItemNumber = "D0001";

    salesOrderLine.OrderedSalesQuantity = 2;

    salesOrderLine.DataAreaId = "DEMF";

    context.SaveChanges(SaveChangesOptions.PostOnlySetProperties | SaveChangesOptions.BatchWithSingleChangeset);

    Hope that could solve your problem.

    Regards,

    Ingo

  • Ingo_Jansen Profile Picture
    15 on at

    Hi Sukrut,

    you are right this solution uses 2 transactions. I don‘t have had the time to figure out what happens using it within one transaction. As soon I checked this I will post it in addition. But by using two transactions it uses the automatic generated salesordernumber of the SalesOrderHeader in operations when adding the lines in the second transaction.

    Regards,

    Info

  • JL-18061441-1 Profile Picture
    5 on at

    Hi Ingo and Sukrut,

    So it seems not possible to do it in one transaction.

    In that case, you might need to update your header after inserting the lines to indicate that this sales order is 'ready to process'. Within D365FO you process only the sales orders with a specific status indicating its readiness.

    Regads, Jeroen

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans