Dynamics 365





Monday, November 4, 2013

Close quote Plugin code in c#.net

Close quote Plugin code in c#.net

Below code to achieve logic 
  using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Crm.Sdk.Messages;
namespace CloseQuote
{
    public class PluginCloseQuote:IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            try
            {
                // Plugin to close the quote
                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity && context.Depth<2)
                {
                    Entity entity = context.InputParameters["Target"] as Entity;

                    IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                    IOrganizationService service = ((IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory))).CreateOrganizationService(new Guid?(context.UserId));


                    OrganizationServiceContext orgContext = new OrganizationServiceContext(service);
                    Entity postImageEntity = (context.PostEntityImages != null && context.PostEntityImages.Contains("PostImage")) ? context.PostEntityImages["PostImage"] : null;
                  
                    if (postImageEntity.GetAttributeValue<bool>("pmax_isclosequote"))
                    {
                     
                        if (((OptionSetValue)postImageEntity.Attributes["statecode"]).Value == 0)
                        {
                            //method to Activate quote
                            SetEntityStatus(service, entity.Id, "quote");

                        }
                       
                        //method to close quote
                        CloseQuoteRequest req = new CloseQuoteRequest();
                        Entity quoteClose = new Entity("quoteclose");
                        quoteClose.Attributes.Add("quoteid", new EntityReference("quote", entity.Id));
                        quoteClose.Attributes.Add("subject", "Customer was mean so we just closed it.");
                        req.QuoteClose = quoteClose;
                        req.RequestName = "CloseQuote";
                        OptionSetValue o = new OptionSetValue();
                        o.Value = 5;
                        req.Status = o;
                       
                        CloseQuoteResponse resp = (CloseQuoteResponse)service.Execute(req);
                    }
                    else if (postImageEntity.GetAttributeValue<bool>("pmax_isactivatequote"))
                    {
                        if (((OptionSetValue)postImageEntity.Attributes["statecode"]).Value == 0)
                        {
                            //method to Activate quote
                            SetEntityStatus(service, entity.Id, "quote");

                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new InvalidPluginExecutionException(
                   "An error occurred in the CloseQuote.PluginCloseQuote Plug-in" + ex.Message, ex);
            }
        }
        //activate the quote
        private void SetEntityStatus(IOrganizationService service, Guid recordGUID, string entityName)
        {

            SetStateRequest setState = new SetStateRequest();

            setState.EntityMoniker = new EntityReference();

            //Pass GUID of the record to be activated or Deactivated

            setState.EntityMoniker.Id = recordGUID;

            setState.EntityMoniker.Name = entityName;

            setState.EntityMoniker.LogicalName = entityName;

            //Setting ‘State’ i.e., (0 – Active ; 1 – InActive)

            setState.State = new OptionSetValue();

            setState.State.Value = 1;

            //Setting ‘Status’ i.e., (1 – Active ; 2 – InActive)

            setState.Status = new OptionSetValue();

            setState.Status.Value = 3;

            SetStateResponse setStateResponse = (SetStateResponse)service.Execute(setState);

        }
        private void SetEntityStatusActive(IOrganizationService service, Guid recordGUID, string entityName)
        {

            SetStateRequest setState = new SetStateRequest();

            setState.EntityMoniker = new EntityReference();

            //Pass GUID of the record to be activated or Deactivated

            setState.EntityMoniker.Id = recordGUID;

            setState.EntityMoniker.Name = entityName;

            setState.EntityMoniker.LogicalName = entityName;

            //Setting ‘State’ i.e., (0 – Active ; 1 – InActive)

            setState.State = new OptionSetValue();

            setState.State.Value = 3;

            //Setting ‘Status’ i.e., (1 – Active ; 2 – InActive)

            setState.Status = new OptionSetValue();

            setState.Status.Value = 5;

            SetStateResponse setStateResponse = (SetStateResponse)service.Execute(setState);

        }
        private SetStateRequest inactiveSuspect(Entity entSuspect)
        {
            //to set prospect record inactive
            //WinOpportunityRequest

           
            SetStateRequest setState = new SetStateRequest();

            setState.EntityMoniker = new EntityReference();

            //Pass GUID of the record to be activated or Deactivated

            setState.EntityMoniker.Id = entSuspect.Id;

            setState.EntityMoniker.Name = "quote";

            setState.EntityMoniker.LogicalName = entSuspect.LogicalName;

            //Setting ‘State’ i.e., (0 – Active ; 1 – InActive)

            setState.State = new OptionSetValue();

            setState.State.Value = 0;

            //Setting ‘Status’ i.e., (1 – Active ; 2 – InActive)

            setState.Status = new OptionSetValue();

            setState.Status.Value = 1;


            return setState;
            //
        }
    }
}