C# Dynamics CRM中的change company插件
我需要为Dynamics CRM Online创建插件,在编辑联系人字段后随机更改公司名称。 正如我所知,我需要更改parentcustomerid字段,但我不知道如何获取所有客户IDа并在插件代码中分配其中一个 我的代码:C# Dynamics CRM中的change company插件,c#,plugins,dynamics-crm,C#,Plugins,Dynamics Crm,我需要为Dynamics CRM Online创建插件,在编辑联系人字段后随机更改公司名称。 正如我所知,我需要更改parentcustomerid字段,但我不知道如何获取所有客户IDа并在插件代码中分配其中一个 我的代码: public class ContactPlugin: IPlugin { public void Execute(IServiceProvider serviceProvider) { IPluginExecutionContext con
public class ContactPlugin: IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
if (context.InputParameters.Contains("Target") &&
context.InputParameters["Target"] is Entity)
{
Entity entity = (Entity)context.InputParameters["Target"];
if (entity.LogicalName == "contact")
{
//entity.Attributes.Add("parentcustomerid", GetNewParentCustomerId());
}
}
}
}
如何才能做到这一点?首先,编写一个函数从CRM检索所有帐户:
static List<Entity> GetAllAccounts(IOrganizationService service)
{
var query = new QueryExpression { EntityName = "account", ColumnSet = new ColumnSet(false) };
var accounts = service.RetrieveMultiple(query).Entities.ToList();
return accounts;
}
然后编写一个函数,可以从列表中检索随机项:
static Entity GetRandomItemFromList(List<Entity> list)
{
var r = random.Next(list.Count);
return list[r];
}
然后将randomAccount
实体转换为EntityReference,并将该值分配给联系人的parentcustomerid
属性:
entity.Attributes.Add("parentcustomerid", new randomAccount.ToEntityReference());
要更新联系人并将值插入数据库,请使用service.update(entity)
。但是,如果您计划在代码中立即调用Update
,我建议您重新设置联系人,以便在数据库中只更新parentcustomerid
属性:
var updatedContact = new Entity { Id = entity.Id, LogicalName = entity.LogicalName };
updatedContact.Attributes.Add("parentcustomerid", new randomAccount.ToEntityReference());
首先,编写一个函数从CRM检索所有帐户:
static List<Entity> GetAllAccounts(IOrganizationService service)
{
var query = new QueryExpression { EntityName = "account", ColumnSet = new ColumnSet(false) };
var accounts = service.RetrieveMultiple(query).Entities.ToList();
return accounts;
}
然后编写一个函数,可以从列表中检索随机项:
static Entity GetRandomItemFromList(List<Entity> list)
{
var r = random.Next(list.Count);
return list[r];
}
然后将randomAccount
实体转换为EntityReference,并将该值分配给联系人的parentcustomerid
属性:
entity.Attributes.Add("parentcustomerid", new randomAccount.ToEntityReference());
要更新联系人并将值插入数据库,请使用service.update(entity)
。但是,如果您计划在代码中立即调用Update
,我建议您重新设置联系人,以便在数据库中只更新parentcustomerid
属性:
var updatedContact = new Entity { Id = entity.Id, LogicalName = entity.LogicalName };
updatedContact.Attributes.Add("parentcustomerid", new randomAccount.ToEntityReference());
谢谢你的帮助,我做了我想做的。谢谢你的帮助,我做了我想做的。