繁体   English   中英

动态CRM插件查找字段

[英]dynamics crm plugin lookup field

我尝试了很多代码,但仍然无法正常工作,我是Dynamics crm 2011开发人员中的新手。我创建了与用户实体有许多关系的新自定义实体“ new_smsmessage”,我正在编写插件以向其发送短信许多用户,我需要在插件中获取用户的mobilenumber,我使用下面的代码来检索userId,但始终获得crm中的错误消息“该给定密钥未出现在任意内容中”任何帮助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();
                }
 }

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

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

这是完整的代码

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

                }
            }
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM