问题描述
我有机会,我需要的是 - 当估计时间更改时,子网格中更改的 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
进行故障排除
这可能是一个简单的复制/粘贴错误。但是代码正在检查属性“name”,如果目标实体中的 name 属性没有更改,则此条件失败。如果您的插件步骤过滤属性相同,也许应该检查“estimatedclosedate”属性。
if (entity.Contains("estimatedclosedate")) //changed name into estimatedclosedate
{
tracingService.Trace("condition passed and est_date is in target entity.");
throw new InvalidPluginExecutionException("Debugging...");