我正在爲自定義實體(OpportunityService)開發更新插件。我的目標是比較更新前後的數據。這就是爲什麼我註冊了Pre圖像和Post圖像類型的實體圖像。圖像的名稱是OpportunityService,別名也是OpportunityService。CRM 2011插件:實體圖像:KeyNotFoundException
然後在我的代碼中,我試圖讓這些圖像,以便我可以檢查是否有一些字段被更改,如果他們是我會執行一些操作。但這不在我的問題範圍之內。
我想是指實體圖像如下
Entity preOpportunityService = (Entity)context.PreEntityImages["OpportunityService"];
Entity postOpportunityService = (Entity)context.PostEntityImages["OpportunityService"];
但在這一點上我的插件拋出System.Collections.Generic.KeyNotFoundException。
「Business Process Error。來自插件的意外異常(Execute):OpportunityServicePlugin.OpportunityServiceCalculatorOnUpdate:System.Collections.Generic.KeyNotFoundException:給定的鍵在字典中不存在。
我此刻的完整代碼很簡單,因爲這:
using System;
using System.ServiceModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
namespace OpportunityServicePlugin
{
public class OpportunityServiceCalculatorOnUpdate: IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
// General plugin components
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = factory.CreateOrganizationService(context.UserId);
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
try
{
// Current opportunity service
Entity opportunityService = (Entity)context.InputParameters["Target"];
// Opportunity service's parent opportunity lookup reference
EntityReference opportunityReference = (EntityReference)opportunityService.Attributes["mpc_opportunityid"];
// Columns to be retrieved for opportunity (aka. columns to be edited)
ColumnSet opportunityColumnSet = new ColumnSet(new string[] { "estimatedvalue", "mpc_estoneoffinvoicing", "mpc_estinvoicingperyear" });
// Retrieve actual opportunity entity
Entity opportunity = service.Retrieve(opportunityReference.LogicalName, opportunityReference.Id, opportunityColumnSet);
// Opportunity service's money fields
Money monthlyPrice = (Money)opportunityService["mpc_monthlyprice"];
Money oneOffPrice = (Money)opportunityService["mpc_startprice"];
Money estInvoicingPerYear = (Money)opportunityService["mpc_estinvoicingperyear"];
Entity preOpportunityService = (Entity)context.PreEntityImages["OpportunityService"];
Entity postOpportunityService = (Entity)context.PostEntityImages["OpportunityService"];
}
catch (FaultException<OrganizationServiceFault> ex) { tracingService.Trace("FaultException", ex.ToString()); }
}
}
}
我的插件在術後階段(更新消息)同步註冊。
我在這裏做錯了,我看不到?
預先感謝您。
編輯:這裏的答案
謝謝你的答案。通過閱讀他們,並試圖找出什麼是錯的,我終於明白,這個問題是不是在所有的實體圖像,但這一行:
EntityReference opportunityReference = (EntityReference)opportunityService.Attributes["mpc_opportunityid"];
,是因爲它是更新的消息,並將其返回只改值的「目標」的「mpc_opportunityid」是實際的問題就在這裏-.-
我改變了我的代碼以下
try
{
Entity preOpportunityService = (Entity)context.PreEntityImages["OpportunityService"];
Entity postOpportunityService = (Entity)context.PostEntityImages["OpportunityService"];
// Opportunity service's parent opportunity lookup reference
EntityReference opportunityReference = (EntityReference)postOpportunityService.Attributes["mpc_opportunityid"];
// Columns to be retrieved for opportunity (aka. columns to be edited)
ColumnSet opportunityColumnSet = new ColumnSet(new string[] { "estimatedvalue", "mpc_estoneoffinvoicing", "mpc_estinvoicingperyear" });
// Retrieve actual opportunity entity
Entity opportunity = service.Retrieve(opportunityReference.LogicalName, opportunityReference.Id, opportunityColumnSet);
}
catch (FaultException<OrganizationServiceFault> ex) { tracingService.Trace("FaultException", ex.ToString()); }
和現在的作品...
問題發生在這一行: 實體preOpportunityService =(實體)context.PreEntityImages [「OpportunityService」]; 我甚至從代碼中拿走了這三個錢幣字段,因爲我們可以在解決這個問題後從圖像中獲得金錢價值。 – kivikall
您可以驗證圖像是否正確註冊?即string registredkeys =「Registered pre images:」+ String.Join(「,」,context.PreEntityImages.Keys);並將此字符串的異常作爲錯誤消息拋出。 – MarioZG
謝謝你的回答。我找到了答案。我很抱歉誤導我的第一個答覆。我在原來的帖子中添加了答案。 – kivikall