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
Newbie_edmNewbie_edm 

Apex batch class to update lookup object

Dear Friends,

i got an requirement, I have teacher and student lookup relationships objects. Teacher object will have student related list, student related list will show student name and their total fees paid. I need to have custom field in Teacher object which will show total fees of all students uunder him.
i would like to use batch apex to update the total fees in Teached object based on student related list fees.

it would be great help if you could direct me to get the proper sample code.

Teacher Obect 
                  Fee total ( Sum ( all students fees under this teacher))


 Thanks in advance 
 
Best Answer chosen by Newbie_edm
Abhishek BansalAbhishek Bansal
Hi Venkat,

Please find your required code below:
global class BatchToUpdateTeacherRecords implements Database.Batchable<sObject>
{
    
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
       String query = 'Select Id, Total_Fees__c,(Select Total_Fees_Paid__c from Studets__r) from Contact';
       Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext BC, List<Contact> scope)
    {
       List<Contact> updateConList = new List<Contact>();
	   Decimal totalFees;
	   
	   for(Contact con : scope) {
	       totalFees = 0;
		   for(Student__c stud : con.Studets__r) {
		       totalFees = totalFees + stud.Total_Fees_Paid__c;
		   }
		   
		   con.Total_Fees__c = totalFees;
		   updateConList.add(con);
	   }
	   
	   if(updateConList.size() > 0) {
	       update updateConList;
	   }
    }
   
    global void finish(Database.BatchableContext BC) {
    }
}

//Please use the correct API name for the fields Total_Fees__c and Total_Fees_Paid__c
//Please use the correct API name for the object Student__c
//Please use correct child relationship name for the Studets__r


Please take of the comments provided at the last of the class and also take care of any syntax error. Let me knwo in case you need any other help on this.

Thanks,
Abhishek Bansal.

All Answers

Abhishek BansalAbhishek Bansal
Hi Venkat,

Please find your required code below:
global class BatchToUpdateTeacherRecords implements Database.Batchable<sObject>
{
    
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
       String query = 'Select Id, Total_Fees__c,(Select Total_Fees_Paid__c from Studets__r) from Contact';
       Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext BC, List<Contact> scope)
    {
       List<Contact> updateConList = new List<Contact>();
	   Decimal totalFees;
	   
	   for(Contact con : scope) {
	       totalFees = 0;
		   for(Student__c stud : con.Studets__r) {
		       totalFees = totalFees + stud.Total_Fees_Paid__c;
		   }
		   
		   con.Total_Fees__c = totalFees;
		   updateConList.add(con);
	   }
	   
	   if(updateConList.size() > 0) {
	       update updateConList;
	   }
    }
   
    global void finish(Database.BatchableContext BC) {
    }
}

//Please use the correct API name for the fields Total_Fees__c and Total_Fees_Paid__c
//Please use the correct API name for the object Student__c
//Please use correct child relationship name for the Studets__r


Please take of the comments provided at the last of the class and also take care of any syntax error. Let me knwo in case you need any other help on this.

Thanks,
Abhishek Bansal.

This was selected as the best answer
Newbie_edmNewbie_edm
Thanks Abhishek. It worked. If possible please send me test class, I am new to development.
Abhishek BansalAbhishek Bansal

Hi Venkat,

Please take some help from the link below to write your test class:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm

If this doesn't help then please open a new question for test class.

Thanks,
Abhishek Bansal.