Tag Archives: QualifyLead Mesage

Get the Created Records When Lead is Qualified Using Plug-in


This blog explains how to register a plug-in in “QualifyLead” message and get the created record when lead is qualified.


Let in some requirement, when a lead is qualified then if you need some custom logic to work then in this scenario you can register you plug-in on the “QualifyLead” message and “Post Operation” Stage.


We can easily get the newly created account, contact and opportunity records and can do any required changes in them.

Codes that will be used inside the plug-in :

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

// Obtain the organization service reference.
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

// Getting the service from the Organisation Service.
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

// Get the qualified lead.
EntityReference leadid = (EntityReference)context.InputParameters["LeadId"];
Entity lead = service.Retrieve("lead", leadid.Id, new ColumnSet(true));

foreach (EntityReference created in (IEnumerable<object>)context.OutputParameters["CreatedEntities"])
    //Check if Account record is created when lead qualified.
    if (created.LogicalName == "account")
        Entity account = service.Retrieve("account", created.Id, new ColumnSet(true));
    //Check if Contact record is created when lead qualified.
    if (created.LogicalName == "contact")
        Entity contact = service.Retrieve("contact", created.Id, new ColumnSet(true));

    //Check if Opportunity record is created when lead qualified.
    if (created.LogicalName == "opportunity")
        Entity opportunity = service.Retrieve("opportunity", created.Id, new ColumnSet(true));

To update any of the created account, contact and opportunity we can use the retrieved data as per below :

Entity entAccount = new Entity("account");
entAccount["accountid"] = account.Attributes["accountid"];
entAccount["name"] = "Test Account Name";

By Abhisek Sriramka
Senior Software Engineer @Team DynamicsCRM.
Mindfire Solutions


Posted by on April 29, 2013 in CRM Plug-in


Tags: , , , , , , , , , , , , , , ,

%d bloggers like this: