简体   繁体   English

CRM Dynamics 2015插件转换错误,指定的转换无效

[英]CRM Dynamics 2015 Plugin Cast Error, Specified Cast Is not valid

Below is a snippet of the code for the plugin, I also added the WCF service to be consumed(code) as well. 下面是该插件的代码片段,我还添加了要使用的WCF服务(代码)。

PLUGIN CODE BELOW 插件代码如下

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

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

            if (phoneCallEntity.LogicalName != "phonecall")
                return;

            if (context.MessageName == "Create")
            {
                try
                {

                    int NumberToCall = phoneCallEntity.Attributes.Contains("phonenumber") ? (int)phoneCallEntity.Attributes["phonenumber"] : 0;
                    int ReceiveCallOn = phoneCallEntity.Attributes.Contains("new_destination") ? (int)phoneCallEntity.Attributes["new_destination"] : 0;
                    string apiKey = phoneCallEntity.Attributes.Contains("new_apikey") ? phoneCallEntity.Attributes["new_apikey"].ToString() : null;
                    int fId = phoneCallEntity.Attributes.Contains("new_fid") ? (int)phoneCallEntity.Attributes["new_fid"] : 0;

                    BasicHttpBinding binding = new BasicHttpBinding();
                    binding.Name = "BasicHttpBinding_IService1";

Here i call the service 在这里我打电话给服务

 binding.SendTimeout = new TimeSpan(0, 10, 0);
 EndpointAddress endPointAddress = new EndpointAddress("http://localhost:62009/Service1.svc");
 ServiceReference1.Service1Client client = new ServiceReference1.Service1Client(binding, endPointAddress);
 client.WebCall(NumberToCall, ReceiveCallOn, apiKey, fId);

Above I call the WCF Service 上面我称WCF服务

public void WebCall(Int64 NumberToCall, Int64 ReceiveCallOn, string APIkey, int FID)
    {
        string url = string.Format("https://xxxx{0},{1},{2}", NumberToCall, ReceiveCallOn, APIkey, FID);
        WebRequest webRequest = WebRequest.Create(url);
        WebResponse webResp = webRequest.GetResponse();
        webRequest.Method = "POST";
    }

Code snippet above is the actual WCF Service being consumed, So the issue is with the casting of the Number toi call and the ReceiveCallOn number, they are both cell phone numbers , in CRM they are a phone data type, any ideas why I cannot cast this. 上面的代码段是实际使用的WCF服务,所以问题在于号码toi呼叫和ReceiveCallOn号码的转换,它们都是手机号码,在CRM中,它们是电话数据类型,我无法转换的任何想法这个。

the field phonenumber from entity phonecall is Single Type of Text (with format Phone ), this means that is a string (as your custom field if the field settings are the same) so your code can be: 实体phonecall的字段phonenumberSingle Type of Text (格式为Phone ),这意味着它是一个字符串(如果字段设置相同,则作为您的自定义字段),因此您的代码可以是:

string NumberToCall = phoneCallEntity.Contains("phonenumber") ? phoneCallEntity["phonenumber"].ToString() : "";
string ReceiveCallOn = phoneCallEntity.Contains("new_destination") ? phoneCallEntity["new_destination"].ToString() : "";

You should rather use Parse or TryParse . 您应该使用ParseTryParse Just casting to int will sometimes convert a char character to its asci value, or have an exception on NULL values. 仅转换为int有时会将char字符转换为其asci值,或者在NULL值上有例外。 Since you are working with user input I guess the following should be saver. 由于您正在使用用户输入,因此我想以下应该是节省程序。 (if you absolutely want int's the below should be ok), I do however agree with the previous post\\answer that you should look at working, with strings rather. (如果您绝对希望使用int's则可以使用下面的方法),但是我确实同意上一篇文章\\ answer的建议,您应该使用字符串,而不是字符串。

so ... 所以...

var NumberToCall =0;
var ReceiveCallOn =0;

if (phoneCallEntity.Contains("phonenumber"))
   int.TryParse(phoneCallEntity["phonenumber"].ToString(), out NumberToCall);
if (phoneCallEntity.Contains("new_destination") )
   int.TryParse(phoneCallEntity["new_destination"].ToString(), out ReceiveCallOn);

... just taking your parse exception into consideration . ...仅考虑解析异常。

Or just Parse 或者只是Parse

if (phoneCallEntity.Contains("phonenumber"))
{
    int NumberToCall = int.Parse(phoneCallEntity["phonenumber"].ToString());
}
if (phoneCallEntity.Contains("new_destination"))
{
    int ReceiveCallOn = int.Parse(phoneCallEntity["new_destination"].ToString());
} 

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

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