Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Unanswered

Hi Community, i am getting error while checking for the duplicate value form contact to lead.

Posted on by 154

i have written the code which is check test case:

Led to lead working.

lead to contact working

contact to contact also working 

but contact to lead not working.

i am sharing the code in that contact to contact and contact to lead code please help me for this: 

public class OnCreateContact : IPlugin
{

public void Execute(IServiceProvider serviceProvider)
{
try
{
ITracingService tracer = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = factory.CreateOrganizationService(context.UserId);

tracer.Trace("Execution started.");
tracer.Trace("Message Name: " + context.MessageName);
tracer.Trace("Stage: " + context.Stage);
tracer.Trace("Depth: " + context.Depth);
Entity target = null;
tracer.Trace("1");
string message_stage = context.MessageName + "_" + context.Stage;
switch (message_stage)
{
case "Create_20":
if (context.InputParameters.Contains("Target"))
{
target = context.InputParameters.Contains(Model.Common.TARGET) ? (Entity)context.InputParameters[Model.Common.TARGET] : null;
tracer.Trace("2");
CreateLead(service, tracer, target);
tracer.Trace("3");
return;
}
break;
default:
break;
}

}
catch (InvalidPluginExecutionException ex)
{
throw new InvalidPluginExecutionException(ex.Message);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}

private void CreateLead(IOrganizationService service, ITracingService tracer, Entity target)
{
tracer.Trace("4");

string Business_email = target.Contains(Model.Contact.ATTR_BUSINESSEMAIL) ? target.GetAttributeValue<string>(Model.Contact.ATTR_BUSINESSEMAIL) : string.Empty;
string FirstName = target.Contains(Model.Contact.ATTR_FIRSTNAME) ? target.GetAttributeValue<string>(Model.Contact.ATTR_FIRSTNAME) : string.Empty;
string LastName = target.Contains(Model.Contact.ATTR_LASTNAME) ? target.GetAttributeValue<string>(Model.Contact.ATTR_LASTNAME) : string.Empty;
EntityReference companyname = target.Contains(Model.Contact.ATTR_ACCOUNT) ? target.GetAttributeValue<EntityReference>(Model.Contact.ATTR_ACCOUNT) : null;

EntityCollection contact = GetContact(service, tracer, Business_email, FirstName, LastName,);
EntityCollection lead = GetLead(service, tracer, Business_email, FirstName, LastName,target.GetAttributeValue<EntityReference>(Model.Contact.ATTR_ACCOUNT).Name.ToString());

if (contact != null || lead!=null)
{
throw new InvalidPluginExecutionException("A record with the same values already exists. Please check your input and try again.");
}

}

//Retive the contact
private EntityCollection GetContact(IOrganizationService service, ITracingService tracer, string Business_email, string FirstName, string LastName, EntityReference companyname)
{
string Query = @"<fetch>
<entity name='contact'>
<attribute name='emailaddress1' />
<attribute name='firstname' />
<attribute name='lastname' />
<attribute name='parentcustomerid' />
<filter type='and'>
<condition attribute='firstname' operator='eq' value='" + FirstName + @"' />
<condition attribute='lastname' operator='eq' value='" + LastName + @"' />
<condition attribute='emailaddress1' operator='eq' value='" + Business_email + @"' />
<condition attribute='parentcustomerid' operator='eq' value='" + companyname.Id + @"' />
</filter>
</entity>
</fetch>";
EntityCollection result = service.RetrieveMultiple(new FetchExpression(Query));
if (result.Entities.Count > 0)
{
return result;
}
return null;

}

Retrive the lead
private EntityCollection GetLead(IOrganizationService service, ITracingService tracer, string Business_email, string FirstName, string LastName, string Companyname)
{
string Query = @"<fetch>" +
"<entity name='lead'>" +
"<attribute name='emailaddress1' />" +
"<attribute name='firstname' />" +
"<attribute name='lastname' />" +
"<attribute name='companyname' />" +
"<filter type='and'>" +
"<condition attribute='firstname' operator='eq' value='" + FirstName + "' />" +
"<condition attribute='lastname' operator='eq' value='" + LastName + "' />" +
"<condition attribute='emailaddress1' operator='eq' value='" + Business_email + "' />" +
"<condition attribute='companyname' operator='eq' value='" + Companyname + "' />" +
"</filter>" +
"</entity>" +
"</fetch>";
EntityCollection result = service.RetrieveMultiple(new FetchExpression(Query));
if (result.Entities.Count > 0)
{
return result;
}
return null;

}

}
}

Error:-

  • Linn Zaw Win Profile Picture
    Linn Zaw Win 3,405 on at
    RE: Hi Community, i am getting error while checking for the duplicate value form contact to lead.

    Since the plugin trace log shows the number "4", it went into CreateLead method and the error was thrown afterwards.

    You have to add more trace logs in the rest of the code find out which line could be the issue.

    Alternatively, you can debug your plugin with the plugin profiler.

    learn.microsoft.com/.../tutorial-debug-plug-in

  • Kadir Kurt Profile Picture
    Kadir Kurt 38 on at
    RE: Hi Community, i am getting error while checking for the duplicate value form contact to lead.

    You need to add more traces and find out in which object you are getting the error.

  • Tushar Jadhav Profile Picture
    Tushar Jadhav 154 on at
    RE: Hi Community, i am getting error while checking for the duplicate value form contact to lead.

    Didn't understand can please explain this.

  • Kadir Kurt Profile Picture
    Kadir Kurt 38 on at
    RE: Hi Community, i am getting error while checking for the duplicate value form contact to lead.

    Hi,

    Object reference not set to of on object is a general error. To go to the root cause of this error, you need to add more traces and find out exactly which name the error came up with. I see that the message block part continues in the screenshot. If you can add a detailed trace and share the whole, we can help.

    Example:

    string Business_email = target.Contains(Model.Contact.ATTR_BUSINESSEMAIL) ? target.GetAttributeValue<string>(Model.Contact.ATTR_BUSINESSEMAIL) : string.Empty;

    tracer.Trace("5");
    string FirstName = target.Contains(Model.Contact.ATTR_FIRSTNAME) ? target.GetAttributeValue<string>(Model.Contact.ATTR_FIRSTNAME) : string.Empty;

    tracer.Trace("6");
    string LastName = target.Contains(Model.Contact.ATTR_LASTNAME) ? target.GetAttributeValue<string>(Model.Contact.ATTR_LASTNAME) : string.Empty;

    tracer.Trace("7");
    EntityReference companyname = target.Contains(Model.Contact.ATTR_ACCOUNT) ? target.GetAttributeValue<EntityReference>(Model.Contact.ATTR_ACCOUNT) : null;
    tracer.Trace("8");
    EntityCollection contact = GetContact(service, tracer, Business_email, FirstName, LastName,);

    tracer.Trace("9");
    EntityCollection lead = GetLead(service, tracer, Business_email, FirstName, LastName,target.GetAttributeValue<EntityReference>(Model.Contact.ATTR_ACCOUNT).Name.ToString());

    tracer.Trace("10");

    When I get this error I did something like this.

    Entity ent=new Entity();    wrong

    Entity ent=new Entity("lead");  correct

    hope it helps

    If you find the answer as useful, please mark it as verified.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans