CRM动态插件更新不起作用?

时间:2015-07-22 18:09:00

标签: c# dynamics-crm-2013

我有一个CRM动态插件,它在更新布尔值更改时触发,当我在Two Option控件上选择是时,它无法触发,请在下面找到我的代码并告知我可能出错的地方。

namespace WebCall.Plugin
{
  public class WebCallTrigger : IPlugin
  {
    /// <summary>
    /// Plugin to initiate a web call from CRM using MaruSip API
    /// </summary>
    /// <param name="serviceProvider"></param>
    public void Execute(IServiceProvider serviceProvider)
    {

        IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

        if (context == null)
        {
            throw new ArgumentNullException("localContext");
        }

        IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

        IOrganizationService service = serviceFactory.CreateOrganizationService(context.InitiatingUserId);

        if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
        {
            //initaialize Entity
            Entity phoneCallEntity = (Entity)context.InputParameters["Target"];

            if (phoneCallEntity.LogicalName != Contact.EntityLogicalName)
                return;

            //ensure that the Plugin fires on a create operaton
            if (context.MessageName == "Update")
            {
                try
                {
                    if (phoneCallEntity.Attributes.Contains("new_dialnumber"))
                        phoneCallEntity["new_dialnumber"] = true;

                    string NumberToCall;                // = phoneCallEntity.Contains("telephone1") ? phoneCallEntity["telephone1"].ToString() : null;

                    if (phoneCallEntity.Contains("telephone1"))
                    {
                        NumberToCall = phoneCallEntity.Attributes["telephone1"].ToString();
                    }

                    else
                    {
                        NumberToCall = phoneCallEntity.Attributes["mobilephone"].ToString();
                    }

                    string ReceiveCallOn = phoneCallEntity.Contains("new_receivecallon") ? phoneCallEntity["new_receivecallon"].ToString() : null;
                    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;

                    //service.Update(phoneCallEntity);

                    //Create a new instance of the WebCallService and call the webcall method
                    WebCallService webCallService = new WebCallService();

                    webCallService.WebCall(NumberToCall, ReceiveCallOn, apiKey, fId);
                }

2 个答案:

答案 0 :(得分:0)

如果未更新telephone1或手机字段 - 您所指的目标将不包含提及的字段。我建议从pre-Image获取数据。您可以查看此文章 - https://deepakexploring.wordpress.com/2011/02/04/preentityimages-and-postentityimages-in-crm-5-0-2011/

答案 1 :(得分:0)

如果您在更新电话呼叫实体时注册了该插件,并且您将其逻辑名称与“联系人”进行比较,那么您的插件将永远无法触发。

从代码中查看此行:

if (phoneCallEntity.LogicalName != Contact.EntityLogicalName)
                return;