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);
}
}