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

CRM Dynamics 2015 Plugin Cast Error, Specified Cast Is not valid

下面是插件的代码片段,我还添加了要使用的 WCF 服务(代码)。

下面的插件代码

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";

我在这里调用服务

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

上面我调用了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";
    }

上面的代码片段是正在使用的实际 WCF 服务,因此问题出在 CRM 中 Number toi 调用和 ReceiveCallOn 号码的转换,它们都是单元格 phone 号码它们是 phone 数据类型,不知道为什么我不能转换它。

来自实体 phonecall 的字段 phonenumberSingle Type of Text(格式为 Phone),这意味着它是一个字符串(如果字段设置为您的自定义字段相同)所以你的代码可以是:

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

您应该使用 ParseTryParse。仅转换为 int 有时会将 char 字符转换为其 asci 值,或者在 NULL 值上出现异常。由于您正在处理用户输入,我想以下内容应该更省事。 (如果你绝对想要 int's 下面应该没问题),但是我同意之前的 post\answer 你应该看看工作,而不是字符串。

所以...

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

...只是考虑了您的解析异常。

或者只是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());
}