locked
Fire plugin when close as won opportunity RRS feed

  • Question

  • Hi,

    I am trying to fire a plugin when the opportunity closed as won (crm2011 online instance), I need to generate a task.  I tried several ways but i am not able to achieve.

    Method 1: Message: Win, Event: Post Operation, Primary Entity:opportunity

    Problem: I wrote the following code. Plugin not firing.

    Code:

    IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
                    ITracingService trace = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
                    trace.Trace("Started");
                  if (context.MessageName != "Win") { return; }
                  if (!(context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)) { return; }
                    
                    Entity opp =context.InputParameters["Target"]  as Entity ;
                        IOrganizationServiceFactory serviceFactory =              (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                        IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
                        Entity task = new Entity("task");
                        task["subject"] = "Followup Task";
                        task["description"] = "From Plugin";
                       task["regardingobjectid"] = new EntityReference("opportunity", opp.Id );
                         service.Create(task);

    Method 2: Message: Update, Event: Post, Primary Entity:opportunity, Post Entity Image

    Problem: Plugin is firing on each update. Either you close as won or re-open the opportunity, everything plugin is firing and creating two times tasks. I don't know why it's creating two times tasks though i am checking the coditions statecode and statuscode

    Code:

        IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
        ITracingService trace = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
        trace.Trace("Started");
        if ((context.MessageName != "Update")  && (context.PrimaryEntityName != "opportunity")) { return; }
                        
               if (!(context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)) { return; }
                    
                    Entity opp =context.InputParameters["Target"]  as Entity ;
                    Entity Pos = context.PostEntityImages["postImg"];
                    var state= (OptionSetValue)Pos.Attributes["statecode"];
                    var status = (OptionSetValue)Pos.Attributes["statuscode"];
                    if  ((state.Value ==1) && (status.Value  == 3))
                    {
                        IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                        IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
                        Entity task = new Entity("task");
                        task["subject"] = "Followup Task";
                        task["description"] = "From Plugin";
                        task["regardingobjectid"] = new EntityReference("opportunity", opp.Id );
                        service.Create(task);
                    }

    Method 3: Message: SetState, SetDynamic, Event: Post, Primary Entity:opportunity

    Problem: I registered the above messages. Plugin is not giving any error. But task is not creating.

        IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
                    ITracingService trace = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
                    trace.Trace("Started");
                   if ((context.MessageName != "SetState" && context.MessageName != "SetStateDynamicEntity") || (context.PrimaryEntityName != "opportunity")) { return; }
        
                    EntityReference opp = (EntityReference)context.InputParameters["EntityMoniker"];                         
                    IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                    IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
                    var state = (OptionSetValue)context.InputParameters["State"];
                    var status = (OptionSetValue)context.InputParameters["Status"];
                    if  ((state.Value ==1) && (status.Value  == 3))
                    {
                     Entity task = new Entity("task");
                     task["subject"] = "Followup Task";
                     task["description"] = "From Plugin";
                     task["regardingobjectid"] = new EntityReference("opportunity", opp.Id );
                     service.Create(task);
                    }

    Kindly let me know the plugin behaviour in each scenario and why it's not firing and what is the correct approach to achieve the my task.

    Regards,

    Monday, February 18, 2013 2:06 PM

Answers

  • Hello,

    you should be good with option 1, just make changes to your code like below

    IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
                    ITracingService trace = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
                    trace.Trace("Started");
                  if (context.MessageName != "Win") { return; }
                  if (!(context.InputParameters.Contains("OpportunityClose") && context.InputParameters["OpportunityClose"] is Entity)) { return; }
                    
                    Entity opp =context.InputParameters["OpportunityClose"]  as Entity ;
                        IOrganizationServiceFactory serviceFactory =              (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                        IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
                        Entity task = new Entity("task");
                        task["subject"] = "Followup Task";
                        task["description"] = "From Plugin";
                       task["regardingobjectid"] = new EntityReference("opportunity", opp.Id );
                         service.Create(task);

    you can refer :http://www.patrickverbeeten.com/Blog/2008/01/25/CRM-40-Plug-in-message-input-parameters for list of parameters


    Contact Me
    Follow me on Twitter
    My Facebook Page
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    • Proposed as answer by HIMBAPModerator Monday, February 18, 2013 2:37 PM
    • Marked as answer by crmpnk1 Tuesday, February 19, 2013 12:11 PM
    Monday, February 18, 2013 2:35 PM
    Moderator
  • Hello,

    You need to read first Opportunity Id from Target entity in your case that is opp.

    try this:

    Guid OppId=Guid.Empty;

    if (opp.Attributes.Contains("opportunityid"))
    {
     OppId = ((EntityReference)opp.Attributes["opportunityid"]).Id;

    }

    then change the following line as follows

    task["regardingobjectid"] = new EntityReference("opportunity", OppId );



    Hope this helps. If you get answer of your question, please mark the response as an answer and vote as helpful !
    Vikram !



    • Edited by _Vikram Tuesday, February 19, 2013 11:54 AM
    • Marked as answer by crmpnk1 Tuesday, February 19, 2013 12:11 PM
    Tuesday, February 19, 2013 11:52 AM

All replies

  • Hello,

    you should be good with option 1, just make changes to your code like below

    IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
                    ITracingService trace = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
                    trace.Trace("Started");
                  if (context.MessageName != "Win") { return; }
                  if (!(context.InputParameters.Contains("OpportunityClose") && context.InputParameters["OpportunityClose"] is Entity)) { return; }
                    
                    Entity opp =context.InputParameters["OpportunityClose"]  as Entity ;
                        IOrganizationServiceFactory serviceFactory =              (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                        IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
                        Entity task = new Entity("task");
                        task["subject"] = "Followup Task";
                        task["description"] = "From Plugin";
                       task["regardingobjectid"] = new EntityReference("opportunity", opp.Id );
                         service.Create(task);

    you can refer :http://www.patrickverbeeten.com/Blog/2008/01/25/CRM-40-Plug-in-message-input-parameters for list of parameters


    Contact Me
    Follow me on Twitter
    My Facebook Page
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    • Proposed as answer by HIMBAPModerator Monday, February 18, 2013 2:37 PM
    • Marked as answer by crmpnk1 Tuesday, February 19, 2013 12:11 PM
    Monday, February 18, 2013 2:35 PM
    Moderator
  • Hi Mahender,

    Thanks for the reply I executed the above code. Getting error as "Record is Unavailable. The requested record was not found or you do not have suffieceint permissions to view it."

    I registered plugin for Win message for Opportunity under Post Event,synchronous/asynchronous.

    Regards,

    Tuesday, February 19, 2013 8:40 AM
  • Hello,

    You need to read first Opportunity Id from Target entity in your case that is opp.

    try this:

    Guid OppId=Guid.Empty;

    if (opp.Attributes.Contains("opportunityid"))
    {
     OppId = ((EntityReference)opp.Attributes["opportunityid"]).Id;

    }

    then change the following line as follows

    task["regardingobjectid"] = new EntityReference("opportunity", OppId );



    Hope this helps. If you get answer of your question, please mark the response as an answer and vote as helpful !
    Vikram !



    • Edited by _Vikram Tuesday, February 19, 2013 11:54 AM
    • Marked as answer by crmpnk1 Tuesday, February 19, 2013 12:11 PM
    Tuesday, February 19, 2013 11:52 AM
  • Thanks for Mahender and Vikram. Its working fine.

    Mean time I approached another way also. I have wrote the below code and registered for Create Message, Primary Entity: opportuiclose, Post Event. Whenever the opportunity won the status code will be 3 in database value. So I kept the condition as below. Plugin is firing but it's failure during that line. I printed the State and Status codes it's showing always state:0 statuscode: 1. Kindly let me know the reason.. thanks for your help.

    IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
                    ITracingService trace = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
                    if ((context.MessageName != "Create") && (context.PrimaryEntityName != "opportunityclose")) { return; }
                    if (!(context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)) { return; }
                    Entity opp = context.InputParameters["Target"] as Entity;
                    //Guid oppId = (Guid)context.OutputParameters["id"];
                    EntityReference oppId = opp.Attributes["opportunityid"] as EntityReference;
                    trace.Trace("Opportunity ID:" + oppId.Id );
                    trace.Trace("Opportunity ID-1");
                    IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                    IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
                    trace.Trace("Opportunity ID-2");

                    ColumnSet cols1 = new ColumnSet(new String[] { "statecode", "statuscode"});
                   
                    Entity Opt = (Entity)service.Retrieve("opportunity", oppId.Id , cols1);
                                
                    OptionSetValue state = Opt["statecode"] as OptionSetValue;
                    OptionSetValue status = Opt["statuscode"] as OptionSetValue;

                    trace.Trace("Opportunity ID" + state.Value);
                    trace.Trace("Opportunity ID" + status.Value);

                    if (status.Value == 3)

                    {
                        Entity task = new Entity("task");
                        task["subject"] = "Followup Task";
                        task["description"] = "state" + state.Value  + "Status " +status.Value ;
                        task["regardingobjectid"] = new EntityReference("opportunity", oppId.Id );
                        service.Create(task);
                    }
                        
                    
                }
                catch (FaultException ex)
                {
                    throw new InvalidPluginExecutionException("Error on PLUGIN", ex);
                }

    Tuesday, February 19, 2013 12:18 PM