【问题标题】:dynamics crm plugin lookup field动态 crm 插件查找字段
【发布时间】:2013-12-19 12:52:59
【问题描述】:

我尝试了很多代码,但仍然无法正常工作,我是动态 crm 2011 开发人员的新手。我创建了新的自定义实体“new_smsmessage”,它与用户实体有很多对多的关系,我正在编写插件向许多用户发送短信,我需要在我的插件中检索用户的手机号码,我使用下面的代码来检索用户 ID,但始终在 crm 中收到错误消息“字典中不存在给定的密钥” 任何帮助请:

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


                if (entity.Attributes.Contains("new_smsmessage") == false)
                {

                    string smstext = entity.Attributes["new_message"].ToString();
                    string smsnumber = entity.Attributes["new_phonenumber"].ToString();

                    EntityReference userlookup = (EntityReference)entity["systemuser"];

                    string receipient = userlookup.Name.ToString();
                }
 }

【问题讨论】:

    标签: plugins dynamics-crm-2011 lookup


    【解决方案1】:

    systemuser 很可能不是您的属性名称。如果它是new_smsmessage 记录的所有者,那么它将是(EntityReference)entity["ownerid"]。如果它是用于查找用户的自定义属性,那么它将类似于 (EntityReference)entity["new_systemuser"]

    您还应该在插件代码中使用属性之前检查这些属性是否存在。

    【讨论】:

    • 但是我将自定义属性更改为查找字段的名称,它给出了相同的错误:
    【解决方案2】:

    这是完整的代码

    public void Execute(IServiceProvider serviceProvider)
            {
                // Obtain the execution context from the service provider.
                Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)
                    serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));
    
                // The InputParameters collection contains all the data passed in the message request.
                if (context.InputParameters.Contains("Target") &&
                    context.InputParameters["Target"] is Entity)
                {
    
                    Entity entity = new Entity("New_smsmessage");
    
                    ExternalSMSService1.ExternalSMSService wbSrvSMS = new ExternalSMSService1.ExternalSMSService();
    
                    string strToken = wbSrvSMS.Login(userName, pwd);
                    string smsResult = string.Empty;
                    string smstext = entity.Attributes["new_message"].ToString();
                    string smsnumber = entity.Attributes["new_phonenumber"].ToString();
                    EntityReference aliaselookup = (EntityReference)entity.Attributes["new_aliaseid"].ToString;
    
                    switch (strToken)
                    {
                        case "01":
                            Console.WriteLine("Invalid Username or pwd");
                            break;
    
                        case "03":
                            Console.WriteLine("Host Application Down");
                            break;
    
                        default:
                            StringBuilder strMsg = new StringBuilder();
                            strMsg.Append("<SEND_SMS>");
                            strMsg.Append("<MSG_DATA TEXT='" + smstext + "' SHORT_CODE='" + aliaselookup + "'/>");
                            strMsg.Append("<RECIPIENTS>");
                            strMsg.Append("<RECIPIENT MOBILE_NUMBER='" + smsnumber + "' RECP_NAME ='tester'/>");
                            strMsg.Append("</RECIPIENTS>");
                            strMsg.Append("</SEND_SMS>");
    
                            smsResult = wbSrvSMS.SendSMS(strMsg.ToString(), strToken);
    
                            switch (smsResult)
                            {
                                case "01":
                                    Console.WriteLine("Invalid or Expired token");
                                    break;
    
                                case "02":
                                    Console.WriteLine("Incorrect input XML format");
                                    break;
    
                                case "03":
                                    Console.WriteLine("Host Application down");
                                    break;
    
                                default:
                                    Console.WriteLine("SMS Sent Successfully");
                                    break;
                            }
    
                            break;
                        // }
    
                    }
                }
            }
    

    【讨论】:

    • 我认为问题出在这一行:Entity entity = new Entity("New_smsmessage"); 而应该是Entity entity = (Entity)context.InputParameters["Target"]。因为您将实体设置为新的 Entity 类,所以没有与之关联的属性,这就是您收到 The given key was not present in the disctionary 错误的原因。
    猜你喜欢
    • 1970-01-01
    • 2017-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多