+ Start a Discussion
sreeesreee 

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)
    {
    }
}

 

souvik9086souvik9086

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

Avidev9Avidev9

Sree can you explain a bit more ?

  • Do you want to sum up these three fields to account, for every related contact ?
  • How many contacts we can have per account ?
sreeesreee
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".
Avidev9Avidev9

We need some modification in the class

 

global class batchcontactUpdate implements Database.Batchable < sObject > {
    public List < Account > accList = new List < Account > ();
    global Database.QueryLocator start(Database.BatchableContext BC) {
        String query = 'SELECT Id,total__c,(SELECT id,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) {
                Double total =0;
for (Contact c: a.Contacts) { total = total + replaceNullByZero(c.result1__c) + replaceNullByZero(c.result2__c) + replaceNullByZero(c.result3__c); } a.total__c = total; } update scope; } global void finish(Database.BatchableContext BC) {} }

private static Double replaceNullByZero(Double num){
return num == null ? 0 : num;
}

 

sreeesreee

Avi Iam Getting Following Error

Error: Compile Error: unexpected token: private at line 21 column 4

Avidev9Avidev9

Sree!! there was a misplaced bracket have a close look

 Corrected the same below

global class batchcontactUpdate implements Database.Batchable < sObject > {
    public List < Account > accList = new List < Account > ();
    global Database.QueryLocator start(Database.BatchableContext BC) {
        String query = 'SELECT Id,total__c,(SELECT id,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) {
                Double total =0;
                for (Contact c: a.Contacts) {
                    total = total + replaceNullByZero(c.result1__c) + replaceNullByZero(c.result2__c) + replaceNullByZero(c.result3__c);
                }
                a.total__c = total;
        }
        update scope;
    }
    global void finish(Database.BatchableContext BC) {}
    
   
    private static Double replaceNullByZero(Double num){
         return num == null ? 0 : num;
    }
}