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 :
Microsoft Dynamics CRM (Archived)

Override Qualify Lead

(0) ShareShare
ReportReport
Posted on by

Hi,

Can anyone see what I am doing wrong with the below code. I am trying to stop the automatic creation of the opportunity record when a user qualifies a lead. We have a custom opportunity entity that I will be creating in the future , but for now I just want to block the opportunity creation.

I have written the below as a pre operation plugin and have registed with step on QualifyLead of the lead entity.

It doesnt seem to go into my code at all and was wondering if anyone knew why.

I am on CRM 2013 SP1 Rollup2
Thanks in advance guys and gals

Andy

----------------

public class LeadQualification : Plugin

{

 ã€€

public LeadQualification()

: base(typeof(LeadQualification))

{

base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(20, "QualifyLead", "lead", new Action<LocalPluginContext>(ExecuteLeadQualification)));

}

protected void ExecuteLeadQualification(LocalPluginContext localContext)

{

if (localContext == null)

{

throw new ArgumentNullException("localContext");

}

IPluginExecutionContext context = (IPluginExecutionContext)

localContext.ServiceProvider.GetService(typeof(IPluginExecutionContext));

if (context.InputParameters.Contains("Target") &&

context.InputParameters["Target"] is Entity)

{

// Obtain the target entity from the input parameters.

Entity target = (Entity)context.InputParameters["Target"];

//</snippetFollowupPlugin2>

// Verify that the target entity represents an account.

// If not, this plug-in was not registered correctly.

if (target.LogicalName != "lead")

return;

try

{

context.InputParameters["CreateOpportunity"] = false;

 

 

}

catch (Exception ex)

{

throw new InvalidPluginExecutionException(ex.Message);

//EntityHelper.CreateLogOnCrm(localContext.OrganizationService, "An error occurred in comparing financial records.", ex, targetRef);

}

 

}

 

}

}

}

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Aileen Gusni Profile Picture
    44,524 on at

    Hi Andy,

    At a glance seems like your problem is the plugin cant get until this stage:

    if (context.InputParameters.Contains("Target") &&

    context.InputParameters["Target"] is Entity)

    {

    // Obtain the target entity from the input parameters.

    Entity target = (Entity)context.InputParameters["Target"];

    ========================================================

    Can you try to put this exception inside your code:

    try

    {

    context.InputParameters["CreateOpportunity"] = false; 

    }

    So will be:

    try

    {

    throw new InvalidPluginExecutionException("tested here");

    }

    Then try to qualify your lead whether you hit this or not.

    Andy, please also refer to this one:

    ashwaniashwin.wordpress.com/.../modify-lead-qualification-process-in-microsoft-dynamics-crm-2013

    I think it is because your target entity cant get the lead so it hit the "return" instead'

    So you need to define the variable lead like this:

    IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

               IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

               service = serviceFactory.CreateOrganizationService(context.UserId);

               // Get the qualified lead

               EntityReference leadid = (EntityReference)context.InputParameters["LeadId"];

               Entity lead = service.Retrieve(leadid.LogicalName, leadid.Id, new ColumnSet(true));

    Try this lead

    Instead of using yours:

    Entity target = (Entity)context.InputParameters["Target"];

    ==========================================================

    public class LeadQualification : Plugin

    {

    public LeadQualification()

    : base(typeof(LeadQualification))

    {

    base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(20, "QualifyLead", "lead", new Action<LocalPluginContext>(ExecuteLeadQualification)));

    }

    protected void ExecuteLeadQualification(LocalPluginContext localContext)

    {

    if (localContext == null)

    {

    throw new ArgumentNullException("localContext");

    }

    IPluginExecutionContext context = (IPluginExecutionContext)

    localContext.ServiceProvider.GetService(typeof(IPluginExecutionContext));

    if (context.InputParameters.Contains("LeadId") )

    {

    // Obtain the target entity from the input parameters.

    //Entity target = (Entity)context.InputParameters["Target"];

    //replace to:

    Entity lead = service.Retrieve(leadid.LogicalName, leadid.Id, new ColumnSet(true));

    //</snippetFollowupPlugin2>

    // Verify that the target entity represents an account.

    // If not, this plug-in was not registered correctly.

    if (lead.LogicalName != "lead")

    return;

    try

    {

    //expected it will hit this..

    context.InputParameters["CreateOpportunity"] = false;

    }

    catch (Exception ex)

    {

    throw new InvalidPluginExecutionException(ex.Message);

    //EntityHelper.CreateLogOnCrm(localContext.OrganizationService, "An error occurred in comparing financial records.", ex, targetRef);

    }

    }

    }

    }

    }

    Hope this helps.

    Thanks

  • Rajkumar Rajaraman Profile Picture
    on at

    Refer this blog, it explains deeply:

    ashwaniashwin.wordpress.com/.../modify-lead-qualification-process-in-microsoft-dynamics-crm-2013

  • Community Member Profile Picture
    on at

    Hiya, yeah thanks thats great. It was the target that was the issue so used your code as a guideline to do what I needed

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 > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans