【问题标题】:Update Contact MiddleName field on Form Load - MS CRM 2011 Plugin更新表单加载上的联系人中间名字段 - MS CRM 2011 插件
【发布时间】:2014-02-08 07:06:53
【问题描述】:

几天前我开始了 MS CRM 开发,今天我花了几个小时在这个非常简单的基本操作上,但没有找到我做错的地方。

我想在创建记录时更新联系人实体的中间名。我可以用下面的代码做到这一点。

但现在我想在打开联系人记录时做同样的事情。我确实在联系实体下注册了检索消息的新步骤。但它不起作用..没有抛出异常。

    public class IzzyPlugin : IPlugin
{
    public void Execute(IServiceProvider serviceProvider)
    {
        try
        {
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

            IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = factory.CreateOrganizationService(context.UserId);


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


                if (currentEntity.Attributes.Contains("middlename"))
                {
                    currentEntity.Attributes["middlename"] = "Middle name changed";
                }
                else
                {
                    currentEntity.Attributes.Add("middlename", "Middle name changed");
                }

                service.Update(currentEntity);


            }
        }
        catch (Exception f)
        {
            throw new InvalidPluginExecutionException(f.ToString());
        }

    }

}

【问题讨论】:

  • 你确定插件正在触发吗?在我看来没问题。用简单的 throw new Exception() 替换您的代码,并验证当您尝试通过 UI 创建联系人时操作已取消。
  • 我这样做了。按预期抛出异常。但是,您能否回答更新后的问题。

标签: dynamics-crm-2011


【解决方案1】:

你不能在页面加载时使用 javascript 在打开记录时设置中间名值吗? IMO会容易得多

类似这样的 onload 会起作用

Xrm.Page.getAttribute('middlename').setValue('New Middle Name');

如果您需要从同一实体中的其他字段计算中间名,您可能需要参考XRM Page documentation on MSDN

【讨论】:

    【解决方案2】:

    我不会质疑您为什么要这样做,或者您将在哪里获得中间名的值,但我怀疑问题在于您的插件/更新的顺序。针对Retrieve 消息编写插件并不是一个好主意,因为它们被频繁调用。我个人认为 JScript 是要走的路,但是...

    如果您想在 middlename 中插入一个值,然后在 Retrieve 上将其返回给用户,但 而不 将其提交到数据库(我知道这不是您的确切问题),那么:

    1. 在检索消息的术后注册您的插件
    2. 在您的代码中输入:
    >     Entity currentEntity = (Entity)context.OutputParameters["Entity"];
    >     if(currentEntity.Attributes.contains("middlename"){
    >         currentEntity["middlename"] = "New value";    
    >     }else{
    >         currentEntity.Attributes.Add("middlename","New value");
    >     }
    

    如果您想在middlename 中插入一个值并在检索时将其返回给用户将其提交到数据库,那么我怀疑您需要将两者结合到一个注册的插件中Retrieve 的操作前和操作后,然后执行类似的操作(但我什至对每次 Retrieve 都尝试更新......!):

    public class IzzyPlugin : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            try
            {
                IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
                IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = factory.CreateOrganizationService(context.UserId);
    
                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                {
                    var middleNameValue = "Middle name changed";
                    Entity currentEntity;
    
                    // Pre-stage plugin
                    if(context.Stage < 30){
                        currentEntity = (Entity)context.InputParameters["Target"];
    
                        if (currentEntity.Attributes.Contains("middlename"))
                        {
                            currentEntity.Attributes["middlename"] = middleNameValue;
                        }
                        else
                        {
                            currentEntity.Attributes.Add("middlename", middleNameValue);
                        }
                        service.Update(currentEntity);
                    }else{
                        currentEntity = (Entity)context.OutputParameters["Entity"];
                        if(currentEntity.Attributes.contains("middlename"){
                            currentEntity["middlename"] = middleNameValue;    
                        }else{
                            currentEntity.Attributes.Add("middlename",middleNameValue);
                        }                   
                    }
                }
            }
            catch (Exception f)
            {
                throw new InvalidPluginExecutionException(f.ToString());
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      我建议使用 JavaScript 执行此操作。我不认为你可以注册一个可以在打开记录时触发的插件。

      【讨论】:

      • 嗨,Retrieve message 有什么作用?我认为它会在我们加载记录时触发
      • 确实如此。您可以在 Retrieve 上注册一个插件,它有可能显着提高性能。
      猜你喜欢
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 2013-03-16
      • 1970-01-01
      • 1970-01-01
      • 2012-12-11
      • 1970-01-01
      • 2011-09-30
      相关资源
      最近更新 更多