[英]Update subgrid by parent in Dynamics CRM (opportunity)
我有機會,我需要的是 - 當 est 時間更改時,子網格中更改的 valid_to 也更改為相同的值。 我試圖為我編寫插件來做到這一點,但沒有發生任何事情,子網格值中的產品仍然相同。 怎么了?
我制作了插件,這是代碼:
public void Execute(IServiceProvider serviceProvider)
{
// extract the service provider
ITracingService tracingservice = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory srevicefactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = srevicefactory.CreateOrganizationService(context.UserId);
if(context.InputParameters.Contains("Target")&&context.InputParameters["Target"] is Entity)
{
Entity entity = (Entity)context.InputParameters["Target"];
if (entity.Contains("name"))
{
var fetch = @"<fetch no-lock='true' >
<entity name='opportunity' >
<attribute name='contactid'/>
<filter>
<condition attribute='opportunityid' operator='eq' value='{0}' />
</filter>
</entity>
</fetch>";
var fetchXML = string.Format(fetch, entity.Id);
var allContacts = service.RetrieveMultiple(new FetchExpression(fetchXML)).Entities;
foreach (var contactEnt in allContacts)
{
Entity contactToUpdate = new Entity("opportunityproduct", contactEnt.Id);
contactToUpdate["new_valid_to"] = entity["estimatedclosedate"];
service.Update(contactToUpdate);
}
}
}
}
我推薦你一些東西,供初學者學習。
tracingservice.Trace
跟蹤代碼執行和調試問題InvalidPluginExecutionException
進行故障排除這可能是一個簡單的復制/粘貼錯誤。 但是代碼正在檢查屬性“名稱”,如果目標實體中的名稱屬性沒有更改,則此條件失敗。 如果您的插件步驟過濾屬性相同,則可能應該檢查“estimatedclosedate”屬性。
if (entity.Contains("estimatedclosedate")) //changed name into estimatedclosedate
{
tracingService.Trace("condition passed and est_date is in target entity.");
throw new InvalidPluginExecutionException("Debugging...");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.