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 :
Finance | Project Operations, Human Resources, ...
Answered

How to Update a Vendor via Odata endpoint

(0) ShareShare
ReportReport
Posted on by 60

I'm trying to update a vendor via the Odata endpoint in d365 F&O, version 10.0.13.  This is a Contoso environment.  I've tried several different ways to arrange the code but, I'm getting one of two errors when using the SaveChanges() method.

Here's the code - I'm deliberately not updating anything in the Vendor.  I'm just passing back the same object that I got for the Odata endpoint but, I still get an error:

in class constructor:

            Uri oDataUri = new Uri(AXURL, UriKind.Absolute);
            AXResources = new Microsoft.Dynamics.DataEntities.Resources(oDataUri);
            AXResources.SendingRequest2  = new EventHandler(delegate (object sender, SendingRequest2EventArgs e)
            {
                e.RequestMessage.SetHeader("Authorization", GetAuthHeader());
            });

            AXResources.BuildingRequest  = (sender, e) =>
            {
                var uriBuilder = new UriBuilder(e.RequestUri);
                var paramValues = HttpUtility.ParseQueryString(uriBuilder.Query);
                paramValues.Add("cross-company", "true");
                uriBuilder.Query = paramValues.ToString();
                e.RequestUri = uriBuilder.Uri;

            };

var query = AXResources.Vendors.Where(v => v.VendorAccountNumber == "1002");
DataServiceCollection updatedVendors = new DataServiceCollection(query, TrackingMode.None);
foreach (Vendor vendor2 in updatedVendors)
{
if (vendor2.VendorAccountNumber == "1002")
{
AXResources.UpdateObject(vendor2);
}

}
AXResources.SaveChanges();//"update not allowed for field 'VendorExceptionGroupId'"
//AXResources.SaveChanges(SaveChangesOptions.PostOnlySetProperties); //"Must be used with DataServiceCollection"
//AXResources.SaveChanges(SaveChangesOptions.BatchWithSingleChangeset); //"update not allowed for field 'VendorExceptionGroupId'"

I've tried this in Visual Studio 2019 and 2017 using the V4 Odata client as well as the new Odata service generator that is still in Beta - both get the same results.  

I also got similar errors when trying to update a customer.

What am I missing?

I have the same question (0)
  • Sergei Minozhenko Profile Picture
    23,093 on at

    Hi BG@Dynamics,

    You need to use SaveChangesOptions.PostOnlySetProperties option for SaveChanges to send only changed fields to F&O, otherwise request will contains values for fields you didn't change.

    var query = AXResources.Vendors.Where(v => v.VendorAccountNumber == "1002");
    DataServiceCollection updatedVendors = new DataServiceCollection(query, TrackingMode.None);
    foreach (Vendor vendor2 in updatedVendors)
    {
        if (vendor2.VendorAccountNumber == "1002")
        {
            updatedVendors.Add(vendor2);
            AXResources.UpdateObject(vendor2);
        }
    
    }
    
    AXResources.SaveChanges(SaveChangesOptions.PostOnlySetProperties);

  • BG@Dynamics Profile Picture
    60 on at

    I've tried that - see line 12 in my code above, commented.  I got an error message - "Must be used with DataServiceCollection" even though I'm using one?

  • Sergei Minozhenko Profile Picture
    23,093 on at

    Hi BG@Dynamics

    I believe it worked before, but maybe I'm wrong. Just tried another solution with AutoChangeTracking and it works fine for me.

    var query = AXResources.Vendors.Where(v => v.VendorAccountNumber == "1002");
    DataServiceCollection updatedVendors = new DataServiceCollection(query, TrackingMode.AutoChangeTracking);
    foreach (Vendor vendor2 in updatedVendors)
    {
        if (vendor2.VendorAccountNumber == "1002")
        {
            //Update only needed fields
        }
    }
    
    AXResources.SaveChanges();

  • BG@Dynamics Profile Picture
    60 on at

    Nope, not working on my end.  I am getting a different error - "precondition failed".  I've only tried a couple of properties on the Vendor.  What properties on the vendor object are you setting and still getting a successful update?

  • Sergei Minozhenko Profile Picture
    23,093 on at

    Hi BG@Dynamics,

    Here is the full code i used for testing

    var query = d365.Vendors.Where(x => x.VendorAccountNumber == "00002" || x.VendorAccountNumber == "00006");             
    DataServiceCollection updatedVendors = new DataServiceCollection(query, TrackingMode.AutoChangeTracking);
    foreach (Vendor vendor2 in query)
    {
        vendor2.PrimaryEmailAddress = "test@gmail.com";
    }
    
    d365.SaveChanges();

    And it's what I see from Fiddler

    PATCH /data/Vendors(dataAreaId='100',VendorAccountNumber='00002')?cross-company=true HTTP/1.1
    
    {"@odata.type":"#Microsoft.Dynamics.DataEntities.Vendor","PrimaryEmailAddress":"test@gmail.com"}
    
    PATCH /data/Vendors(dataAreaId='100',VendorAccountNumber='00006')?cross-company=true HTTP/1.1
    
    {"@odata.type":"#Microsoft.Dynamics.DataEntities.Vendor","PrimaryEmailAddress":"test@gmail.com"}

    I'm using oData client v7.6.4.0

  • BG@Dynamics Profile Picture
    60 on at

    Sergei - I really appreciate the input and your gratuity.

    I'm getting the "PreconditionFailed" error message when trying to update the primary email address.

    Client version v7.6.4.0 - Is this the version of the code generator?  I'm using version 7.5.1 and no updates are present.  How can I get 7.6.4?

    pastedimage1602001967919v1.png

  • Verified answer
    Sergei Minozhenko Profile Picture
    23,093 on at

    Hi BG@Dynamics,

    I'm using the same version for the client code generator.

    But I meant these libraries

    pastedimage1602178807229v2.png

  • Fangyuan Hou Profile Picture
    on at

    Hi BG,

    Do you still have this issue, what's the latest code, there is a demo Project on the github, you can reference.

    https://github.com/microsoft/Dynamics-AX-Integration

    Please feel free raise a support ticket for further troubleshooting. 

  • BG@Dynamics Profile Picture
    60 on at

    Yes, I just tried it with the updated NuGet packages and it's working.- latest version 7.7.2

    pastedimage1602594339857v1.png

    Here's the update code - the authentication code remains the same as my original post:

    var query = AXResources.Vendors.Where(v => v.VendorAccountNumber == "1002");
    DataServiceCollection updatedVendors = new DataServiceCollection(query, TrackingMode.AutoChangeTracking);
    foreach (Vendor vendor2 in updatedVendors)
    {
        vendor2.PrimaryEmailAddress = "somebody@mail.com"; 
        //AXResources.UpdateObject(vendor2);
    }
    
    AXResources.SaveChanges();

    The end result of this update, through the magic of the entities, is that a "Contact information" record for the email address that is passed in was created on the vendor.  This is in a Contoso VM - d365 F&O 10.0.13.

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 > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 503 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

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

#3
BillurSamdancioglu Profile Picture

BillurSamdancioglu 278 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans