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
Sanjay Vinayak TSanjay Vinayak T 

How to count the Number of records updated from Batch class?

Hi All,

I have created a batch class to update the fields. 
i want the count of the field updated based on the condition.
Global class BatchUpdateTrainer implements Database.Batchable<Sobject> {
    
    
    Global database.QueryLocator start(database.BatchableContext BC){
        return Database.getQueryLocator('Select id, Background_Check_Done__c, LinkedIn_Profile__c, Verification_Status__c from Trainer_Master__c);
    }
    
    Global void execute(database.BatchableContext BC, List<Trainer_Master__c> Scope){
	list<Trainer_Master__c> lstTM = new list<Trainer_Master__c>();
        for (Trainer_Master__c trainer : Scope){
		system.debug('Background Check Done >>>> ' + trainer.Background_Check_Done__c);
		system.debug('LinkedIn Profile >>>> ' + trainer.LinkedIn_Profile__c);
            if(trainer.Background_Check_Done__c =='No' && trainer.LinkedIn_Profile__c == null){
                trainer.Verification_Status__c = 'Details Needed';
				system.debug('Verification_Status__c1 ' + trainer.Verification_Status__c);
            }
            else if(trainer.Background_Check_Done__c =='No' && trainer.LinkedIn_Profile__c!= null){
                trainer.Verification_Status__c = 'Non Verified';
				system.debug('Verification_Status__c2 ' + trainer.Verification_Status__c);
            }
            else if(trainer.Background_Check_Done__c =='Yes' && trainer.LinkedIn_Profile__c!= null){
                trainer.Verification_Status__c = 'Verified';
				system.debug('Verification_Status__c3 ' + trainer.Verification_Status__c);
            }
			lstTM.add(trainer);
        }
        
        if(!lstTM.IsEmpty()){
		update lstTM;
		}
    }
    Global void finish(database.BatchableContext BC){
        
    }
}


TO Execute
BatchUpdateTrainer M = new BatchUpdateTrainer (); 
Database.executeBatch(M);

Above is my Batch class code and I want to count the number of records with the Verification_Status__c = 'Non Verified'.

Kindly help me in enhancing the code.

Thanks in advance.

Regards,
Sanjay Vinayak T
Best Answer chosen by Sanjay Vinayak T

All Answers

Sanjay Vinayak TSanjay Vinayak T
Hi Amol Wagh,
Thanks for sharing the reference answer, problem got solved.

will you be able to help with the below-mentioned problem?

I wanted to show a custom error message on the Visualforce page for the below code.

Kindly help me in fixing the code and show the error message.
Trigger: 
trigger StudentRecordTrigger on Student_Details__c (after insert) {
    if(Trigger.isinsert || Trigger.isafter ){
        StudentRecordTriggerHandler.checkStudentStatus(Trigger.new);
    }
}

-----------------------------------------------------------------------------------------------

Trigger HandlerClass:

public class StudentRecordTriggerHandler {
    List<Student_Master__c> studentToUpdate {get; set;}
    public static void checkStudentStatus(List<Student_Master__c> newStudent){
        try{
            Set<Id> StuId = new Set<id>();
            
            if(newStudent!=null){
                for(Student_Master__c stu : newStudent){
                    StuId.add(stu.Id);
                }
            }
            
            List<Student_Master__c> studentToUpdate = new List<Student_Master__c>();
            studentToUpdate = [Select id, Background_Check_Status__c, PAN_Card_Number__c, Phone_Number__c from Student_Master__c where id in : StuId];
            
            if(StuId!=null){
                List<Black_Listed_Candidate__c> blackListedStu = [Select id from Black_Listed_Candidate__c where PAN__c = :studentToUpdate[0].PAN_Card_Number__c limit 1];
                if(blackListedStu!=null){
                    blackListedStu[0].Phone__c = studentToUpdate[0].Phone_Number__c;
                    update blackListedStu;
                    studentToUpdate[0].Background_Check_Status__c = 'Candidate is blacklisted! We cannot Hire.';
                    
                    update studentToUpdate;
                }
              if (studentToUpdate[0].Background_Check_Status__c !=null){
                  ApexPages.addmessage(new apexPages.Message(ApexPages.severity.WARNING,'<h1 style="color : red;"> Student is blacklisted.'));
              }  
            }
        }
        catch(Exception e){
            system.debug('failed to update');
        }
        
    }
}

--------------------------------------------------------------

Visualforce page:
<apex:page Controller="StudentRecordTriggerHandler">
  <apex:form >
   <apex:pageBlock >
      <apex:pageMessages id="Message" escape="False"></apex:pageMessages>
    </apex:pageBlock>
     </apex:form>
</apex:page>



Regards,
Sanjay.