给定的密​​钥不在字典Microsoft CRM插件中

时间:2013-08-09 08:31:26

标签: c# dynamics-crm-2011

另一个Microsoft CRM插件相关问题。我的插件在更新发票记录时触发。我想将一些发票字段的内容复制到佣金记录中的相应字段。包含截止日期的if似乎工作正常,但包含invoiceamount的if表示密钥不存在错误。我已检查所有字段名称是否正确并存在于正确的实体中。

任何想法发生了什么?我是否需要使用图片?如果是的话,有人可以帮我一些代码吗?

由于

// <copyright file="PreInvoiceUpdate.cs" company="">
// Copyright (c) 2013 All Rights Reserved
// </copyright>
// <author></author>
// <date>8/8/2013 10:40:22 AM</date>
// <summary>Implements the PreInvoiceUpdate Plugin.</summary>
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.1
// </auto-generated>
namespace UpdateCommission
{
    using System;
    using System.ServiceModel;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Query;


    /// <summary>
    /// PreInvoiceUpdate Plugin.
    /// Fires when the following attributes are updated:
    /// All Attributes
    /// </summary>    
    public class PreInvoiceUpdate : Plugin
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="PreInvoiceUpdate"/> class.
        /// </summary>
        public PreInvoiceUpdate()
            : base(typeof(PreInvoiceUpdate))
        {
            base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(20, "Update", "invoice", new Action<LocalPluginContext>(ExecutePreInvoiceUpdate)));

            // Note : you can register for more events here if this plugin is not specific to an individual entity and message combination.
            // You may also need to update your RegisterFile.crmregister plug-in registration file to reflect any change.
        }

        /// <summary>
        /// Executes the plug-in.
        /// </summary>
        /// <param name="localContext">The <see cref="LocalPluginContext"/> which contains the
        /// <see cref="IPluginExecutionContext"/>,
        /// <see cref="IOrganizationService"/>
        /// and <see cref="ITracingService"/>
        /// </param>
        /// <remarks>
        /// For improved performance, Microsoft Dynamics CRM caches plug-in instances.
        /// The plug-in's Execute method should be written to be stateless as the constructor
        /// is not called for every invocation of the plug-in. Also, multiple system threads
        /// could execute the plug-in at the same time. All per invocation state information
        /// is stored in the context. This means that you should not use global variables in plug-ins.
        /// </remarks>
        protected void ExecutePreInvoiceUpdate(LocalPluginContext localContext)
        {
            if (localContext == null)
            {
                throw new ArgumentNullException("localContext");
            }

            // TODO: Implement your custom Plug-in business logic.

            // Obtain the execution context from the service provider.
            IPluginExecutionContext context = localContext.PluginExecutionContext;
            IOrganizationService service = localContext.OrganizationService;
            IServiceProvider serviceProvider = localContext.ServiceProvider;
            ITracingService tracingService = localContext.TracingService;


            // Obtain the target entity from the input parmameters.
            //Entity contextEntity = (Entity)context.InputParameters["Target"];

            if (context.Depth == 1)
            {

                #region Set up variables

                Entity targetInvoice = null;
                targetInvoice = (Entity)context.InputParameters["Target"];
                Guid invoiceID = targetInvoice.Id;


                ColumnSet invoiceCols = new ColumnSet("new_totalcomm", "new_grossprofit", "new_invoiceamount", "duedate");


                //Entity contact = service.Retrieve("contact", cid, cols);
                //contact.Attributes["jobtitle"] = "Sometitle";
                // contact.Attributes["address1_line1"] = "Some address";
                //service.Update(contact);


                string fetchCommissionQuery = @"
<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
  <entity name='tbw_commission'>
    <attribute name='tbw_commissionid' />
    <attribute name='tbw_name' />
    <attribute name='createdon' />
    <order attribute='tbw_name' descending='false' />
    <filter type='and'>
      <condition attribute='new_relatedinvoice' operator='eq' uitype='invoice' value='";

                fetchCommissionQuery += invoiceID;
                fetchCommissionQuery += @"' />
    </filter>
  </entity>
</fetch> ";


                EntityCollection commissionCollection = service.RetrieveMultiple(new FetchExpression(fetchCommissionQuery));

                //targetInvoice = service.Retrieve("invoice", invoiceID, invoiceCols);

                #endregion

                foreach (var commission in commissionCollection.Entities)
                {
                    if (targetInvoice.Attributes["duedate"] != null)
                    {

                        commission.Attributes["tbw_duedate"] = targetInvoice.Attributes["duedate"];
                    }

                    if (targetInvoice.Attributes["new_invoiceamount"] != null)// && commission.Attributes["tbw_paymentrate"] != null)
                    {
                        //dynamic commRate = commission.Attributes["tbw_paymentrate"];

                        //dynamic invoiceTotal = ((Money)targetInvoice.Attributes["new_invoiceamount"]).Value;

                        //dynamic commTotal = commRate * invoiceTotal;

                        //commission.Attributes["tbw_total"] = new Money(commTotal);
                        //commission.Attributes["tbw_total"] = 1.02;
                    }

                    //commission.Attributes["tbw_name"] = "TestCommName";
                    service.Update(commission);

                }


                // dynamic currentInvoiceTotal = ((Money)targetInvoice.Attributes["new_invoiceamount"]).Value;

                // currentInvoiceTotal = currentInvoiceTotal - 10;

                // Entity invoice = service.Retrieve("invoice", invoiceID, invoiceCols);
                // invoice.Attributes["new_grossprofit"] = new Money(currentInvoiceTotal);
                //service.Update(invoice);

                //}

            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

在更新中,您将只获得更新字段的值。目标实体中不存在其他字段。您需要使用PreImage注册插件。将字段添加到要在插件中访问的PreImage。

preImage = (Entity)context.PreEntityImages["PreImage"];

如何Register Plugin with Image