2017-12-22 25 views
0

'name = SampleAccountOne'을 사용하여 20 개의 레코드를 생성했습니다. 내가 20 레코드에 주어진 연락처를 추가합니다 배치 클래스를 만들려고. 문법 및 어디로 갈지. 올바른 방향으로 도움을 주시면 대단히 감사하겠습니다.배치 클래스 계정에 연락처 추가

global class UpdateProjectNameBatch implementsDatabase.Batchable<sObject> { 
    List<Contact> conList = new conList<Contact>(); 
    String query = 'Select Id,Name FROM Account WHERE Name = \'SampleAccountOne\'' 

    global Database.QueryLocator start(Database.BatchableContext bc){ 
    return Database.getQueryLocator(query); 
    } 

    global void execute(Database.BatchableContext bc,List<sObject> batch){ 
    for(Contact c : conList){ 
     batch.c = 'New Contact Name'; 
    } 
    } 

    global void finish(){ 

    } 
} 

답변

0

여러 명의 계정으로 단일 연락처를 연결하려고합니다. 이 기능은 Spring 16'에서 릴리스되었으며 Salesforce Junction 개체 (AccountContactRelation)를 통해 구현됩니다.

UI를 통해이 기능을 이미 사용하도록 설정했으며이 연락처에 대한 상위 계정이 선언되어 있다고 가정합니다.

연락처를 모든 20 개의 계정에 연결하도록 코드를 수정했습니다.

global class UpdateProjectNameBatch implements Database.Batchable 
    <sObject> 
    { ID conId = '0037F00000IfCCKQA3'; //Add the contact Id here 
    List<AccountContactRelation> ListInsertion = new List<AccountContactRelation>(); 
    String query = 'Select Id,Name FROM Account WHERE Name = \'SampleAccountOne\''; 

    global Database.QueryLocator start(Database.BatchableContext bc) 
    { 
    return Database.getQueryLocator(query); 
    } 

    global void execute(Database.BatchableContext bc,List<Account> batch){ 

     for (Account a : batch) 
     { 
      AccountContactRelation c = new AccountContactRelation(); 

      c.AccountId = a.id; 
      c.contactId = conId; 
      ListInsertion.add(c); 
     } 


    Insert ListInsertion; 

    } 

    global void finish(Database.BatchableContext bc) 
    { 
    //DoNothing. 
    } 
}