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)

Plug-In looping issue

(0) ShareShare
ReportReport
Posted on by

Hello,

I am trying to write my first plug-in for CRM 2015. The basic idea is to qualify a lead without creating a contact. I am using the SDK example "WorkingWithLeads" but when I get to the IOrganizationService.execute it restarts my plug in. I feel like I am missing some basic knowledge on how to submit/close out my plug in. Below is my code.

using System;
using System.ServiceModel;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Query;
using Plugins.ProxyClasses;
using Plugins.SDKClasses;

public class LeadQualify : IPlugin
{
    public void Execute(IServiceProvider serviceProvider)
    {
        //throw new InvalidPluginExecutionException()
        //{
        //    //trest
        //};

        // Extract the tracing service for use in debugging sandboxed plug-ins.
        // If you are not registering the plug-in in the sandbox, then you do
        // not have to add any tracing service related code.
        ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

        // Obtain the execution context from the service provider.
        IPluginExecutionContext context = (IPluginExecutionContext)
            serviceProvider.GetService(typeof(IPluginExecutionContext));

        // The InputParameters collection contains all the data passed in the message request.
        if (context.InputParameters.Contains("LeadId") &&
            context.InputParameters["LeadId"] is EntityReference)
        {
            // Obtain the target entity from the input parameters.
            EntityReference entityRef = (EntityReference)context.InputParameters["LeadId"];

            // Verify that the target entity represents an entity type you are expecting. 
            // For example, an account. If not, the plug-in was not registered correctly.
            if (entityRef.LogicalName != "lead")
                return;

            // Obtain the organization service reference which you will need for
            // web service calls.
            IOrganizationServiceFactory serviceFactory =
                (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
            try
            {
                // Plug-in business logic goes here.

                //QUalifyLead

                var qualifyIntoAccountContactReq = new QualifyLeadRequest
                {
                    CreateAccount = true,
                    CreateContact = false,
                    CreateOpportunity = true,
                    LeadId = new EntityReference(entityRef.LogicalName, entityRef.Id),
                    Status = new OptionSetValue((int)lead_statuscode.Qualified)
                };

                var qualifyIntoAccountContactRes = (QualifyLeadResponse)service.Execute(qualifyIntoAccountContactReq);

            }
            catch (FaultException<OrganizationServiceFault> ex)
            {
                throw new InvalidPluginExecutionException("An error occurred in LeadQualify.", ex);
            }

            catch (Exception ex)
            {
                tracingService.Trace("MyPlugin: {0}", ex.ToString());
                throw;
            }
        }
    }
}


*This post is locked for comments

I have the same question (0)
  • Shantnu Sharma Profile Picture
    655 on at

    Which event is it registered on? Also, System creates a contact only if there is no value supplied to Company name field on the Lead. I do not see any issue with the code while reading it; not executed though. Are you trying to over ride system functionality?

  • Verified answer
    Daniel Wikell Profile Picture
    2,360 on at

    If your plugin is already registered on the Lead Qualify event, then you are indeed creating an infinite loop since you are making a new lead qualify request with your "qualifyIntoAccountContactReq" which will then trigger the event that triggers the plugin again.

    What you really want to do is change the parameters of the current request instead of creating and executing a completely new request. If you change the context input parameters and set the "create contact"-attribute to false, this change will then be passed on to core operation (which seems to be what you want).

  • Community Member Profile Picture
    on at

    I am registered synchronous, pre-operation in the QualifyLead message. Yes I am trying to override default behavior of Qualifying a lead to never create a contact.

  • Community Member Profile Picture
    on at

    Daniel This is exactly what I am looking for. I will try it out. So I don't need to return anything in the plugin, just modify the correct context property and let it flow through the end of the plug in? Seems weird that i am not explicitly passing the modified context to something. :)

  • rcis Profile Picture
    5 on at

    Having a similar issue with CRM 2015.  
     Firing the plugin on the 'QualifyLead' event, and I've set the InputParameters values for CreateAccount/CreateContact, and CreateOpportunity all to false.

    when I execute  the QualifyLeadRequest, it sets up an infinite loop.

    Has anyone worked through this?

    pertinent code is below:

     var query = new QueryExpression("organization");
     query.ColumnSet = new ColumnSet("basecurrencyid");
     var result = _service.RetrieveMultiple(query);
     var currencyId = (EntityReference)result.Entities[0]["basecurrencyid"];
    
     var qualifyReq = new QualifyLeadRequest             
    {
    CreateOpportunity = true, OpportunityCurrencyId=currencyId, OpportunityCustomerId=new EntityReference(Account.EntityLogicalName,_oppAcct.Id), LeadId = new EntityReference(Lead.EntityLogicalName,cLead.Id), Status = new OptionSetValue((int)LeadState.Qualified) }; var qualifyResponse = (QualifyLeadResponse)_service.Execute(qualifyReq); foreach (var entity in qualifyResponse.CreatedEntities) { //we're only creating an opportunity, so that's all we're concerned with here. if (entity.LogicalName == Opportunity.EntityLogicalName) {
    //update a lookup field on the opportunity we just created - that's the goal. ColumnSet csNewOpp = new ColumnSet(new string[]{"opportunityid","hcc_location"}); Entity _cOppty = _service.Retrieve("opportunity", entity.Id, csNewOpp); _cOppty["hcc_location"] = new EntityReference("hcc_location", _oppLoc.Id); _service.Update(_cOppty); } }

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