locked
Update record plugin not working--CRM 2011 RRS feed

  • Question

  • Hi, I'm trying to write a plugin so when  the user chooses a value from a optionset (stage), another optionset(probability) will have a selected value. For example, if the user chose Warm Prospect stage, the probability value should be 30%. The create plugin worked fine but the update plugin didnt work, no error message....Below is my code, thanks a lot.

     
    if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                {
                    try
                    {
                        // Obtain the target entity from the input parameters.
                        Entity entity = (Entity)context.InputParameters["Target"];
                        ////Obtain the pre-image entity from the PreEntityImages dictionary.
                        Entity preImage = (Entity)context.PreEntityImages["PreOpportunityImage"];
                        if (entity.LogicalName != "opportunity")
                            return;
                        if (entity.Attributes.Contains("cig_stagecode") && entity.Attributes["cig_stagecode"] != null)
                        {
                            OptionSetValue stagevalue = (OptionSetValue)entity.Attributes["cig_stagecode"];
                            OptionSetValue probvalue = (OptionSetValue)preImage.Attributes["cig_probabilitycode"];
                            
                            if (stagevalue != null && stagevalue.Value == 100000001) //Warm Prospect---30%
                            {
                                probvalue.Value = 100000002; 
                                
                            }
                            //if (stagevalue != null && stagevalue.Value == 100000002) //Serious Prospect --- 80%
                            //{
                            //    probvalue.Value = 100000003;
                            //}
                            //if (stagevalue != null && stagevalue.Value == 100000003) //Open Won --- 95%
                            //{
                            //    probvalue.Value = 100000004;
                            //}
                            //if (stagevalue != null && stagevalue.Value == 100000004)//Closed Won --- 100%
                            //{
                            //    probvalue.Value = 100000005;
                            //}
                            
                        }
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }

    Tuesday, May 27, 2014 3:08 PM

All replies

  • Your code sets an attribute in the preImage entity. For changes to be saved, you need to write them to the Target entity

    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    Tuesday, May 27, 2014 4:21 PM
    Moderator
  • Try the following code.

    Register the plug-in step Pre-operation create and update.

                IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
                IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                {
                    try
                    {
                        // Obtain the target entity from the input parameters.
                        Entity entity = (Entity)context.InputParameters["Target"];
                        ////Obtain the pre-image entity from the PreEntityImages dictionary.
                        //Entity preImage = (Entity)context.PreEntityImages["PreOpportunityImage"];
                        if (entity.LogicalName != "opportunity")
                            return;
                        if (entity.Attributes.Contains("cig_stagecode") && entity.Attributes["cig_stagecode"] != null)
                        {
                            OptionSetValue stagevalue = (OptionSetValue)entity.Attributes["cig_stagecode"];
    						OptionSetValue probvalue = new OptionSetValue(-1);
                            
                            if (stagevalue != null) &&  == ) //Warm Prospect---30%
                            {
    							switch(stagevalue.Value){
    								case 100000001 : {probvalue.Value = 100000002;break;}//Warm Prospect---30%							
    								case 100000002 : {probvalue.Value = 100000003;break;}//Serious Prospect --- 80%							
    								case 100000003 : {probvalue.Value = 100000004;break;}//Open Won --- 95%							
    								case 100000004 : {probvalue.Value = 100000005;break;}//Closed Won --- 100%
    							}
                                
                            }
    						
    						entity.Attributes.Remove("cig_probabilitycode");
    						entity.Attributes.Add("cig_probabilitycode",probvalue);
                        }
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }
    

    • Proposed as answer by F.Kuyler Wednesday, May 28, 2014 12:10 PM
    Wednesday, May 28, 2014 12:09 PM
  • Hi,

    Please refer to this :

    https://community.dynamics.com/crm/f/117/p/128021/277166.aspx?WT.mc_id=ForumPost#277166

    Actually it is because you set the probvalue in the preImage not in the entity.

    OptionSetValue probvalue = (OptionSetValue)preImage.Attributes["cig_probabilitycode"];

    You have to set the value to the target entity as input, not to the preImage

    You need to add to this :

    Entity entity = (Entity)context.InputParameters["Target"];

    Try to add this line :

    entity["cig_probabilitycode"] = new OptionSetValue(probvalue.Value);

    Register in the preOperation on Update (preUpdate) and do not forget to register the image.

    Hope it helps!

    Thanks.

    • Proposed as answer by Aileen Gusni Wednesday, May 28, 2014 5:27 PM
    Wednesday, May 28, 2014 5:26 PM