CRM 插件 C#,如何根据 post-event 中的另一个查找字段设置查找字段?

CRM Plug-in C#, How to set lookup field according to another lookup field in post-event?

我有一个自定义实体学生,每个学生有一个系,每个系属于一个校区(系和校区是查找字段)。

我正在尝试做的是创建一个新帐户并为他选择一个部门。

然后插件会根据选择的院系更改校区。

这是我的代码,有人可以向我解释一下我需要执行哪些步骤吗?

        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");
        }

我建议你在 create/update 的前期活动中这样做,因此你可以在目标本身中设置校园属性,这将避免另一个 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");

}