另一个与 Microsoft CRM 插件相关的问题。更新发票记录时,我的插件正在触发。我想将一些发票字段的内容复制到佣金记录上的相应字段中。包含到期日期的 if 似乎工作正常,但包含 invoiceamount 的 if 给出了一个 key not present 错误。我检查了所有字段名称是否正确并且存在于正确的实体中。
任何想法发生了什么?我需要使用图像吗?如果需要,有人可以帮我写一些代码吗?
谢谢
// <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);
//}
}
}
}
}