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 

update fields in custom object using Batch class

Hi,

I want to update the fields in a custom object based on the condition using the Batch class. 
I have written a code on batch class but I'm unable to update the fields.

Below is the code which I have worked, 

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 where id!=null');
    }
    
    Global void execute(database.BatchableContext BC, List<Trainer_Master__c> Scope){
        for (Trainer_Master__c trainer : Scope){
            if(trainer.Background_Check_Done__c =='No' && trainer.LinkedIn_Profile__c == null){
                trainer.Verification_Status__c = 'Details Needed';
            }
            else if(trainer.Background_Check_Done__c =='No' && trainer.LinkedIn_Profile__c!= null){
                trainer.Verification_Status__c = 'Non Verified';
            }
            else if(trainer.Background_Check_Done__c =='Yes' && trainer.LinkedIn_Profile__c!= null){
                trainer.Verification_Status__c = 'Verified';
            }
        }
        system.debug('verification status' + scope);
        Database.update(Scope);
    }
    Global void finish(database.BatchableContext BC){
        
    }
}

Kindly Help me to fix the code.

Thank You..!!

Regards,
Sanjay Vinayak T
Best Answer chosen by Sanjay Vinayak T
CharuDuttCharuDutt
Hii Sanjay
Try Below Class 
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);
Please Mark It As Best Answer If It Helps
Thank You!

All Answers

CharuDuttCharuDutt
Hii Sanjay
Can You Please Tell What Error You Are Facing
Sanjay Vinayak TSanjay Vinayak T
Hi CharuDutt,
I want to update the Verification status field as mentioned in the above code, but after calling the batch class from the anonymous window Verification status field is not getting updated and there are no errors in the code.
AnkaiahAnkaiah (Salesforce Developers) 
Hi Sanjay,

Have you executed the batch class?

Thanks!!
Sanjay Vinayak TSanjay Vinayak T
Hi Ankaiah,

I have executed the Batch class using an anonymous window.
User-added image
but unable to update the fields.
below are the debug logs for limit usage after the above execution.
User-added image

Thanks!!
CharuDuttCharuDutt
Hii Sanjay
Try Below Class 
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);
Please Mark It As Best Answer If It Helps
Thank You!
This was selected as the best answer
Sanjay Vinayak TSanjay Vinayak T
Hi CharuDutt,
Thanks for fixing the code. it's working as per expectations.

Regards,
Sanjay Vinayak T
Sanjay Vinayak TSanjay Vinayak T
Hi CharuDutt,

How to get the count of the total number of records with verification status as NonVerified?

Thanks..!!
Shekar S 4Shekar S 4
Hey Sanjay.. 
I too got same issue regards this.. no error but field won't update.. for this go to setup >> apex jobs 
In the apex jobs u wil find custom field  VALIDATION EXCEPTION error due to validation in ur object.. 
Just update the code DATABASE.UPDATE(Scope, false);
This wil neglect validation rule and update ur batch class or scheduled class perfectly.
Thank u .