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 :
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,
                                });

                            }
                        }
                    }

I have the same question (0)
  • Suggested answer
    Naveen Ganeshe Profile Picture
    3,397 User Group Leader on at

    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

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 > Service | Customer Service, Contact Center, Field Service, Guides

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 61 Super User 2025 Season 2

#2
Siv Sagar Profile Picture

Siv Sagar 52 Super User 2025 Season 2

#3
Daniyal Khaleel Profile Picture

Daniyal Khaleel 42 Most Valuable Professional

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans