Hi there,
I have a business logic to validate whether any contact available with same email address as in lead record. If yes, pop up a Business Process Error that there is a contact available with same email address. If not, I can let the lead get created in the system.
I'm able to do the validation for existing contact on creation of lead record from lead entity but is not letting me to create a new lead record when there is no matching found in contact.
I've registered the plugin on Post Operation of lead entity with the below code.
Also, I would need to do the same validation while data import.
public void Execute(IServiceProvider serviceProvider) { // Get Plugin Execution Context IPluginExecutionContext executionContext = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext)); // Get a reference to the Organization service. IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); //Get Currently User ID IOrganizationService service = factory.CreateOrganizationService(executionContext.UserId); //Extract the tracing service for use in debugging sandboxed plug-ins. ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService)); // try //{ Entity lead = (Entity)executionContext.InputParameters["Target"]; LeadCreation objLogic = new LeadCreation(); bool isDuplicateContactPresent = isDuplicateContactPresent = objLogic.IsDuplicateContactPresent(serviceProvider, tracingService, service, executionContext); if (isDuplicateContactPresent) { throw new InvalidPluginExecutionException("There is already a contact available with same email address"); } else { Entity leadRecord = new Entity("lead"); service.Create(leadRecord); } } } ////// To Create Contact when Lead gets created from D365 /// This class has one method /// public class LeadCreation { ////// To Check If there Duplicate Email ID in Contact /// PluginMessage.Create() /// string leadEmailId; public bool IsDuplicateContactPresent(IServiceProvider serviceProvider, ITracingService tracingService, IOrganizationService service, IPluginExecutionContext executionContext) { try { Entity entity = (Entity)executionContext.InputParameters[SMO_constants_plugin.EntityTargetName]; if (entity.Contains(SMO_constants_plugin.leadEmailAddress)) { leadEmailId = entity.GetAttributeValue(SMO_constants_plugin.leadEmailAddress); } if (entity.Contains(SMO_constants_plugin.leadEmailAddress)) { QueryExpression Contact = new QueryExpression { EntityName = SMO_constants_plugin.contactEntityName, ColumnSet = new ColumnSet(SMO_constants_plugin.contactEmailAddress) }; Contact.Criteria.AddCondition(SMO_constants_plugin.contactEmailAddress, ConditionOperator.Equal, leadEmailId); EntityCollection RetrieveContact = service.RetrieveMultiple(Contact); if (RetrieveContact.Entities.Count > 0) { return true; } else { return false; } } } catch (InvalidOperationException ex) { tracingService.Trace("IsDuplicateContactPresent", ex.Message); } return true; }
Can anyone please help me how this can be done here.
Thank you,
Leo