[英]Update subgrid by parent in Dynamics CRM (opportunity)
I have opportunity, what I need is - when the est time changed the valid_to changed in the subgrid also changed to the same value.我有机会,我需要的是 - 当 est 时间更改时,子网格中更改的 valid_to 也更改为相同的值。 I have tried to write plugin to do that for me but nothing is happening, the product in the subgrid values still the same.
我试图为我编写插件来做到这一点,但没有发生任何事情,子网格值中的产品仍然相同。 What's wrong?
怎么了?
I made plugin and this is the code:我制作了插件,这是代码:
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);
}
}
}
}
I recommend you few things, for a beginner to learn.我推荐你一些东西,供初学者学习。
tracingservice.Trace
to trace the code execution and debug the issuetracingservice.Trace
跟踪代码执行和调试问题InvalidPluginExecutionException
for troubleshootingInvalidPluginExecutionException
进行故障排除This could be a simple copy/paste error.这可能是一个简单的复制/粘贴错误。 But the code is checking for attribute “name”, if there is no change made to name attribute in target entity, then this condition fails.
但是代码正在检查属性“名称”,如果目标实体中的名称属性没有更改,则此条件失败。 Maybe it should be checking “estimatedclosedate” attribute if your plugin step filtering attribute is the same.
如果您的插件步骤过滤属性相同,则可能应该检查“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.