Задача - создать связанный с opportunity объект сущности tcc_oppmonthly при измененении opportunity. Поле связи - tcc_monthlydetailsid.
//values for fields - заготовка, мне надо разделить значение поля estimatedvalue на некое значение и записать результат в поле tcc_value вновь создаваемой сущности.
Код:
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Crm.Sdk;
using Microsoft.Crm.SdkTypeProxy;
using Microsoft.Crm.Sdk.Query;
namespace MonthlyOppDetail
{
public class MonthDetailCreationHandler : IPlugin
{
#region IPlugin Members
public void Execute(IPluginExecutionContext context)
{
if (context.MessageName == MessageName.Update
&& context.InputParameters.Contains("Target")
&& context.InputParameters["Target"] is DynamicEntity)
{
DynamicEntity entity = (DynamicEntity)context.InputParameters["Target"];
//Check that target entity is opportunity
if (entity.Name != EntityName.opportunity.ToString())
return;
//Check that this opportunity has entered needed fields
if (!entity.Properties.Contains("estimatedvalue"))
return;
//values for fields
decimal sum = ((CrmMoney)entity.Properties["estimatedvalue"]).Value;
CrmMoney sum223 = new CrmMoney();
sum223.Value = sum / 2;
ICrmService crmservice = context.CreateCrmService(true);
DynamicEntity entityM = new DynamicEntity();
TargetCreateDynamic targetCreate = new TargetCreateDynamic();
CreateRequest createRequest = new CreateRequest();
entityM.Name = "tcc_oppmonthly";
CrmMoneyProperty _Tcc_value = new CrmMoneyProperty();
_Tcc_value.Name = "tcc_value";
_Tcc_value.Value = sum223;
entityM.Properties.Add(_Tcc_value);
KeyProperty _tcc_monthlydetailsid = new KeyProperty();
_tcc_monthlydetailsid.Name = "tcc_monthlydetailsid";
_tcc_monthlydetailsid.Value = (Key)entity.Properties["opportunityid"];
entityM.Properties.Add(_tcc_monthlydetailsid);
targetCreate.Entity = entityM;
createRequest.Target = targetCreate;
crmservice.Create(entityM);
}
}
#endregion IPlugin Members
}
}
Регистрацию провожу на Post событие Update, Parent pipeline . В синхронном режиме - в файл трассировки записано только "The entity with a name = 'none' was not found in the MetadataCache., ErrorCode: -2147217150". В асинхронном - также
|Level: Error | MessageProcessor.Execute
>MessageProcessor fail to process message 'Create' for 'tcc_oppmonthly'.
[2009-08-28 19:35:56.0] Process: w3wp |Organization:959e4b7c-ab67-dd11-b616-000c29704829 |Thread: 6 |Category: Platform.Sdk |User: b04a20e9-ab67-dd11-b616-000c29704829 |Level: Error | DefaultExceptionHandler.Handle
>CrmSoapExtension detected non-CrmException - report will be sent to Watson:
System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.InvalidCastException: Unable to cast object of type 'Microsoft.Crm.Sdk.KeyProperty' to type 'Microsoft.Crm.Sdk.LookupProperty'.
Пока не могу понять - почему
KeyProperty ошибка есть только в одном режиме.
Заранее благодарен за указание ошибок.
Спасибо.