web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Unanswered

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

(0) ShareShare
ReportReport
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:-

I have the same question (0)
  • Kadir Kurt Profile Picture
    38 on at

    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.

  • Tushar Jadhav Profile Picture
    154 on at

    Didn't understand can please explain this.

  • Kadir Kurt Profile Picture
    38 on at

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

  • Linn Zaw Win Profile Picture
    3,407 on at

    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

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 171 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 83

#3
Jimmy Passeti Profile Picture

Jimmy Passeti 50 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans