动态crm插件查找字段

时间:2013-12-18 11:00:25

标签: plugins dynamics-crm-2011 lookup

我已经尝试了很多代码,但仍然没有工作,我是动态crm 2011中的开发新手。我创建了新的自定义实体“new_smsmessage”,他与User实体有多对多关系,我正在编写插件发送短信给很多用户,我需要在我的插件中检索用户mobilenumber,我使用下面的代码来检索userId,但始终获取crm中的错误消息“给定的密钥不存在于disctionary中” 任何帮助PLZ:

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

2 个答案:

答案 0 :(得分:0)

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

在插件代码中使用它们之前,还应检查属性是否存在。

答案 1 :(得分:0)

这是完整的代码

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

                }
            }
        }