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)

Updating records using odata

(0) ShareShare
ReportReport
Posted on by 692

Hi

I need to update odata records that I looping through using a linq query, but an exception is being thrown when I call SaveChanges. Here is a basic outline of the code - hopefully this will be enough to demonstrate what I am doing wrong

var query = from BatchOrder 
	in context.BatchOrders
	where BatchOrder.Sent == NoYes.No
	select BatchOrder;

foreach (var batchOrder in query)
{
	// do some processing on the batchOrder record here

	// update AX record
	batchOrder.Sent = NoYes.Yes;
}

// send changes to the AX records back to AX
context.SaveChanges(SaveChangesOptions.PostOnlySetProperties);

The exception message is "'SaveChangesOptions.OnlyPostExplicitProperties' must be used with 'DataServiceCollection'", however I cannot find any examples of how to use a DataServiceCollection when looping through records.

Can someone point me in the right direction please?

Thanks,
Joe

*This post is locked for comments

I have the same question (0)
  • Community Member Profile Picture
    on at

    Hello,

    Did you get the above solved?

    We are facing similar case.

    Thanks,

    Rick

  • Suggested answer
    Community Member Profile Picture
    on at

    I have had some success updating values using the DataServiceCollection as shown below (I am assuming BatchOrder / BatchOrders are DataEntities you created):

    var query = from BatchOrder
       in context.BatchOrders
       where BatchOrder.Sent == NoYes.No
       select BatchOrder;
    
    DataServiceCollection<BatchOrder> batchOrders = new DataServiceCollection<BatchOrder>(query);
    
    foreach (var batchOrder in batchOrders)
    {
       // do some processing on the batchOrder record here
       // update AX record
       batchOrder.Sent = NoYes.Yes;
      context.UpdateObject(batchOrder); } // send changes to the AX records back to AX context.SaveChanges(SaveChangesOptions.PostOnlySetProperties);

  • Suggested answer
    Sukrut Parab Profile Picture
    71,710 Moderator on at
    Look at the below code for how to use 'DataServiceCollection'

    https://stoneridgesoftware.com/working-with-the-odata-endpoint-in-dynamics-365-for-operations/


    DataServiceCollection<Customer> customersCollection = new DataServiceCollection<Customer>(context); customersCollection.Add(myCustomer); myCustomer.CustomerAccount = "US-X11111"; myCustomer.Name = "ABC Trees 111"; myCustomer.CustomerGroupId = "10"; myCustomer.SalesCurrencyCode = "USD"; myCustomer.CreditRating = "Excellent"; myCustomer.AddressCountryRegionId = "USA"; #region Create multiple customers Customer myCustomer2 = new Customer(); customersCollection.Add(myCustomer2); myCustomer2.CustomerAccount = "US-X22222"; myCustomer2.Name = "ABC Rains vb"; myCustomer2.CustomerGroupId = "10"; //myCustomer2.SalesCurrencyCode = "USD"; myCustomer2.CreditRating = "Excellent"; myCustomer2.AddressCountryRegionId = "USA"; #endregion


     

  • Community Member Profile Picture
    on at

    Hi justin,

    Do you have any idea to delete multiple records at a time(single call to delete), like above update example.

    foreach (string customerId in customersToDelete)

               {

                   Customer customer = dataServiceContext.CreateTrackedEntityInstance<Customer >();

                   customer = RetreiveCustomer (customerId );              

                   dataServiceContext.DeleteObject(customer );

               }

               dataServiceContext.SaveChanges(SaveChangesOptions.BatchWithSingleChangeset);

    Could you please help me if you have any idea about this.

    Thank you

  • swetha desai Profile Picture
    152 on at

    Hello Justin,

    Using the same code, i am unable to update salesorderheader entity. It gives me the error, no resources found while selecting for update.

    Any suggestions?

    Thanks,

    Swetha K Desai

  • Community Member Profile Picture
    on at

    I have not seen that specific error before, but one thing we have found that solved a few of our issues is that you must set the initial resources to pull from all the companies.  That isn't always the case, but try setting the "cross-company" parameter to "true", as shown in bold in the code below when you are first getting the resources.

    private Resources MakeResources()

            {

                string entityRootPath = appContract.resource + "/data";

                Uri oDataUri = new Uri(entityRootPath, UriKind.Absolute);

                var resources = new Resources(oDataUri);

     

                resources.SendingRequest2 += new

                EventHandler<SendingRequest2EventArgs>(delegate (object sender, SendingRequest2EventArgs e)

                {

                    e.RequestMessage.SetHeader(OAuthHeader,

                    authenticationHeader);

                });

     

                resources.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;

                };

     

                return resources;

            }

  • Community Member Profile Picture
    on at

    I know this is a few weeks old, but I'll do my best to respond...

    I am not sure how to delete as you are asking, but I assume you need to be deleting the record from "customersToDelete".  Calling dataServiceContext.DeleteObject(customer ); by itself isn't enough.  Make sure the "customersToDelete" is a DataServiceCollection object:

    (This is just an example, I have no idea what you are using for your query to get the customers)

    var query = from Customer

      in context.Customers

      where Customer.FieldToDelete == true

      select Customer;

    DataServiceCollection<Customer> customersToDelete = new DataServiceCollection<Customer>(query);

    The problem is, looping over a DataServiceCollection and trying to delete an element in that same collection may prove problematic.  I haven't tried it, but I could see how it may be a problem.

    Let me know if you were able to figure it out or not.

    Thanks,

    Justin

  • swetha desai Profile Picture
    152 on at

    Thanks Justin. This worked well for me :)

    I was using the crosscompany=true using the AddQueryFilter option on DataServiceQuery. But changed that to what you suggested, and got rid of the error.

    Regards,

    Swetha K Desai

  • Community Member Profile Picture
    on at

    Thanks Justin for your response,

    find solution, By using below code  we can delete multiple records at a time.

    StringBuilder sb = new StringBuilder();
                int count = 1;

                foreach (Customer customer in customers)
                {                
                    sb.Append(String.Format("customerId eq '{0}'", customer.cutomerId));
                    sb.Append(" and ");
                    sb.Append(String.Format("dataAreaId eq '{0}'", company));
                    if (count < customers.Count)
                        sb.Append(" or ");
                    count++;
                }
                var dynamicfilter = sb.ToString();
         
                var customerBatch = from entity in dataServiceContext.Customers.AddQueryOption("$filter", dynamicfilter)
                                 select entity;

               DataServiceCollection<Customer> customersToDelete = new DataServiceCollection<Customer>(customerBatch);
        
                foreach (var customer in customersToDelete)
                {
                    dataServiceContext.DeleteObject(customer);
                }
                dataServiceContext.SaveChanges(SaveChangesOptions.BatchWithSingleChangeset);

  • Community Member Profile Picture
    on at

    Hi Justin

    I am also trying to update the customer with the help of your query. But is is throwing this error.

    try
               {
                   var query = from Customer
                               in d365.Customers
                               where Customer.CustomerAccount == "004073"
                               where Customer.DataAreaId =="usrt"
                               select Customer;
                   DataServiceCollection<Customer> customersToDelete = new DataServiceCollection<Customer>(query);
                   foreach (var customer in customersToDelete)
                   {
                       d365.UpdateObject(customer);
                   }
                   d365.SaveChanges(SaveChangesOptions.BatchWithSingleChangeset);
               }
               catch (DataServiceRequestException e)
               {
                   // Console.WriteLine(string.Format("Customer {0} - Save Failed !"));
                   Console.WriteLine(e.Message);
               }        
    Microsoft.OData.Client.DataServiceQueryException
     HResult=0x80131509
     Message=An error occurred while processing this request.
     Source=Microsoft.OData.Client
     StackTrace:
      at Microsoft.OData.Client.DataServiceRequest.Execute[TElement](DataServiceContext context, QueryComponents queryComponents)
      at Microsoft.OData.Client.DataServiceQuery`1.Execute()
      at Microsoft.OData.Client.DataServiceCollection`1.InternalLoadCollection(IEnumerable`1 items)
      at Microsoft.OData.Client.DataServiceCollection`1.StartTracking(DataServiceContext context, IEnumerable`1 items, String entitySet, Func`2 entityChanged, Func`2 collectionChanged)
      at Microsoft.OData.Client.DataServiceCollection`1..ctor(DataServiceContext context, IEnumerable`1 items, TrackingMode trackingMode, String entitySetName, Func`2 entityChangedCallback, Func`2 collectionChangedCallback)
      at Microsoft.OData.Client.DataServiceCollection`1..ctor(IEnumerable`1 items)
      at ODataConsoleApplication.SimpleCRUDExamples.UpdateCRUD_Customer(Resources d365) Inner Exception 1:
    DataServiceClientException: NotFound

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