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 :
Business Central forum

Error Saving a new Sales Invoice using ODataService

(0) ShareShare
ReportReport
Posted on by

I'm trying to add a new Sales Invoice using OData Service. But while Saving it end up with the error "NotAcceptable".

Here is the code that I've Used. It error out on "nav.SaveChanges();" line. 

NAV nav = new NAV(new Uri("tenant.dynamics.com/.../Company('Company')"));
nav.Credentials = new NetworkCredential("username", "code");
SalesInvoice invoice = new SalesInvoice()
            {
                Document_Type = "Invoice",
                Sell_to_Customer_Name = "20000",
                Posting_Date=DateTime.Now
            };
            nav.AddObject("SalesInvoices", invoice);

            SalesInvoiceSalesLines line = new FEAPIClient.SalesInvoiceSalesLines()
            {
                Description = "1896-S"  ,
                Quantity = 1
            };
            nav.AddRelatedObject(invoice, "SalesInvoiceSalesLines", line);

            DataServiceResponse response = nav.SaveChanges();


Thanks in advance for helping out!

Regards,

Himal

Categories:
  • Suggested answer
    Mohana Yadav Profile Picture
    61,252 Super User 2026 Season 2 on at

    things I noticed in your code is,

    we need to assign customer no. to Sell_to_Customer_No. and not name..

    also Item No. to No. field in Sales line and not Description.

  • Verified answer
    Community Member Profile Picture
    on at

    Hi Mohana,

    From one of the link I got following.

    http://www.interdynartis.com/blog/use-the-dynamics-365-for-financials-odata-service

    "The relationships between records (i.e. between the invoice and the customer) are through the Name fields, but you can specify the key values. Above, I have specified Customer No. 20000 for the Sell_to_Customer_Name."

    About the error, i figured it out. The issue was with the odata client i was using. I added the service reference using "Add Service reference" in Visual Studio. And as the OData4 Service doesn't support that. I need to use different approach to generate the Client Code for the Service. I used following link for the same. 

    https://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-v4/create-an-odata-v4-client-app

    However, even with that face the one more issue. All other properties of the object which is null, are set to null (text) when the object is serialized and sent to the API. and that result in the error, "HTTP/1.1 400 Data object not provided." Example of the input is "{"No":null,"Name":"Customer Name-2"}". I couldn't find any way to excluding those null properties during serialize in OData Client. So I end up using the HttpWebRequest instead.

    Please let me know if you have any other better suggestion.

    Regards,
    Himal

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard > Business Central

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans