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