0

I wrote the following plug-in for ms crm 2016 that's detect duplication.

    public void Execute(IServiceProvider serviceProvider)
    {
        var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
        var serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
        var service = (IOrganizationService)serviceFactory.CreateOrganizationService(context.UserId);

        if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
        {
            var entity = (Entity) context.InputParameters["Target"];
            var name = entity.GetAttributeValue<string>("name");

            var opportunityCompany = new QueryExpression
            {
                EntityName = entity.LogicalName,
                ColumnSet = new ColumnSet("name")
            };
            opportunityCompany.Criteria.AddCondition("name", ConditionOperator.Equal, name);

            var retrieveAccount = service.RetrieveMultiple(opportunityCompany);

            //If the retrieved Company Count Greater Than 1 , following Error Message Throw

            if (retrieveAccount.Entities.Count > 1)
            {
                throw new InvalidPluginExecutionException("Following Record with Same Name is Exists");
            }
        }
    }

This works fine but in case of duplicate i want to end record creation without exception and without message on page. How can i do this in plug-in? Some people talks to me that i can implement this in CodeActivity. How can i implement CodeActivity in ms crm plug-in?

Or rather I want to stop the plug-in so that the "user" did not know about it. How better do it?

user6408649
  • 1,227
  • 3
  • 16
  • 40

1 Answers1

2

There is no way to "prevent a record from being created without having the user know about it". The only supported way of preventing a record creation is if a synchronous plugin is registered on pre-operation and an exception is thrown. CRM rolls back the entire transaction preventing a save.

A better alternative would be to use JavaScript:

On Save of the record, query CRM for the matching records and Prevent Save of the record on the client side. This way the save would never even make it to the server side.


Ugly workaround (if for whatever reason you prefer not to use JavaScript):

Register the plugin on post-op or use a custom workflow activity and delete the record after the fact. Not ideal.

dynamicallyCRM
  • 2,980
  • 11
  • 16