C# 如何在创建实体记录时插入自定义字段的值?
我面临的问题是如何通过创建新的systemuser来插入自定义字段值 在MicrosoftCRM系统中,有systemuser表,我有一个名为“SSOID”的自定义字段,“SSOID”的值是从webapi获得的 我的要求是,当我在CRM的系统用户创建页面中时,我在domainname文本框中输入domainname值,其他字段(如firstname、lastname和businessID)将根据domainname自动填充。它们来自active directory 同时,我还调用webapi来获取基于域名的SSOID 我想要的是,在单击“保存”按钮后,应该创建新的systemuser,并且SSOID也应该与新的systemuser一起插入 但是现在当我点击按钮时,发生了一些错误 Id为5ab7eb74-511a-ea11-810b-005056ba5450的用户设置不存在 下面是我的代码,看起来很简单,但是发生了一些错误C# 如何在创建实体记录时插入自定义字段的值?,c#,plugins,dynamics-crm,crm,C#,Plugins,Dynamics Crm,Crm,我面临的问题是如何通过创建新的systemuser来插入自定义字段值 在MicrosoftCRM系统中,有systemuser表,我有一个名为“SSOID”的自定义字段,“SSOID”的值是从webapi获得的 我的要求是,当我在CRM的系统用户创建页面中时,我在domainname文本框中输入domainname值,其他字段(如firstname、lastname和businessID)将根据domainname自动填充。它们来自active directory 同时,我还调用webapi来获
IPluginExecutionContext context;
IOrganizationServiceFactory factory;
IOrganizationService service;
// 获取执行上下文
context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
// 获取服务工厂
factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
// 获取服务
service = factory.CreateOrganizationService(context.UserId);
if (context.MessageName.ToLower() == "create"
&& context.InputParameters.Contains("Target")
&& context.InputParameters["Target"] is Entity)
{
try
{
var domainName = "";
Entity entity = (Entity)context.InputParameters["Target"];
if (context.Depth > 1)
{ return; }
if (entity.Attributes.Contains("domainname"))
{
domainName = entity["domainname"].ToString();
}
var SSOId = " get from webapi";
if (!string.IsNullOrEmpty(SSOId))
{
var businessEntityRef = (Microsoft.Xrm.Sdk.EntityReference)(entity.Attributes["businessunitid"]);
var businessunit = service.Retrieve(businessEntityRef.LogicalName, businessEntityRef.Id, new ColumnSet(true));
entity["businessunitid"] = businessEntityRef; // businessunit id is look up field comes from businessunit reference table
entity["domainname"] = entity["domainname"];
entity["lastname"] = entity["lastname"];
entity["firstname"] = entity["firstname"];
entity["new_ssoid"] = SSOId;
service.Update(entity); //i can't use create method, it shows "The specified Active Directory user already exists as a Dynamics 365 user"
}
else
{
throw new InvalidPluginExecutionException("userID not exist");
}
}
catch (Exception ex)
{
throw new InvalidPluginExecutionException(ex.Message);
}
}
我是CRM插件开发新手,谁能指导我如何解决此问题?请使用以下代码:
var SSOId = " get from webapi";
if (!string.IsNullOrEmpty(SSOId))
{
Entity entityToUpdate = new Entity("systemuser", new Guid(entity.Id));
entityToUpdate["new_ssoid"] = SSOId;
service.Update(entityToUpdate);
}
else
{
throw new InvalidPluginExecutionException("userID not exist");
}
您需要创建一个实体对象的新实例,以便在记录创建后更新(创建后异步插件)
此外,如果在没有错误时抛出异常,事务将回滚。仔细想想。请使用以下代码:
var SSOId = " get from webapi";
if (!string.IsNullOrEmpty(SSOId))
{
Entity entityToUpdate = new Entity("systemuser", new Guid(entity.Id));
entityToUpdate["new_ssoid"] = SSOId;
service.Update(entityToUpdate);
}
else
{
throw new InvalidPluginExecutionException("userID not exist");
}
您需要创建一个实体对象的新实例,以便在记录创建后更新(创建后异步插件)
此外,如果在没有错误时抛出异常,事务将回滚。想想看。你的代码不是这样可读的,请将其格式化为describerd…你的代码不是这样可读的,请将其格式化为describerd…太棒了,它对我有用,非常感谢Arun。此外,我不明白如果我选择同步,它将不工作。@iven.liu有时,当我们再次更新同一记录时,执行管道会在同步后期创建时出错(可能是由于内部插件)。异步将工作。顺便说一句,你可以做预创建插件来拦截创建目标记录并在单个事务中设置属性,而无需在创建后进行更新。谢谢你的解释,这有助于我更好地理解。太棒了,它对我有用,非常感谢阿伦。此外,我不明白如果我选择同步,它将不工作。@iven.liu有时,当我们再次更新同一记录时,执行管道会在同步后期创建时出错(可能是由于内部插件)。异步将工作。顺便说一句,你可以做预创建插件来截获创建目标记录并在单个事务中设置属性,而无需在创建后进行更新。谢谢你的解释,这有助于我更好地理解。