Skip to main content

Notifications

Announcements

No record found.

Service | Customer Service, Contact Center, Fie...
Suggested answer

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

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
    Naveen Ganeshe 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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans