Qualify Lead without Opportunity in Microsoft Dynamics CRM using C#

 

Old Qualify Lead Dialog in CRM 2011

We had a dialog box in Microsoft CRM 2011 which we used to create Account, Contact and Opportunity in Lead Qualification process.

 

Qualify Lead Process in CRM 2011

 

But this dialog box is deprecated, and we do not have this dialog box in Dynamic 365 Sales anymore.

Qualify Lead in Dynamics 365 Sales

Now inside the lead form in CRM there are two fields named as “Existing Contact” and “Existing Account”, and we use these fields to add related Contact or Account.

 

Dynamics 365 Sales Qualify Lead Process

 

Adhoc Lead Qualify Process

This is very useful design to optimize flow based on business requirements. If a record already exists, system uses it to associate qualified lead rather than creating a new one. However, it depends on how you implement it and what the business requirements are. Sometimes we don’t want to create new opportunity as part of the lead qualification process. In addition, we also need lead qualification without account or contact creation at times. Moreover, at times in case the implementation is using xRM as a platform, business requirements might govern creation of a completely new entity record on lead qualification (not covered). Therefore, to tackle the above-mentioned problem, we are giving a solution using a plugin and JS for Dynamics 365 that would help users qualify lead without creating opportunity, account or contact.

Plugin Code

Plugin code mentioned below would not create opportunity, account or contact:

 

public void Execute(IServiceProvider serviceProvider)
{

IPluginExecutionContext context =

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

IOrganizationServiceFactory serviceFactory =

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

service = serviceFactory.CreateOrganizationService(context.UserId);

 

if (context.MessageName != "QualifyLead") return;

 

OptionSetValue LeadStatecode = new OptionSetValue();

OptionSetValue LeadStatuscode = new OptionSetValue();

EntityReference LeadRef = null;

try

{

// Get the qualified lead

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

 

if (LeadRef.LogicalName != "lead") { return; }

 

ColumnSet attributes = new ColumnSet(new string[] { "statecode", "statuscode" });

Entity lead = service.Retrieve(LeadRef.LogicalName, LeadRef.Id, attributes);

 

LeadStatecode = Lead.GetAttributeValue<OptionSetValue>("statecode");

LeadStatuscode = Lead.GetAttributeValue<OptionSetValue>("statuscode");

 

if (LeadStatecode.Value == 0 && LeadStatuscode.Value == 1)

{

context.InputParameters["CreateOpportunity"] = false;

context.InputParameters["CreateAccount"] = false;

context.InputParameters["CreateContact"] = false;

}

}

catch (Exception ex)

{

throw new InvalidPluginExecutionException("An error occurred in the Plugin." + ex, ex);

}

}

 

Register this plugin at ‘QualifyLead’ Message at ‘Pre-Operation’.

Above code works fine, however the lead form freezes when we trigger it from the Qualify Lead Button. So, we need to refresh the page after performing this operation. It means we apply below JS on “onload” event of lead Form.

 

JavaScript Code

For the solution, I wrote a webresource of type JScript with the following code.

var entityname = Xrm.Page.data.entity.getEntityName();

var entityid =Xrm.Page.data.entity.getId();

Xrm.Utility.openEntityForm(entityname,entityid);function refresh(Context) {

var SaveMode, SaveEventVal;

 

// Change the Save Event Value as per required Save Event

SaveEventVal = 16;

 

if (Context != null && Context.getEventArgs() != null) {

SaveMode = Context.getEventArgs().getSaveMode();

 

//On Lead Qualify button click

if (SaveMode == SaveEventVal) {

setTimeout(function () {

var entityname = Xrm.Page.data.entity.getEntityName();

var entityid =Xrm.Page.data.entity.getId();

Xrm.Utility.openEntityForm(entityname,entityid);

}, 1);

}

}

}

 

This will solve the above-mentioned issue and all-together they work fine in the background.

Hope this will be helpful.

Thanks!!!

 

Conclusion

So, we can avoid creation of the records while performing the Lead qualifying process in Dynamics 365 by simply registering a plugin at the Qualify message and applying a JS on the load of Lead Page.

2 thoughts on “Qualify Lead without Opportunity in Microsoft Dynamics CRM using C#”

  1. I’m trying to implement your solution in our CRM. First, I create the plugin on the Lead entity on the Qualify message but the plugin was not fired, so I change the configuration to the update message and the result was good; In my case I need to create only a contact and an account without the opportunity. Server side no problem, but the client side JavaScript code does work for me or I did not understood haw I must use it. You said that I need to put the JavaScript code on the Onload of the Lead. I did it but the result was that the form keep reloading all the time in a loop!!! without opening the contact form.

    Thanks for you help

  2. @HEDI

    Can you debug the value of SaveMode variable as I think reloading issue occurs when the check failed related to SaveMode.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.