繁体   English   中英

c#-动态CRM在线插件-使用字段值填充相关实体的属性

[英]c# - dynamics crm online plugin - use field value to populate attribute of related entity

我是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"];

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

给定的键在词典中不存在。

我已经检查并再次检查,并且我知道字段名称正确。 从输入参数获取值的方式上我做错了吗? 还是我搞砸了,甚至没有意识到自己可能搞砸了? 任何帮助表示赞赏,谢谢。

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

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

以下代码段看起来不正确

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

属性名称和关系名称很少相同。 关系名称通常包括目标实体和相关实体,最常见的是最终成为查找的属性以不同的方式命名new_eventregistrationid 通过查看“ Customization Field Properties仔细检查名称。

同样,安全地获取相关属性:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM