CRM插件C#,如何根据事后的另一个查找字段设置查找字段



我有一个自定义实体学生,每个学生都有一个部门,每个部门都属于校园(部门和校园是查找领域(。

我要做的是创建一个新帐户并为他选择一个部门。

然后,插件根据选定的部门更改校园。

这是我的代码,有人可以向我解释我需要做的哪个步骤。

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

我建议您在创建/更新前进行此操作,因此您可以在目标本身中设置校园属性,这将避免另一个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");
}

最新更新