You need to sign in to do that
Don't have an account?
sreee
error in batch apex.....
Hi,
i have 3 cust fields result1, result2, result3 fields in contact and total field in account, now i want to add 3 fields in con and display in account total...Plz help me...thanx in advance
global class batchcontactUpdate implements Database.Batchable<sObject> { global Database.QueryLocator start(Database.BatchableContext BC) { String query = 'SELECT result1__c,result2__c,result3__c FROM contact, total__c.account from account', //String query = 'SELECT totsl__c from account'; return Database.getQueryLocator(query); } global void execute(Database.BatchableContext BC, List<contact> scope) global void execute(Database.BatchableContext BC, List<account> scope1) { for(contact c : scope) { for( account a:scope1) { a.total__c= c.result1__c+c.result2__c+c.result3__c; } } global void finish(Database.BatchableContext BC) { } }
Try the following
global class batchcontactUpdate implements Database.Batchable<sObject>
{
public List<Account> accList = new List<Account>();
global Database.QueryLocator start(Database.BatchableContext BC)
{
String query = 'SELECT total__c,(select id,name,result1__c,result2__c,result3__c FROM contacts) from account',
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<account> scope)
{
for(Account a : scope)
{
for(Contact c : a.Contacts){
if(a.total__c != NULL){
a.total__c += c.result1__c + c.result2__c + c.result3__c;
}
else{
a.total__c = c.result1__c + c.result2__c + c.result3__c;
}
}
accList.add(a);
}
if(accList.size()>0){
update accList;
}
}
global void finish(Database.BatchableContext BC)
{
}
}
If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.
Thanks
Sree can you explain a bit more ?
Requirement Make 1 custom field on Account called "Total" (Integer). and 3 Custom fields on Contact Result1,Result2,Result3 with Integer as datatype. You are required to create scheduler class which will run every night at 1 am and calls batch class which sum the value of Result1,Result2 and Result3 fields of every Contact record and save the result in corresponding Account record field called "Total".
We need some modification in the class
Avi Iam Getting Following Error
Error: Compile Error: unexpected token: private at line 21 column 4
Sree!! there was a misplaced bracket have a close look
Corrected the same below