【问题标题】:CRM 2011 Plugin for CREATE (post-operational): Why is the value of "baseamount" zero in post entity image and target?CRM 2011 Plugin for CREATE(post-operational):为什么 post entity image 和 target 的“baseamount”值为零?
【发布时间】:2013-04-12 20:04:53
【问题描述】:

重新表述的问题(4 月 24 日):

我正在使用 VS2012 的 CRM Developer Toolkit 创建一个 CRM2011 插件。该插件为“发票产品”实体的CREATE 消息注册。 Pipeline-Stage 是后期操作,执行是同步的。我注册了一个包含baseamount的帖子图片。

该工具包创建一个如下所示的执行函数:

protected void ExecutePostInvoiceProductCreate(LocalPluginContext localContext)
{
    if (localContext == null)
    {
        throw new ArgumentNullException("localContext");
    }

    IPluginExecutionContext context = localContext.PluginExecutionContext;

    Entity postImageEntity = (context.PostEntityImages != null && context.PostEntityImages.Contains(this.postImageAlias)) ? context.PostEntityImages[this.postImageAlias] : null;
}

既然处于后期运算阶段,postImageEntity 中的baseamount 的值应该已经从用户输入中计算出来了吧?但是,postImageEntity 中的baseamount 的值为零。我使用以下代码获得的目标实体中baseamount 的值也是如此:

Entity targetEntity = (context.InputParameters != null && context.InputParameters.Contains("Target")) ? (Entity)context.InputParameters["Target"] : null;

使用如下所示的检索请求,我得到了正确的 baseamount 值:

Entity newlyCreated = service.Retrieve("invoicedetail", targetEntity.Id, new ColumnSet(true));
decimal baseAmount = newlyCreated.GetAttributeValue<Money>("baseamount").Value;

该问题不会出现在更新事件的后期运行阶段。

我很高兴听到你关于为什么会这样的想法/解释/建议......

(更多信息:远程调试,无隔离模式,插件存储在数据库中)

原问题:

我正在为 CRM 2011 开发一个插件,该插件应该在创建发票详细信息时计算要支付的税额。为此,我试图在后期操作阶段从后期实体图像中获取新创建的invoicedetail实体的baseamount。据我了解,发布实体图像是创建新发票详细信息后数据库中实体的快照。因此它应该包含新创建的发票明细的所有属性。

我获得了 IPluginExecutionContext 的“postentityimages”属性,其中包含一个具有我注册的别名(“postImage”)的实体。这个“postImage”实体包含一个“baseamount”的键,但它的值为 0。谁能帮我理解为什么会这样以及我能做些什么?

(我还注意到 postImage 不包含我注册的所有实体,而只包含我注册的实体的一个子集。)

代码如下:

  protected void ExecutePostInvoiceProductCreate(LocalPluginContext localContext)
  {
        if (localContext == null)
        {
            throw new ArgumentNullException("localContext");
        }

        // Get PluginExecutionContext to obtain PostEntityImages
        IPluginExecutionContext context = localContext.PluginExecutionContext;

        // This works: I get a postImage that is not null.
        Entity postImage = (context.PostEntityImages != null && context.PostEntityImages.Contains(this.postImageAlias)) ? context.PostEntityImages[this.postImageAlias] : null;

        // Here is the problem: There is a "baseamount" key in the postImage 
        // but its value is zero!
        decimal baseAmount = ((Money)postImage["baseamount"]).Value;

  }

添加:用于后期操作更新的前后图像包含非零值的 baseamount。

【问题讨论】:

  • 您是在本地调试以找出值吗?
  • 不,我正在使用“调试”菜单中的“附加到进程...”进行远程调试。顺便说一句,我在调试器中得到了正确的“数量”值,但“基本数量”为零。
  • 要么该值被其他插件设置为 0,要么没有设置为其他值。由于您正在调试它,请执行一个简单的retrieveEntity 请求来获取新创建的记录,并检查它的baseamount 只是为了查看PostImage 是否有错误,或者在插件运行时是否有其他东西将值更改为0。
  • 我通过以下方式检索到新创建的记录:Entity newlyCreated = service.Retrieve("invoicedetail", targetEntity.Id, new ColumnSet(true));newlyCreated 中baseamount 的值是正确的。因此,我猜 PostImage 有问题。
  • Target 上的值是否正确?

标签: plugins dynamics-crm-2011 crm


【解决方案1】:

只是为了关闭这个问题:最后,我在服务器上重新安装了CRM实例,问题就消失了。

【讨论】:

    【解决方案2】:

    您可以使用以下代码创建一个简单的插件.. 参考here

    public void Execute(IServiceProvider serviceProvider)
        {
            // Obtain the execution context from the service provider.
            ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
    
            // Obtain the execution context from the service provider.
            IPluginExecutionContext context = (IPluginExecutionContext)
                serviceProvider.GetService(typeof(IPluginExecutionContext));
    
            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
    
            // The InputParameters collection contains all the data passed in the message request.
            if (context.InputParameters.Contains("Target") &&
                context.InputParameters["Target"] is Entity)
            {
                Entity entity = (Entity)context.InputParameters["Target"];
    
                // Verify that the target entity represents an account.
                // If not, this plug-in was not registered correctly.
                if (entity.LogicalName != "account")
                    return;
    
                try
                {
    
                         //Access / get data of entity
                         string country = entity.Attributes.ContainsKey("address1_county") ? entity.Attributes["address1_county"].ToString() : "";
                         //Update existing values in entity (Account)
                         entity.Attributes["name"] = "My Name"
    
                }
    
                catch (Exception ex)
                {
                    throw new InvalidPluginExecutionException(ex.Message + ", Stack trace : " +  ex.StackTrace);
                }
    
            }
        }
    

    【讨论】:

      【解决方案3】:

      注意三件事,一件希望能解决您的问题,另外两件是最佳做法。

      1. 帖子图像属性只会填充您在注册时指定填充的值,因此请检查您是否已指定 baseamount 应包含在您的帖子图像中。 (如果用户对该字段没有权限,您也可能会遇到安全问题,但由于它是 0 且不为空,我认为这不是问题)
      2. 使用GetAttributeValue,而不是仅仅访问实体的索引。如果字符串键不在实体属性集合中,则会抛出错误:

        十进制baseAmount = e.GetAttributeValue("baseamount").Value;

      3. 由于这是在创建发票详细信息时运行的,因此目标应包含在创建过程中填充的所有数据,这将消除对后期图像的需要。

      【讨论】:

      • 感谢您的回答达里尔! 1. 我注册了baseamount,但它是零。如果我不注册它,它根本不会出现在后期图像中。 2. 谢谢,我会听从这个建议的。 3. 目标确实包含 baseamount 但它也为零...
      • 如果它解决了您的问题或对@Olli 有帮助,请将其标记为答案。如果没有,那么无论如何,就这样吧。
      • 我是 StackOverflow 的新手,声誉只有 11 人。需要 15 人才能投票。
      • 说真的,我想我已经忘记了那些日子......您将其标记为答案即可获得积分。
      • 好的,我再次检查了所有内容,但不幸的是我的问题仍未解决。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-14
      • 1970-01-01
      相关资源
      最近更新 更多