2017-05-14 9 views
0

Dynamics CRM Onlineのプラグインを作成する必要があります。連絡先のフィールドを編集した後、会社名をランダムに変更します。 私はparentcustomeridフィールドを変更する必要があると知っていますが、私はすべての顧客IDを取得し、プラグインのコードでそれらのいずれかを割り当てる方法を知らない。Dynamics CRMの変更会社のプラグイン

マイコード:

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

私はそれをどのように行うことができますか?

答えて

3

まず、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; 
} 

その後、あなたのプラグインクラスの最上部にRandomクラスのインスタンスを格納します。

static Random random = new Random(); 

そして、どの関数を書きますリストからランダムな項目を取得できます。

static Entity GetRandomItemFromList(List<Entity> list) 
{ 
    var r = random.Next(list.Count); 
    return list[r]; 
} 

var accounts = GetAllAccounts(service); 
var randomAccount = GetRandomItemFromList(accounts); 

その後のEntityReferenceにrandomAccountエンティティを変換し、連絡先の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()); 
+0

は私が望んでいた、助けてくれてありがとう。 – AlxZahar

関連する問題