I have a requirement where in a contact is associated to an office(account),firm(custom entity) via lookup. On contact entity, we have a field that stores the office,firm identification number. As soon as the contact records is created (or) office/firm identification numbers gets updated, we have plugin that fires and checks if the identification number exist then gets the office/firm guid and fills the lookup on contact entity. If the identification number doesn't exist, then it creates a new record on office/firm entity and gets the guid to fill contact lookup. Registered my plugin on both create and update messages.
My code works good when it is created via UI by user but fails when multiple contact records are loaded via SSIS. Plugin is succeeding for few and fails for others. Trace logs doesn't give much information except Generic SQL error.
Here is my code. Please suggest me if there is any mistake in the code.
try { // Get a reference to the Organization service. IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); if (entity.Attributes.Contains("st_firmidentificationnumber") || entity.Attributes.Contains("st_officeidentificationnumber")) { if (entity.Attributes.Contains("st_firmidentificationnumber")) { QueryExpression query = new QueryExpression { EntityName = "st_firm", ColumnSet = new ColumnSet("st_firmname") }; query.Criteria.AddCondition("st_firmidentificationnumber", ConditionOperator.Equal, entity.Attributes["st_firmidentificationnumber"]); EntityCollection result = service.RetrieveMultiple(query); tracingService.Trace("result count : {0}", result.Entities.Count.ToString()); if (result.Entities.Count == 1) { tracingService.Trace("matching firm found"); entity["st_firmid"] = new EntityReference("st_firm", result.Entities[0].Id); } else if (result.Entities.Count == 0) { tracingService.Trace("matching firm not found"); Entity Firm = new Entity("st_firm"); Firm["st_firmidentificationnumber"] = entity.Attributes["st_firmidentificationnumber"].ToString(); Guid Firmid = service.Create(Firm); entity["st_firmid"] = new EntityReference("st_firm", Firmid); } } if (entity.Attributes.Contains("st_officeidentificationnumber")) { QueryExpression query = new QueryExpression { EntityName = "account", ColumnSet = new ColumnSet("name") }; query.Criteria.AddCondition("st_officeidentificationnumber", ConditionOperator.Equal, entity.Attributes["st_officeidentificationnumber"]); EntityCollection result = service.RetrieveMultiple(query); tracingService.Trace("result count : {0}", result.Entities.Count.ToString()); if (result.Entities.Count == 1) { tracingService.Trace("matching office found"); entity["parentcustomerid"] = new EntityReference("account", result.Entities[0].Id); } else if (result.Entities.Count == 0) { tracingService.Trace("matching office not found"); Entity Office = new Entity("account"); Office["st_officeidentificationnumber"] = entity.Attributes["st_officeidentificationnumber"].ToString(); Guid Officeid = service.Create(Office); entity["parentcustomerid"] = new EntityReference("account", Officeid); } } service.Update(entity); tracingService.Trace("Update Complete"); } }
Here is the trace log :
Error Message: Unhandled Exception: Microsoft.Xrm.Sdk.InvalidPluginExecutionException: An error occured in PostCrdNumberUpdateOfficeOrFirmDetails plugin. at STCRM.Plugins.PostCrdNumberUpdateOfficeOrFirmDetails.Execute(IServiceProvider serviceProvider) at Microsoft.Crm.Asynchronous.EventOperation.InvokePlugin(AsyncExecutionContext context, IPlugin pluginInstance) Inner Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=8.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: Generic SQL error.Detail: <OrganizationServiceFault xmlns:i="www.w3.org/.../XMLSchema-instance" xmlns="schemas.microsoft.com/.../Contracts"> <ErrorCode>-2147204784</ErrorCode> <ErrorDetails xmlns:d2p1="schemas.datacontract.org/.../System.Collections.Generic" /> <Message>Generic SQL error.</Message> <Timestamp>2018-10-16T16:26:13.8141024Z</Timestamp> <ExceptionRetriable>false</ExceptionRetriable> <ExceptionSource i:nil="true" /> <InnerFault> <ErrorCode>-2147204784</ErrorCode> <ErrorDetails xmlns:d3p1="schemas.datacontract.org/.../System.Collections.Generic" /> <Message>Generic SQL error.</Message> <Timestamp>2018-10-16T16:26:13.8141024Z</Timestamp> <ExceptionRetriable>false</ExceptionRetriable> <ExceptionSource i:nil="true" /> <InnerFault> <ErrorCode>-2147204784</ErrorCode> <ErrorDetails xmlns:d4p1="schemas.datacontract.org/.../System.Collections.Generic" /> <Message>Generic SQL error.</Message> <Timestamp>2018-10-16T16:26:13.8141024Z</Timestamp> <ExceptionRetriable>false</ExceptionRetriable> <ExceptionSource i:nil="true" /> <InnerFault> <ErrorCode>-2147220970</ErrorCode> <ErrorDetails xmlns:d5p1="schemas.datacontract.org/.../System.Collections.Generic" /> <Message>System.Data.SqlClient.SqlException: Microsoft Dynamics CRM has experienced an error. Reference number for administrators or support: #B364B63B</Message> <Timestamp>2018-10-16T16:26:13.8141024Z</Timestamp> <ExceptionRetriable>false</ExceptionRetriable> <ExceptionSource i:nil="true" /> <InnerFault i:nil="true" /> <OriginalException i:nil="true" /> <TraceText i:nil="true" /> </InnerFault> <OriginalException i:nil="true" /> <TraceText i:nil="true" /> </InnerFault> <OriginalException i:nil="true" /> <TraceText i:nil="true" /> </InnerFault> <OriginalException i:nil="true" /> <TraceText i:nil="true" /> </OrganizationServiceFault> at Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.Update(Entity entity, CorrelationToken correlationToken, CallerOriginToken callerOriginToken, WebServiceType serviceType, Boolean checkAdminMode, Boolean checkForOptimisticConcurrency, Dictionary`2 optionalParameters) at Microsoft.Crm.Extensibility.InprocessServiceProxy.UpdateCore(Entity entity)
Thanks..
*This post is locked for comments