【问题标题】:CRM Plug-in C#, How to set lookup field according to another lookup field in post-event?CRM Plug-in C#, 如何在事后根据另一个查找字段设置查找字段?
【发布时间】:2018-04-23 12:16:54
【问题描述】:
我有一个自定义实体student,每个学生都有一个系,每个系都属于一个校区(系和校区是查找字段)。
我要做的是创建一个新帐户并为他选择一个部门。
然后插件根据选择的部门改变校区。
这是我的代码,谁能解释一下我需要执行哪些步骤。
var context = serviceProvider.GetService(typeof(IPluginExecutionContext)) as IPluginExecutionContext;
Entity student = context.InputParameters["Target"] as Entity;
string Department = string.Empty;
if (student.Contains("karam_department"))
{
Department = student.GetAttributeValue<>("karam_department");
}
【问题讨论】:
标签:
c#
plugins
dynamics-crm
crm
xrm
【解决方案1】:
我建议您在创建/更新之前执行此操作,因此您可以在目标本身中设置校园属性,这将避免另一个 service.Update。您只需从所选部门查询相应的校区,然后在目标实体中设置。
var context = serviceProvider.GetService(typeof(IPluginExecutionContext)) as IPluginExecutionContext;
Entity student = context.InputParameters["Target"] as Entity;
//get the department from target
EntityReference department = student.GetAttributeValue<EntityReference>("karam_department");
if (department != null)
{
//retrieve the campus from department
Entity deptEntity = service.Retrieve("karam_department", Department.Id, new ColumnSet("karam_campus"));
//set the campus attribute in target itself
student["karam_campus"] = deptEntity.GetAttributeValue<EntityReference>("karam_campus");
}