You need to sign in to do that
Don't have an account?
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
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
I have to populate that field with the first contact in that account. The batch class is below.