RSS

Tag Archives: Plug-in Development

Use of Shared Variables in Plugin

Introduction

Shared Variables will be useful for sharing the data during complex Plug-in development by sharing the data between Plugins registered on both the pre and post events.

Any value stored in the Plug-in context of Pre-event will be available in the Post-event of the Plug-in. This way, we can avoid storing the values in a custom attribute. Certain data validation which needs to be done at post-event can be achieved by passing the value from Pre-event of the Plug-in. This also helps for performing business logic like updating related records asynchronously.

Using the Code

The below code snippet illustrates the usage of Shared Variable. This sample explains the data validation done at Post-event operation based on the value received from the Pre-event operation where a flag is updated with a value at the Pre-event level and sent to the Post-event through shared variable.

//Pre-Create (Entity Name) Plug-in
if (pluginContext.MessageName == "Create")
{
    if (pluginContext.InputParameters.ContainsKey("Target")
                             && 
        pluginContext.InputParameters["Target"] is Entity)
    {
        Entity targetEntity = context.InputParameters["Target"] as Entity;
        if (targetEntity != null)
        {
            bool setFlag = false;

            //Check data validation for attributes
            string country = targetEntity.Attributes["address1_country"];
            OptionSetValue industryCode = (OptionSetValue)target.Attributes["industrycode"];

            // Country is US and Industry Type is Accounting are Preferred
            if (country.ToLower() == "us" && industryCode.Value == 1)
            {
                setFlag = true;
            }
            
            pluginContext.SharedVariables.Add("flag", setFlag);
        }
    }
}

//Post-Create (Entity Name) Plug-in
if (pluginContext.MessageName == "Create")
{
    if (pluginContext.InputParameters.ContainsKey("Target")
                           && 
        pluginContext.InputParameters["Target"] is Entity)
    {
        Entity targetEntity = pluginContext.InputParameters["Target"] as Entity;
        if (targetEntity != null)
        {
            if (pluginContext.SharedVariables.ContainsKey("flag"))
            {
                bool recievedFlag = (bool)pluginContext.SharedVariables["flag"];
                if (recievedFlag)
                {
                    // Do whatever you want to do
                }
             }
         }
    }
}

This method lets you to pass data between Plug-ins without having to customize the system by creating hidden fields on entities.

Note: SharedVariables (collection of key/value pairs) property is available in the IPluginExecutionContext of the execution pipeline.

By Gitanjali
Senior Software Engineer @Team DynamicsCRM.
Mindfire Solutions

 

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

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

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

 
3 Comments

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

 

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

 
%d bloggers like this: