CRM Dynamics 2015插件投射错误,指定投射无效

时间:2015-07-14 10:16:08

标签: c# plugins dynamics-crm

下面是该插件代码的片段,我还添加了要使用的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服务,因此问题在于转换Number toi调用和ReceiveCallOn号码,它们都是手机号码,在CRM中它们是手机数据类型,任何想法,为什么我不能投这个。

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

您应该使用ParseTryParse。只是转换为int有时会将char字符转换为其asci值,或者NULL值有例外。由于您正在使用用户输入,我猜以下应该是保护程序。 (如果你绝对想要int's,下面的内容应该没问题),但我同意你之前的帖子\答案,你应该看看工作,而不是字符串。

所以......

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