Monday, August 6, 2012

Create a plugin in MS CRM, How to start a plugin in MS CRM?


Please find the following line of code to start a plugin either in MS CRM 4.0 or 2011.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using Microsoft.Xrm.Sdk;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Query;

namespace CRM2011Plugin
{
    public class vpscrmActivateQuote:IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
            {
                // obtain target entity from context
                Entity entity = context.InputParameters["Target"] as Entity;
                //check if the entity name is lead
                if (entity.LogicalName != "quote")
                { return; }
                else
                {
                    IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                    IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
                    if (entity.Attributes.Contains("statuscode"))
                    {
                        if (entity.GetAttributeValue<OptionSetValue>("statuscode").Value == 2)
                        {
                            //Retrieve data.
                            var trackingOptionObj = entity.GetAttributeValue<OptionSetValue>("new_trackingoption");
                            var accountId = entity.GetAttributeValue<Guid>("accountid");
                            var newCustomerStatus = entity.GetAttributeValue<OptionSetValue>("new_customerstatus").Value;
                            var current_syn_customerid = entity.GetAttributeValue<string>("new_customerid");
                            var current_syn_debtorclassid = entity.GetAttributeValue<EntityReference>("new_customerclass").Id.ToString();

                            //Add attribute
                            Guid contactId = Guid.Empty; //sepcify value
                            entity.Attributes.Add("primarycontactid", new EntityReference("contact", contactId));

                            updateDetailsinNet4DB(context, service, entity, "Yes", entity.GetAttributeValue<string>("name"), entity.GetAttributeValue<EntityReference>("customerid").Id.ToString());
                        }
                    }
                }
            }
        }
    }
}

No comments:

Split the String values with a special character in MS Flow to convert this into Array

 Many times we have a requirement to prepare the Mailing address for some of the documents, suppose there are Address Line1, Address Line2, ...