Message error "The given key was not present in the dictionary"

Message error "The given key was not present in the dictionary"

This question is answered

Hi,

i have an issue with my plugin, i have a custom entity "new_smsmessage" and from my plugin i want to retrieve custom attributes to send sms but it gives me the message error ""The given key was not present in the dictionary", the code is bellow and the names of attributes are correct in the crm entity :

public void Execute(IServiceProvider serviceProvider)

       {

           ITracingService tracingService =

               (ITracingService)serviceProvider.GetService(typeof(ITracingService));

           // Obtain the execution context from the service provider.

           IPluginExecutionContext context = (IPluginExecutionContext)

               serviceProvider.GetService(typeof(IPluginExecutionContext));

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

                  string uservalue = "";

                  string phonevalue = "";

                  string aliasevalue = "";

                  ColumnSet columnSet = new ColumnSet(true);

                  ColumnSet allFields = new ColumnSet() { AllColumns = true };

                  ExternalSMSService1.ExternalSMSService wbSrvSMS = new ExternalSMSService1.ExternalSMSService();

                  string strToken = wbSrvSMS.Login(userName, pwd);

                  string smsResult = string.Empty;

                  if (entity.Attributes.Contains("new_username"))

                  {

                      uservalue = entity.Attributes["new_username"].ToString();

                  }

                  else

                  {

                      throw new InvalidPluginExecutionException("field name not found");

                  }

                  if (entity.Attributes.Contains("new_userphone"))

                  {

                      phonevalue = entity.Attributes["new_userphone"].ToString();

                  }

                  else

                  {

                      throw new InvalidPluginExecutionException("field Phone not found");

                  }

                  if (entity.Attributes.Contains("new_aliasecode"))

                  {

                      phonevalue = entity.Attributes["new_aliasecode"].ToString();

                  }

                  else

                  {

                      throw new InvalidPluginExecutionException("aliase Phone not found");

                  }

                  string smsmessage = entity.Attributes["new_message"].ToString();

                  string[] strArr = null;

                  string[] strArr2;

                  char[] splitchar = { ';' };

                  strArr = uservalue.Split(splitchar);

                  char[] splitchar2 = { '-' };

                  strArr2 = phonevalue.Split(splitchar2);

                  for (int i = 0; i < strArr.Length; i++)

                  {

                      StringBuilder strMsg = new StringBuilder();

                      strMsg.Append("<SEND_SMS>");

                      strMsg.Append("<MSG_DATA TEXT='" + smsmessage + "' SHORT_CODE='" + aliasevalue + "'/>");

                      strMsg.Append("<RECIPIENTS>");

                      strMsg.Append("<RECIPIENT MOBILE_NUMBER='" + strArr2[i].ToString() + "' RECP_NAME ='" + strArr[i].ToString() + "'/>");

                      strMsg.Append("</RECIPIENTS>");

                      strMsg.Append("</SEND_SMS>");

                      smsResult = wbSrvSMS.SendSMS(strMsg.ToString(), strToken);

                  }

       }

Verified Answer
  • The error can be caused by one of these two lines (however I'm pretty sure that is the first one)

    1) here you need to write Target, not target

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

    and is always suggested to check if the parameter is there:

    if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity) {

    2) you are not checking the new_message attribute as you do with the other ones

    string smsmessage = entity.Attributes["new_message"].ToString();

All Replies
  • The error can be caused by one of these two lines (however I'm pretty sure that is the first one)

    1) here you need to write Target, not target

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

    and is always suggested to check if the parameter is there:

    if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity) {

    2) you are not checking the new_message attribute as you do with the other ones

    string smsmessage = entity.Attributes["new_message"].ToString();

  • Thank you Guido,

    I've make change as you mentioned and now i get a new error message "Index was outside the bounds of the array"

  • It's ok now, thank you i just have forgot to modify my variables

    Thanks again