function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Subodh shuklaSubodh shukla 

Hi, In my batch class i have to populate Account Object field Account_Field__c with contact id when these field(field Account_Field__c) is null. I have tried below piece of code but it is not updating the field.


List contactList = new List();    
List accountList = new List();

accountList = [Select Id,Account_Field__c from Account]; 
contactList = [Select Id from contact where AccountId in :accountList];

for(Account a:accountList){
              if(a.Account_Field__c == null)
                  for(Contact c:cntList){
                  a.Account_Field__c = c.Id;
                  accountList.add(a);
                  }
              }
          }
update accountList;
Thanks in advance
Vishal_GuptaVishal_Gupta
Hi Subodh,

Piece of code which you shared is not correct, first issue is that you should only fetch those accounts in which Account_Field__c equal to null like :
accountList = [Select Id,Account_Field__c from Account where Account_Field__c = null]; 

Second issue is that you are keeping contact loop in account loop and setting same account field a.Account_Field__c = c.Id; multiple times.

Please share your complete batch class and let us know what is your use case means which contact you want to set in  a.Account_Field__c because there might me more than one contact related to account.

Thanks
Vishal
 
Subodh shuklaSubodh shukla
Hi Vishal,

I have to populate that field with the first contact in that account. The batch class is below.
public class AccountContactBatch implements Database.Batchable<sObject>{

        String qry;
        public Database.QueryLocator start(Database.BatchableContext BC){
            qry= 'Select Name,O__c,S__c,E__c,F_N__c,L_N__c,P__c,C__c,A__c,Ad__c,ct__c,cmn__c,S_P__c,Z_P_C__c,cnt__c,J_T__c,E_I__c,Ip_Ad__c,Ot__c,R_C_S__c ,R_C_B__c ,R_L_P_A__c ,R_D__c,Sub__c,In__c,Src__c,M__c,F__c,M_S__c,G__c,T_G__c,o_s__c,o_S_L_U__c,Atnd__c,I_R__c,J_T__c,L_T__c,T_I_S__c from R__c where S__c=null or S__c=\'N\'';
            return Database.getQueryLocator(qry);
           
        }
      
       public void execute(Database.BatchableContext BC, List<R__c> scope){
          List<Contact> contactList = new List<Contact>();
          List<Account> acctList = new List<Account>();
          
          List<Contact> cntList = new List<Contact>();
          List<Account> accountList = new List<Account>();
           
          set<String> EList = New set<String>();
          set<String> FNList = New set<String>();
          set<String> LNList = New set<String>();
          set<String> ANList = New set<String>();
          map<string,Account> ANMap = new map<string,Account>();
          map<string,Contact> CEMap = new map<string,Contact>();
          map<string,Contact> CNMap = new map<string,Contact>();
    
          for(R__c r :scope){
              if(r.E__c!=null)EList.add(r.E__c);
              FNList.add(r.F_N__c);
              LNList.add(r.L_N__c);
              ANList.add(r.O__c);
          }
          
          contactList = [select Id,FirstName,LastName,Name,Email,Account.name,Phone,Description,MailingStreet,MailingCity,MailingState,MailingPostalCode,MailingCountry,Title  from Contact where (Email In :EList or  (FirstName In :FNList and lastName In :LNList))];
          acctList = [select Id,Name,Description,BillingStreet,BillingCity,BillingState,BillingPostalCode,BillingCountry from Account where (Name  In :ANList)];
     
          accountList = [Select Id,Account_Field__c from Account where Account_Field__c =null];  
          cntList = [Select Id, Name from contact where AccountId in :accountList];
          
          for(Account a:acctList ){
              ANMap.put(a.Name,a);   
          }
          for(Account a:accountList){
                  for(Contact c:cntList){
                  a.Account_Field__c = c.Name;
                  accountList.add(a);
                  } 
          }
          update accountList;
         
          for(Contact c:contactList ){
              CEMap.put(c.Email,c);  
              CNMap.put(c.Name,c);         
          }

          Map<String,Contact> createContact = new Map<String,Contact>();
          Map<String,Account> createAccount = new Map<String,Account>();
          Map<String,String> NEMap = new Map<String,String>();
          
          for(R__c r :scope){
              
              Boolean accountCreated=false;
              Boolean contactCreated=false; 
            
              if(CEMap.containsKey(r.E__c) || CNMap.containsKey(r.F_N__c+' '+r.L_N__c)){
                  r.S__c='Pending';
                  continue;
              }
              
              String aName = r.O__c!=null ? r.O__c :r.F_N__c+' '+r.L_N__c;
              Account a = ANMap.get(aName);
              Contact c =new Contact();
            
              accountContactCtrl.mapContact(c,r);
              contactCreated = true;
              createContact.put(r.E__c,c);  

              CEMap.put(r.E__c,c); 
              String cName = r.F_N__c+' '+r.L_N__c; 
              CNMap.put(cName,c);    
                
              if(a!=null){                  
                  c.AccountId=a.Id;
              }
              
              if(a==null){
                  a = new Account();                  
                  accountContactCtrl.mapAccount(a,r);                                
                  createAccount.put(aName,a);  
                  NEMap.put(r.E__c,aName);                               
                  accountCreated=true; 

                   ANMap.put(aName,a);
              }
              
              r.S__c= (contactCreated || accountCreated)?'Processed':'Pending';
          }
         
          List<Database.SaveResult> results = Database.Insert(createAccount.values(), false);   
       
          Set<String> excption = new Set<String>();
          if(results != null){
           List<Account> accList = new  List<Account>();
           integer i = 0; 
           for(Database.SaveResult accResult: results){
               if (!accResult.isSuccess()) {
                    Database.Error error = accResult.getErrors().get(0);
                    
                    if(error.getMessage().contains('data value too large')){
                       
                        List<String> failed = error.getFields();
                        accList = createAccount.values();
                        
                        excption.add(accList[i].Name);
                        
                        accList.remove(i);
                     }
                  }
                  i = i + 1;
              }
            
              Database.Insert(accList, false);  
          }
        
          for(String email:NEMap.keySet()){
              if (NEMap.get(email) !=null){
                  createContact.get(email).accountid=createAccount.get(NEMap.get(email)).id;                     
              }             
          }
          
          
          List<Database.SaveResult> cresults = Database.Insert(createContact.values(), false);   
          
          if(cresults != null){   
           List<Contact> conList = new List<Contact>(); 
           integer i = 0;
           for(Database.SaveResult conResult: cresults){
                 if (!conResult.isSuccess()) {
                    Database.Error error = conResult.getErrors().get(0);
                  
                    if(error.getMessage().contains('data value too large')){
                       
                        List<String> failed = error.getFields();
                        conList = createContact.values();
                        excption.add(conList[i].FirstName + ' ' + conList[i].LastName);
                      
                        conList.remove(i);
                     }
                   }
                   i = i + 1;
            }
            Database.Insert(conList, false); 
          }
          
          for(R__c r :scope){
              String aName = r.O__c!=null ? r.O__c :r.F_N__c+' '+r.L_N__c;
              String regName = r.F_N__c + ' ' + r.L_N__c;
              if (createAccount.get(aName) !=null){
                  r.A__c=createAccount.get(aName).Id;
              }
              if (createContact.get(r.E__c) !=null){
                  r.C__c=createContact.get(r.E__c).Id;   
              }
              if(regName != '' && excption.contains(regName)){
                    r.S__c = 'Error';
              }                 
             
          }

          update scope;

       }
       
       public void finish(Database.BatchableContext BC){
            
       }
}