繁体   English   中英

按 Dynamics CRM 中的父级更新子网格(机会)

[英]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);

                 }
             }
        }
    }

在此处输入图像描述

我推荐你一些东西,供初学者学习。

  1. 使用tracingservice.Trace跟踪代码执行和调试问题
  2. 您可以使用分析器或简单地通过抛出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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM