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,...
Answered

How to add plugin on lead qualify and apply a condition if the opportunity contains record with regarding_lead_id equal to a current lead id then connect it with it else created a new opportunity.

(0) ShareShare
ReportReport
Posted on by 185

Hi everyone, I am facing an issue, whenever I qualify a lead it creates an opportunity for that lead if I reactivate that lead and qualify it again it is creating another opportunity for that lead.
How to add plugin on lead qualify and apply a condition if the opportunity contains record with regarding_lead_id equal to a current lead id then connect it with it else created a new opportunity.

I have the same question (0)
  • Verified answer
    Kumar Viju Profile Picture
    1,026 on at

    Logic in Plugin:

    1. Check If existing active opp with the lead 

    2. If opp found then stop the qualified message to create Opp.

    Code Flow:

    1. Query D365 with FetchXml created using advance find.(https://docs.microsoft.com/en-us/powerapps/developer/common-data-service/use-fetchxml-construct-query) to get the result.

    2. Set the CreateOpportunity Parameter as False in the LeadQualification Message.

    Note:

    - Assumption; You are registering the plugin message on Lead Qualify

    - I suspect that you may need to even stop the creation of Account and Contact, as the entire operation of execution will be prevented.

    - My experience has been that you need to pass all the parameters(CreateAccount,CreateContact ,CreateOpportunity) for the LeadQulification Action to Work properly.

    Ex:
    var qualifyIntoAccountContactReq = new QualifyLeadRequest
    {
    CreateAccount = false,
    CreateContact = false,
    CreateOpportunity = false
    };
    var qualifyIntoAccountContactRes = (QualifyLeadResponse)svc.Execute(qualifyIntoAccountContactReq);

  • Suggested answer
    Jishnu Thapa  Profile Picture
    185 on at

    this code helps me to solve my problem.      

    I added this on pre-operation on QualifyLead Message

    public void Execute(IServiceProvider serviceProvider)

           {

               // Obtain the execution context from the service provider.

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

               if (context.MessageName != "QualifyLead")

                   return;

               //Get a reference to the Organization service.

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

               IOrganizationService service = factory.CreateOrganizationService(context.UserId);

               //Extract the tracing service for use in debugging sandboxed plug-ins

               ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

               tracingService.Trace("Input parameters before:");

               foreach (var item in context.InputParameters)

               {

                   tracingService.Trace("{0}: {1}", item.Key, item.Value);

               }

               //Modify the below input parameters to suit your requirements.

               //In this example, only a Contact record will be created

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

               var id = leadid.Id.ToString();

               RetrieveMultipleRequest rmreq = new RetrieveMultipleRequest();

               RetrieveMultipleResponse rmresp = new RetrieveMultipleResponse();

               QueryExpression query = new QueryExpression { EntityName = "opportunity", ColumnSet = new ColumnSet("name", "originatingleadid") };

               query.Criteria.AddCondition("originatingleadid", ConditionOperator.Equal, id);

               query.TopCount = 1;

               rmreq.Query = query;

               rmresp = (RetrieveMultipleResponse)service.Execute(rmreq);

               context.InputParameters["CreateContact"] = false;

               context.InputParameters["CreateAccount"] = false;

               if(rmresp.EntityCollection.Entities.Count>0)

               context.InputParameters["CreateOpportunity"] = false;

               else

                   context.InputParameters["CreateOpportunity"] = true;

               tracingService.Trace("Input parameters after:");

               foreach (var item in context.InputParameters)

               {

                   tracingService.Trace("{0}: {1}", item.Key, item.Value);

               }

           }

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 108 Super User 2025 Season 2

#2
Jimmy Passeti Profile Picture

Jimmy Passeti 50 Most Valuable Professional

#3
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 49 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans