关联方法

时间:2012-02-03 13:15:04

标签: c# plugins dynamics-crm-2011

我正在尝试将实体的相关记录关联到新创建的实体。插件会在创建和预操作时触发。

尝试将集合与新实体关联时发生错误:“id = ad630ba6-684e-e111-92e3-00155d151905的new_ligneContrat不存在”

这是我的代码:

public void Execute(IServiceProvider serviceProvider)
        {
            // Instanciation des services

            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = factory.CreateOrganizationService(null);


            Entity target = (Entity)context.InputParameters["Target"];
            EntityReference contrats = (EntityReference)target.Attributes["new_contratsid"];

            FetchExpression fetch = new FetchExpression(@"
                    <fetch distinct='false' mapping='logical'>
                      <entity name='" + context.PrimaryEntityName + "'><link-entity name='new_contrats' alias='nombreligne' from='new_contratsid' to='new_contratsid'><filter type='and'><condition attribute='new_contratsid' value='" + contrats.Id + "' operator='eq'></condition></filter></link-entity></entity></fetch>");

            EntityCollection lines = service.RetrieveMultiple(fetch);

                  // Vérification qu'il y a au moins une ligne de contrat associée
            if (lines.Entities.Any())
            {

                var first = lines.Entities.Last();

                if (first.GetAttributeValue<OptionSetValue>("statecode").Value == 1)
                {
                    FetchExpression query = new FetchExpression(@"
                        <fetch distinct='false' mapping='logical'>
                        <entity name='incident'><filter type='and'><condition attribute='new_lignecontrat' value='"+first.Id+"' operator='eq'/></filter></entity></fetch>");

                    EntityCollection incident = service.RetrieveMultiple(query);

                    if (incident.Entities.Any())
                    {


                        foreach (var e in incident.Entities)
                        {
                           e.Attributes["new_lignecontrat"] = new EntityReference (target.LogicalName, target.Id);
                        }


                 }
              }
           }   

有什么问题?

提前致谢!!

编辑1:ok似乎合乎逻辑,因为记录尚不存在&gt;&lt;。只有一件事:如何更改查找字段的值?它的类型是什么?

编辑2:我在执行代码时没有出错,但是事件实体的字段没有更新&gt;&lt;'....我用invalidPluginExceptions测试了我的代码,代码的结尾是达到......这是代码:

编辑3:代码已更新...

1 个答案:

答案 0 :(得分:3)

要回答原始问题及其编辑,是的,当核心数据库操作尚未完成时,您无法将记录与其他记录相关联。

  

Pre-operation:管道中的阶段,用于在main之前执行的插件   系统运作。执行此阶段中注册的插件   在数据库事务中。

因此,要处理关联,您可以将阶段更改为后期操作,或者让一个IPlugin类处理预操作阶段,另一个处理一个或多个项目中的操作后阶段。

要回答编辑,查找字段属于班级EntityReference。 (看起来你正在使用1:N关系?)

要回答第二次修改,我在您的代码段中没有看到您将新EntityReference分配给目标Entity的位置。此外,您不必在操作前阶段向服务发出Update请求,因为尚未执行核心数据库操作。您只需将Entity的属性设置为等于您选择的值,此更改将被转移到数据库。

if (entity.Attributes.ContainsKey("new_lignecontrat"))
{
    entity.Attributes["new_lignecontrat"] = YourEntityReference;
}
else //attribute not included in the plugin operation
{
    entity.Attributes.Add("new_lignecontrat", YourEntityReference);
}

Microsoft在SDK中演示了这一概念:

\ sdk \ samplecode \ cs \ plug-ins \ accountnumberplugin.cs