Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Dynamics CRM中的change company插件_C#_Plugins_Dynamics Crm - Fatal编程技术网

C# Dynamics CRM中的change company插件

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

我需要为Dynamics CRM Online创建插件,在编辑联系人字段后随机更改公司名称。 正如我所知,我需要更改parentcustomerid字段,但我不知道如何获取所有客户IDа并在插件代码中分配其中一个

我的代码:

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());

谢谢你的帮助,我做了我想做的。谢谢你的帮助,我做了我想做的。