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 create an order (header + lines) using c# (odata)

(0) ShareShare
ReportReport
Posted on by 1,915

Hi,

I downloaded the code from gitHub and i'm trying to call this method "CreateSalesOrderInSingleChangeset"

Here's my code:

public static void CreateSalesOrderInSingleChangeset(Resources context)
        {
            try
            {
                SalesOrderHeader salesOrder = new SalesOrderHeader();
                DataServiceCollection salesOrderCollection = new DataServiceCollection(context);
                salesOrderCollection.Add(salesOrder);

                //salesOrder.SalesOrderNumber = salesOrderNumber; // Change number sequence setting in AX to allow user to set values.
                salesOrder.CurrencyCode = "GBP";
                salesOrder.InvoiceCustomerAccountNumber = "Cust1";
                salesOrder.OrderingCustomerAccountNumber = "Cust1";
                salesOrder.LanguageId = "en-gb";
                salesOrder.DataAreaId = "comp";

                context.MergeOption = MergeOption.OverwriteChanges;
                context.SaveChanges(SaveChangesOptions.PostOnlySetProperties | SaveChangesOptions.BatchWithIndependentOperations);


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

                salesOrderLine.SalesOrderNumber = salesOrder.SalesOrderNumber;
                salesOrderLine.ItemNumber = "item1";
                salesOrderLine.OrderedSalesQuantity = 1;
                salesOrderLine.ShippingSiteId = "0106";
                salesOrderLine.ShippingWarehouseId = "Loc1";
                salesOrderLine.DataAreaId = "comp";

                context.SaveChanges(SaveChangesOptions.PostOnlySetProperties | SaveChangesOptions.BatchWithSingleChangeset); // Batch with Single Changeset ensure the saved changed runs in all-or-nothing mode.
                Console.WriteLine(string.Format("Invoice {0} - Saved !", salesOrderNumber));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.InnerException.Message);
            }
        }

1. It seems at the header i'm getting this error:

"insert not allowed for field 'CustomField'","type":"Microsoft.Dynamics.Platform.Integration.Services.OData.ODataSecurityException","stacktrace":

** this field is not mine, it's customized (other model)How to solve it?

 

2) Is using the following two lines for creating sales order header and line together and is correct? cause currently it seems i'm saving twice one for the header and one for line.

( i want to use standard number sequence for the sales order number in d365)


context.MergeOption = MergeOption.OverwriteChanges;
context.SaveChanges(SaveChangesOptions.PostOnlySetProperties | SaveChangesOptions.BatchWithIndependentOperations);

I have the same question (0)
  • Martin Dráb Profile Picture
    236,804 Most Valuable Professional on at
    RE: How to create an order (header + lines) using c# (odata)

    Are you trying to set 'CustomField' in your C# code, or aren't you referring to it at all? Do you agree that the field shouldn't accept input, or do you believe that it should (and the customization is incorrect)?

  • .. Profile Picture
    1,915 on at
    RE: How to create an order (header + lines) using c# (odata)

    Hi Martin,

    No i'm not setting this customField value and not referring to it at all.

    yes i don't beliver it should accept input, what should i do with this error?

  • Martin Dráb Profile Picture
    236,804 Most Valuable Professional on at
    RE: How to create an order (header + lines) using c# (odata)

    You'll need to debug it.

    Start by checking references to the custom field; it might reveal something interesting. If it doesn't help, use the debugger to find out what's going on inside the entity.

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
André Arnaud de Calavon Profile Picture

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

#2
CA Neeraj Kumar Profile Picture

CA Neeraj Kumar 743

#3
Sumit Singh Profile Picture

Sumit Singh 551

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans