c# - 为字段设置值

标签 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é);
}                        

         }
      }
   }

最佳答案

几个想法:

问:您确定您的插件正在执行吗?
要检查这一点,您可以更改代码以在某处抛出 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:

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

关于c# - 为字段设置值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8487065/

相关文章:

css - 如何在页面顶部显示特定标签?

dynamics-crm-2011 - 创建 "Split Button"

c# - 是否可以在没有 SDK 的情况下调用 Dynamics CRM 2011 后期绑定(bind) WCF 组织服务 - 直接自定义绑定(bind)?

dynamics-crm-2011 - 动态 CRM 2016 中电子邮件表单上的发件人字段锁定

css - 如何压缩 .ttf 字体文件大小

c# - 无法从 JSON 响应创建 javascript 数组

c# - 模拟 Networkstream.Read

c# - XNA 游戏中的分辨率

c# - 使用状态和工厂设计模式管理状态和验证规则

java - 用于热类重载的各种Java插件之间有什么区别,哪个最直观?