Quantcast
Channel: Microsoft Dynamics CRM Forum - Recent Threads
Viewing all articles
Browse latest Browse all 82002

Plugin not triggering during data import

$
0
0

We have one of our plugins on Create of Contact. however during the import of the contact records we are not supposed to allow duplicate records into CRM. we are handling this scenario through plugin code (Pre validation)   We had the Depth (as 1) check in our plugin code to check for infinite loop.

During Manual contact creation the plugin is triggering and throwing exception as expected.

After some debugging we realized that Depth value is 2 instead of 1 during import, Hence duplicates are getting created. as the plugin logic is not triggering. please find the below code and give your valuable suggestions.

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DuplicateDetection
{
public class DetectDuplicates : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
IOrganizationServiceFactory servicefactory =
(IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = servicefactory.CreateOrganizationService(context.UserId);

#region code
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
Entity entity = (Entity)context.InputParameters["Target"];
tracingService.Trace(context.Depth.ToString());
if (entity.LogicalName != "contact")
{
return;
}
if (context.Depth > 1)
{
return;
}
try
{
tracingService.Trace("1");

//Logic
}
catch (Exception ex)
{
throw (ex);
}
}
#endregion
}

}
}


Viewing all articles
Browse latest Browse all 82002

Latest Images

Trending Articles



Latest Images