Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Answered

Plugin Issue Updating

(0) ShareShare
ReportReport
Posted on by 105

Hi World,

I triying to copy a value from OptionSet field to number Field

6646.pastedimage1629688227455v1.png

This is my plugin code

 public void Execute(IServiceProvider serviceProvider)
        {
           // Contexto del plugin.
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));



            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
            {
                //Obtenemos la entidad "Contacto" que viene de los parametros de entrada (Target).  
                Entity contacto = (Entity)context.InputParameters["Target"];

                // Interfaz de CRM para las consultas.
                IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

                try
                {
                    Entity preImage = (Entity)context.PreEntityImages["preImage"];
                    Entity contactoCrm = service.Retrieve("contact", contacto.Id, new ColumnSet("clk_customtype", "clk_customtypename", "clk_customtypevalueflow"));

                    string customType = contactoCrm.Attributes.Contains("clk_customtypename") ? contactoCrm["clk_customtypename"].ToString() : string.Empty;


                    // Si el contexto trae el CustomType, es porque lo acabaron de cambiar.
                    if (contacto.Attributes.Contains("clk_customtypename"))
                    {
                        customType = contacto["clk_customtypename"] != null ? contacto["clk_customtypename"].ToString() : string.Empty;
                    }

                    if (string.IsNullOrEmpty(customType))
                    {
                        throw new InvalidPluginExecutionException("Validación de negocio: El CustomType del cliente se encuentra vacío.");
                    }


                    if (!string.IsNullOrEmpty(customType))
                    {
                        int newCustomType = Int32.Parse(customType);

                        contactoCrm.Id = contacto.Id;
                        contactoCrm["clk_customtypevalueflow"] = newCustomType;
                    }

                    service.Update(contactoCrm);
                }


                catch (InvalidPluginExecutionException e)
                {
                    throw e;
                }
                catch (FaultException ex)
                {
                    string error = ex.ToString();

                    throw new InvalidPluginExecutionException(ex.Message);
                }
                catch (Exception e1)
                {
                    throw new InvalidPluginExecutionException(e1.Message, e1);
                }
            }
        }

and this my result

4544.pastedimage1629688406278v2.png

Plugin Trace:

[ClickaliaTest: ClickaliaTest.ActualizarContacto]
[b76f39fb-ab03-ec11-b6e7-000d3ac1ab43: ClickaliaTest.ActualizarContacto]



Error Message:

Exception type: System.ServiceModel.FaultException`1[Microsoft.Xrm.Sdk.OrganizationServiceFault]
Message: The given key was not present in the dictionary.Detail: 

  d8ea166e-54c2-4c2b-8478-ceee99d88669
  -2147220891
  
    
      OperationStatus
      0
    
    
      SubErrorCode
      -2146233088
    
    
      Plugin.ExceptionFromPluginExecute
      ClickaliaTest.ActualizarContacto
    
    
      Plugin.ExceptionRetriable
      false
    
    
      Plugin.ExceptionSource
      PluginExecution
    
    
      Plugin.OriginalException
      PluginExecution
    
    
      Plugin.PluginTrace
      
    
  
  http://go.microsoft.com/fwlink/?LinkID=398563&error=Microsoft.Crm.CrmException:80040265&client=platform
  The given key was not present in the dictionary.
  2021-08-23T03:07:15.4119533Z
  false
  PluginExecution
  
  PluginExecution
  
[ClickaliaTest: ClickaliaTest.ActualizarContacto]
[b76f39fb-ab03-ec11-b6e7-000d3ac1ab43: ClickaliaTest.ActualizarContacto]


So, someone tell me What are doing wrong?

Thanks a lot

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Plugin Issue Updating

    Hi Shiongo,

    For option set field, which has label and value:

    8081.pastedimage1629703521020v1.png

    For getting the option set value:

    int value = ((OptionSetValue)entity["yourattributename"]).Value;

    For getting the text(label):

    String text = entity.FormattedValues["yourattributename"].ToString();

    In the above code entity is the Entity object from which the optionset value/text to be retrieved. Please replace the attribute name with your case.

    Or you can refer following links to check it:

    (+) CRM Plugin - "The given key was not present in the dictionary." - Microsoft Dynamics CRM Forum Community Forum

    (+) The given key was not present in the dictionary - what it means, and how to avoid it - Microsoft Dynamics CRM Community

    Regards,

    Leah Ju

    Please mark as verified if the answer is helpful. Welcome to join hot discussions in Dynamics 365 Forums.

  • Verified answer
    crm development Profile Picture
    crm development 870 on at
    RE: Plugin Issue Updating

    Hi Carlos,

    Error is from line no.22. Comment line no.22- to 29 and use below code instead

    Pass customtypenameValue to your required field.

    if(contactoCrm.Attributes.Contains("clk_customtypename")

    {

      //Get value of option set

       OptionSetValue customtypename = (OptionSetValue)contactoCrm.Attributes["clk_customtypename"];

       int customtypenameValue=     customtypename.Value;

    }

    Hope this helps.

    Thanks,

    Anand

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,711 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,458 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans