RSS

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

29 Apr

Introduction

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

Background

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.

Code

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";
service.Update(entAccount);

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

Advertisements
 
3 Comments

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

 

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

3 responses to “Get the Created Records When Lead is Qualified Using Plug-in

  1. mikael

    March 13, 2014 at 3:58 pm

    I copy/ paste your code and I’ve this error : The specified domain does not exist or cannot be contacted
    An unexpected error occurred from ISV code

     
    • DynamicsCRM@MindfireSolutions

      April 15, 2014 at 6:29 am

      Hey mikael,

      Thanks for reading our Blog. 🙂

      We have tested this with Plugin and it works fine. You must be doing something wrong.
      Please review what exactly you are doing and provide more information, if possible.

      Thanks,
      DynamicsCRM@MindfireSolutions

       
  2. Nitin Ramapure

    July 22, 2014 at 7:12 am

    Thanks a lot..
    This helped me to solve an issue.
    I had a plugin on create of opportunity which was not executing while opportunity was created by qualifying lead.

     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: