public void Execute(IServiceProvider serviceProvider) { ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService)); IPluginExecutionContext context = (IPluginExecutionContext) serviceProvider.GetService(typeof(IPluginExecutionContext)); tracingService.Trace("Entered in R"); if (context != null) { //tracingService.Trace(context) } if (context.Depth > 1) { tracingService.Trace("depth"); return; } //Entity entity = context.PostEntityImages["PostImage"]; if (context.PostEntityImages.Contains("PostImage") && context.PostEntityImages["PostImage"] is Entity) { Entity entity = (Entity)context.PostEntityImages["PostImage"]; if (entity.LogicalName != "poad_customaddress") { return; } IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); try { CrmDataHelper objHelper = new CrmDataHelper(); Guid accountId = Guid.Empty; Guid contactid = Guid.Empty; Guid addressId = Guid.Empty; int addressNumber = 0; int objectTypeCode = 0; bool blnupdateaddressnumber = false; Entity customAddressObject; if (entity.Contains(CustomAddressAttributes.poad_addressnumber) && entity[CustomAddressAttributes.poad_addressnumber] != null) { addressNumber = (int)entity[CustomAddressAttributes.poad_addressnumber]; } if (entity.Contains(CustomAddressAttributes.poad_customerid) && entity[CustomAddressAttributes.poad_customerid] != null) { EntityReference customer = ((EntityReference)entity[CustomAddressAttributes.poad_customerid]); if (customer.LogicalName == "contact") { objectTypeCode = 2; } else if (customer.LogicalName == "account") { objectTypeCode = 1; } addressId = objHelper.GetAddressId(service, addressNumber, customer.Id); if (addressNumber == 0) { addressNumber = objHelper.GetMaxAddressNumber(service, tracingService, customer.Id, objectTypeCode); blnupdateaddressnumber = true; } } //else if(entity.Contains(CustomAddressAttributes.poad_contactid) && entity[CustomAddressAttributes.poad_contactid] != null) //{ // objectTypeCode = 2; // contactid = ((EntityReference)entity[CustomAddressAttributes.poad_contactid]).Id; // addressId = objHelper.GetAddressId(service, addressNumber, contactid); // if (addressNumber == 0) // { // addressNumber = objHelper.GetMaxAddressNumber(service, tracingService, contactid, objectTypeCode); // blnupdateaddressnumber = true; // } //} tracingService.Trace("Object Type code" + objectTypeCode); tracingService.Trace("ADdress number::" + addressNumber); if (objectTypeCode != 1 && objectTypeCode != 2) { return; } if (addressNumber == 0) { return; } tracingService.Trace("message" + context.MessageName); if (context.MessageName == "Create" || context.MessageName == "Update") { if (addressId == Guid.Empty) { customAddressObject = CreateAddressObject(entity, objectTypeCode, addressNumber); service.Create(customAddressObject); } else { customAddressObject = CreateAddressObject(entity, objectTypeCode, addressNumber); customAddressObject.Id = addressId; service.Update(customAddressObject); } } if (context.MessageName == "Create" && blnupdateaddressnumber == true) { entity[CustomAddressAttributes.poad_addressnumber] = addressNumber; service.Update(entity); } } catch (Exception ex) { tracingService.Trace("HandleCustom OOB Address: {0}", ex.ToString()); throw; } //catch (FaultException<OrganizationServiceFault> ex) //{ // throw new InvalidPluginExecutionException("An error occurred in HandleCustomAddress.", ex); //} } }