1
votes

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 text message, but it gives me the following error message "The given key was not present in the dictionary". The plugin's code is shown 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);
   }
}
3

3 Answers

1
votes

Try to change line

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

to

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

It's ok now, i've just forgot to modify my variable Thanks

0
votes

Always first check key before getting like.

if (_entity.Attributes.ContainsKey("field1"))
{Here code to get field1 value }

Because return entity not contains attributes having null values.