Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Service | Customer Service, Contact Center, Fie...
Suggested answer

How to update only the changed values of an entity in Dynamics 365 c# using Linq

(0) ShareShare
ReportReport
Posted on by 70

I am new to CRM development, and I am hoping to get help. I am using the early bound service context generated by CRMSvcUtil and Linq to write queries and update records.

I am doing this in a Batch service job and not a plugin, I need find a way to update only the fields/attributes that changes and not all the fields on the service.Udpate method. How can I do this, please see my code below. I basically get a list of details from a service and then update Dynamics, this job runs daily to check if data is new if its new it should update just that field and not everything. Please see code below.

//Loop through each client and search for record in OCM service
                foreach (var a in DESCClient)
                {
                    string dt_desccdoe = a.dt_DESCCode;
                    string phoenixClientName = a.Name;

                    var phoenixConfig = ConfigManager.GetPhoenixConfig(serviceContext);
                    var ocmConfig = ConfigManager.GetOcmConfig(serviceContext);

                    using (var ocmManager = new Integration.Ocm.OcmManager(new Phoenix.Core.Factories.SqlConnectionFactory(phoenixConfig.StagingDbConnectionString), ocmConfig))
                    {
                        Log.InfoFormat("Desc Code is "   dt_desccdoe);
                        ocmClientSearch = ocmManager.SearchOcmClients(phoenixClientName, numberofPages, defaultPageNum);

                        if (ocmClientSearch.OcmClient.Count > 0)
                        {
                            //Get the Desc Code using the OCM search endpoint as the CRM desc codes fail on some records.
                            var ocmDescCode = ocmClientSearch.entity1[0].DGMFID;
                            ocmDetails = ocmManager.GetOcmClientDetail(0, ocmDescCode);

                            if (ocmDetails != null)
                            {
                                //Update the Client entity with the OCM Desc details
                                serviceContext.Update(new Account
                                {
                                    AccountId = a.AccountId,
                                    dt_Designation = ocmDetails.IndependenceInformation?.FirstOrDefault().Designation,
                                    dt_DesignationDescription = ocmDetails.IndependenceInformation?.FirstOrDefault().DesignationDescription,
                                    dt_DesignationRuleSet = ocmDetails.IndependenceInformation?.FirstOrDefault().DesignationRuleSet,
                                    dt_DesignationType = ocmDetails.IndependenceInformation?.FirstOrDefault().DesignationType,
                                });

                            }
                        }
                    }

  • Suggested answer
    Naveen Ganeshe Profile Picture
    3,393 User Group Leader on at
    RE: How to update only the changed values of an entity in Dynamics 365 c# using Linq

    Hi Papi,

    Can you try this one:

    foreach (var a in DESCClient)
    {
        // Retrieve the existing record from Dynamics CRM
        var existingAccount = serviceContext.AccountSet.Where(x => x.AccountId == a.AccountId).FirstOrDefault();
    
        if (existingAccount != null)
        {
                       string dt_desccdoe = a.dt_DESCCode;
                        string phoenixClientName = a.Name;
    
                        var phoenixConfig = ConfigManager.GetPhoenixConfig(serviceContext);
                        var ocmConfig = ConfigManager.GetOcmConfig(serviceContext);
    
                        using (var ocmManager = new Integration.Ocm.OcmManager(new Phoenix.Core.Factories.SqlConnectionFactory(phoenixConfig.StagingDbConnectionString), ocmConfig))
                        {
                            Log.InfoFormat("Desc Code is "   dt_desccdoe);
                            ocmClientSearch = ocmManager.SearchOcmClients(phoenixClientName, numberofPages, defaultPageNum);
    
        if (ocmClientSearch.OcmClient.Count > 0)
            {
           //Get the Desc Code using the OCM search endpoint as the CRM desc codes fail on some records.
             var ocmDescCode = ocmClientSearch.entity1[0].DGMFID;
             ocmDetails = ocmManager.GetOcmClientDetail(0, ocmDescCode);
    
            // Compare the fields with the new data
            if (existingAccount.dt_Designation != ocmDetails.IndependenceInformation?.FirstOrDefault().Designation)
                existingAccount.dt_Designation = ocmDetails.IndependenceInformation?.FirstOrDefault().Designation;
    
            if (existingAccount.dt_DesignationDescription != ocmDetails.IndependenceInformation?.FirstOrDefault().DesignationDescription)
                existingAccount.dt_DesignationDescription = ocmDetails.IndependenceInformation?.FirstOrDefault().DesignationDescription;
    
            if (existingAccount.dt_DesignationRuleSet != ocmDetails.IndependenceInformation?.FirstOrDefault().DesignationRuleSet)
                existingAccount.dt_DesignationRuleSet = ocmDetails.IndependenceInformation?.FirstOrDefault().DesignationRuleSet;
    
            if (existingAccount.dt_DesignationType != ocmDetails.IndependenceInformation?.FirstOrDefault().DesignationType)
                existingAccount.dt_DesignationType = ocmDetails.IndependenceInformation?.FirstOrDefault().DesignationType;
    
            // Update the record in Dynamics CRM
            serviceContext.Update(existingAccount);
            }
          }
        }
    }

    Please take a look at the documentation as well: https://learn.microsoft.com/en-us/power-apps/developer/data-platform/org-service/linq-query-examples 

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 293,245 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,925 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans