【问题标题】:c# - dynamics crm online plugin - use field value to populate attribute of related entityc# - 动态 crm 在线插件 - 使用字段值填充相关实体的属性
【发布时间】:2016-10-31 21:17:12
【问题描述】:

我是 C# 和动态插件的新手。为了学习和测试,我成功地创建了几个非常简单的插件。现在,我正在尝试更多地了解我实际上需要对插件执行的操作——我正在尝试获取自定义实体上的字段值,并使用该值来更新相关自定义的属性实体。

我的插件在自定义实体的更新消息上注册(称为 new_registration)。它在操作后异步运行。更新和触发插件的字段(选项集“状态”字段)不在任何地方的代码中使用。

这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.ServiceModel;
using Microsoft.Xrm.Sdk;

namespace PlugInTests
{
    public class AdjustTimeSlots: IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            //Extract the tracing service for use in debugging sandboxed plug-ins.
            ITracingService tracingService =
                (ITracingService)serviceProvider.GetService(typeof(ITracingService));

            // Obtain the execution context from the service provider.
            IPluginExecutionContext context = (IPluginExecutionContext)
                serviceProvider.GetService(typeof(IPluginExecutionContext));

            if (context.InputParameters != null)
            {
                Entity entity = (Entity)context.InputParameters["Target"];
                Guid id = entity.Id;
                tracingService.Trace("got input parameters");

                //get time slot
                string slot = (string)entity.Attributes["new_yourtimeslot"];
                EntityReference eventclass = (EntityReference)entity.Attributes["new_eventregistrationrelationshipid"];
                tracingService.Trace("got time slot");

                //set updated entity (event/class)
                Entity parentevent = new Entity("new_eventclass");
                parentevent.Id = eventclass.Id;
                parentevent.Attributes["new_timeslotsfordelete"] = slot;


                // Obtain the organization service reference.
                IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

                //update event record
                tracingService.Trace("Update time slot plugin");
                service.Update(parentevent);
            }
        }
    }
}

通过测试,我已经缩小了这一行(至少最初是这样)失败的范围:

string slot = (string)entity.Attributes["new_yourtimeslot"];

我在插件跟踪日志中遇到的错误是:

给定的键不在字典中。

我已经检查并再次检查,我知道我得到的字段名称是正确的。我在如何从输入参数中获取值方面做错了吗?还是我把事情搞砸了,我什至没有意识到我可能搞砸了?任何帮助表示赞赏,谢谢。

【问题讨论】:

    标签: c# plugins crm microsoft-dynamics entities


    【解决方案1】:

    始终尝试以安全的方式获取属性值(检查属性集合中的属性或使用如下所示的 SDK 方法)。如果属性值为 null,则该属性不会作为属性集合的一部分返回。

    var slot = entity.GetAttributeValue<string>("new_yourtimeslot");
    

    以下代码 sn-p 看起来不正确

    EntityReference eventclass = (EntityReference)entity.Attributes["new_eventregistrationrelationshipid"];
    

    属性名称和关系名称很少相同。关系名称通常包括目标实体和相关实体,并且大多数情况下最终成为查找的属性名称不同new_eventregistrationid 可能吗?通过查看 Customization - Field Properties 仔细检查名称。

    另外,安全获取相关属性:

    var eventclass = entity.GetAttributeValue<EntityReference>("new_eventregistrationid");
    

    【讨论】:

    • 谢谢。那非常有帮助。获得这些属性值的调整奏效了。您对该语法的参考在哪里?我一直在阅读 msdn 文档,但我不相信我见过这种语法?我可能会将其与语法过时的旧在线示例混淆。感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2012-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-15
    • 1970-01-01
    • 1970-01-01
    • 2011-09-12
    相关资源
    最近更新 更多