在 Dynamics CRM 中按父更新子网格机会

问题描述

我有机会,我需要的是 - 当估计时间更改时,子网格中更改的 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);

                 }
             }
        }
    }

enter image description here

解决方法

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

  1. 使用 tracingservice.Trace 跟踪代码执行并调试问题
  2. 您可以使用探查器或直接抛出 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...");

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...