2
votes

I have try many codes around but still not working, i'm new in dev in dynamics crm 2011. i have create new custom entity "new_smsmessage" who has many to many relationship with User entity, i'm writing plugin to send sms to many users, and i need in my plugin to retreive users mobilenumber, i use the code bellow to retrieve userId but get all the time the error message in the crm "The given key was not present in the disctionary" any help 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

2 Answers

0
votes

Most likely systemuser is not the name of your attribute. If it is the owner of the new_smsmessage record then it would be (EntityReference)entity["ownerid"]. If it is a custom attribute that is a lookup to user then it would be something like this (EntityReference)entity["new_systemuser"].

You should also check to see if the attributes exist before using them in your plugin code.

0
votes

here is the complete code

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

                }
            }
        }