将值设置为字段

时间:2011-12-13 09:44:29

标签: c# plugins dynamics-crm-2011

我创建了一个插件,用于计算与另一个实体相关的实体的记录数,如果有多个记录,则检索最后一条记录。如果该记录处于非活动状态且特定字段的值> 0,那么我将此字段的值添加到新创建的字段中...但无法使其工作...

任何帮助都会很棒!

修改 该插件已注册到“new_lignecontrat”,其中包含“new_unitesutilisees”和“new_unitesrestantes”属性。

编辑2:

好的解决了!我只需要为查找字段获取EntityReference并重新排列我的代码......

感谢您的帮助!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System.Web;
using System.Collections;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Messages;

namespace ClassLibrary1
{
    public class StatusContrat : IPlugin
    {         
        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 statusEntité = (Entity)context.InputParameters["Target"];

                    // Récupération des lignes de contrat liées au contrat courant
                FetchExpression fetch   = new FetchExpression("<fetch distinct='false' mapping='logical'>" +
               "<entity name='new_contrats'>" +
               "<link-entity name='new_lignecontrat' alias='nombreligne' from='new_contratsid' to='new_contratsid'>" +
               "</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.Count > 0)
                    {   
                        if (lines.Entities.Last().GetAttributeValue<OptionSetValue>("statecode").Value == 1)
                        {
                           if (lines.Entities.Last().GetAttributeValue<float>("new_unitesrestantes")<0)
                           {
                              var unitesRestantes = (statusEntité.GetAttributeValue<float>("new_unitesrestantes")) + (lines.Entities.Last().GetAttributeValue<float>("new_unitesrestantes"));
                              var unitesUtilisee =  (statusEntité.GetAttributeValue<float>("new_unitesutilisees")) - (lines.Entities.Last().GetAttributeValue<float>("new_unitesutilisees"));

                                statusEntité ["new_unitesutilisees"] = unitesUtilisee;
                                statusEntité ["new_unitesrestantes"] = unitesRestantes;

                                service.Update(statusEntité);
                           }
                        }
                    }
                    else
                    {

                        statusEntité["new_unitesutilisees"] = "0";
                        statusEntité["new_unitesrestantes"] = statusEntité["new_unitestotales"];

                        service.Update(statusEntité);
}                        

         }
      }
   }

2 个答案:

答案 0 :(得分:1)

有几点想法:

问:你确定你的插件正在被执行吗?
要检查这一点,您可以更改代码以在某处抛出InvalidPluginExecutionException,以便您知道它至少在执行。

问:您的插件是否正确注册?
检查您的插件是否已在“创建”消息和“预验证”或“预操作”步骤中注册。

问:您的插件是否沿着您期望的路径执行?
我建议你在服务器上执行一些调试(假设你正在做你的开发内部部署)

修改

我刚刚意识到您正在尝试使用服务调用更新目标实体。当插件需要更新该插件的主要实体上的字段时,它只需要在预验证或预操作阶段修改目标上的属性。

        Entity target = (Entity)context.InputParameters["Target"];

        // Récupération des lignes de contrat liées au contrat courant
        FetchExpression fetch = new FetchExpression(@"
            <fetch distinct='false' mapping='logical'>
              <entity name='new_contrats'>
                <link-entity name='new_lignecontrat' alias='nombreligne' from='new_contratsid' to='new_contratsid'>
                </link-entity>
              </entity>
            </fetch>");

        // Note: Do you need some attribute fields so that the entities are actually returning some relevant data
        // Note: Do you want to retrieve ALL the 'new_contrats' or should you be adding a condition in here to the primary entity?
        //  <filter type='and'>
        //    <condition attribute='MyIdFieldHere' operator='eq' value='" + context.PrimaryEntityId + "' /> 
        //  </filter>

        EntityCollection lines = service.RetrieveMultiple(fetch);

        // Vérification qu'il y a au moins une ligne de contrat associée
        if (lines.Entities.Any())
        {
            // store last entity in variable so that the collection is enumerabled 4 seperate times
            var last = lines.Entities.Last();
            if (last.GetAttributeValue<OptionSetValue>("statecode").Value == 1)
            {
                if (last.GetAttributeValue<float>("new_unitesrestantes") < 0)
                {
                    var unitesRestantes = (target.GetAttributeValue<float>("new_unitesrestantes")) + (last.GetAttributeValue<float>("new_unitesrestantes"));
                    var unitesUtilisee = (target.GetAttributeValue<float>("new_unitesutilisees")) - (last.GetAttributeValue<float>("new_unitesutilisees"));

                    target["new_unitesutilisees"] = unitesUtilisee;
                    target["new_unitesrestantes"] = unitesRestantes;
                }
            }
        }
        else
        {
            // if 'new_unitesutilisees' is a float, then the value must also be a float
            target["new_unitesutilisees"] = 0f; 
            target["new_unitesrestantes"] = target["new_unitestotales"];
        }     

编辑2:

另外,我假设有一个更好的获取查询来运行而不是检索所有实体,然后只使用最后一个。您是否可以通过调整查询来设置相反的顺序并仅检索第一个项目来缩小检索到的列表?根据系统中的实体数量,这个小优化将大大减少此插件的执行时间。

答案 1 :(得分:0)

如果这是您正在使用的代码,可能是因为您的FetchXML没有检索任何属性,尤其是属性new_unitesrestantesnew_unitesutilisees,因此检索的任何实体都是RetrieveMultiple请求没有这些属性的值。

<fetch distinct='false' mapping='logical'>
  <entity name='new_contrats'>
    <!-- no attributes listed here -->
    <link-entity name='new_lignecontrat' alias='nombreligne' from='new_contratsid' to='new_contratsid'>
      <!-- no attributes listed here either -->
    </link-entity>
  </entity>
</fetch>