[英]c# - dynamics crm online plugin - use field value to populate attribute of related entity
I'm new to c# and plugins for dynamics. 我是C#和动态插件的新手。 In order to learn and test, I've successfully created a couple very simple plugins that worked.
为了学习和测试,我已经成功创建了几个非常简单的插件。 Now, I'm trying to get more into what I'll actually need to do with plugins -- I'm trying to get the value of a field on a custom entity, and use that value to update an attribute on a related custom entity.
现在,我试图深入了解我实际需要使用插件做什么—我试图获取自定义实体上字段的值,并使用该值来更新相关自定义上的属性实体。
My plugin is registered on the Update message of a custom entity (called new_registration). 我的插件已在自定义实体的更新消息(称为new_registration)上注册。 It runs post-operation, asynchronously.
它在手术后异步运行。 The field that updates and triggers the plugin (an option set "Status" field) is not used in the code anywhere.
更新和触发插件的字段(选项集“状态”字段)未在任何地方的代码中使用。
Here is my code: 这是我的代码:
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);
}
}
}
}
Through testing, I've narrowed down that this is failing (at least initially) on this line: 通过测试,我缩小了(至少在最初阶段)在此行上失败的范围:
string slot = (string)entity.Attributes["new_yourtimeslot"];
The error I'm getting in the plugin trace logs is: 我在插件跟踪日志中遇到的错误是:
The given key was not present in the dictionary.
给定的键在词典中不存在。
I've checked and double-checked, and I know that I'm getting the field name correct. 我已经检查并再次检查,并且我知道字段名称正确。 Am I doing something wrong in how I'm getting the value from the input parameters?
从输入参数获取值的方式上我做错了吗? Or am I messing something up I don't even realize I might be messing up?
还是我搞砸了,甚至没有意识到自己可能搞砸了? Any help is appreciated, thanks.
任何帮助表示赞赏,谢谢。
Always try to get the attribute value in a safe way (either check for the attribute in the attribute collection or use the SDK method like below). 始终尝试以安全的方式获取属性值(检查属性集合中的属性或使用如下所示的SDK方法)。 If an attribute value is null, the attribute is not returned as part of the attribute collection.
如果属性值为null,则该属性不会作为属性集合的一部分返回。
var slot = entity.GetAttributeValue<string>("new_yourtimeslot");
Following code snippet doesn't look correct 以下代码段看起来不正确
EntityReference eventclass = (EntityReference)entity.Attributes["new_eventregistrationrelationshipid"];
Attribute names and relationships names are seldom the same. 属性名称和关系名称很少相同。 Relationship names often include target entity and related entity and most often the attributes that end up being the lookups are named differently
new_eventregistrationid
maybe? 关系名称通常包括目标实体和相关实体,最常见的是最终成为查找的属性以不同的方式命名
new_eventregistrationid
? Double check the name by looking at Customization
- Field Properties
. 通过查看“
Customization
Field Properties
仔细检查名称。
Also, safely get the related attribute: 同样,安全地获取相关属性:
var eventclass = entity.GetAttributeValue<EntityReference>("new_eventregistrationid");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.