动态crm插件错误

时间:2013-12-29 11:13:16

标签: c# plugins dynamics-crm crm

我的插件存在问题,我有一个自定义实体new_smsmessage,我想从我的插件中检索自定义属性以发送短信,但它给出了以下错误消息“给定的密钥不是出现在字典中“。插件的代码如下所示,属性名称在CRM实体中是正确的:

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 个答案:

答案 0 :(得分:1)

尝试更改行

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

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

答案 1 :(得分:0)

现在好了,我只是忘了修改我的变量 感谢

答案 2 :(得分:0)

在开始之前,请先检查一下钥匙。

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

因为return实体不包含具有空值的属性。