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
shrayas reddyshrayas reddy 

batch apex - I have to update my candidate status feild to accepted if any one of the job application status related to candidate is Accepted then his candidate status should be accepted anything else in his job application status should be updated reject

public class candidateStatusUpdate implements
Database.Batchable<sObject>,Database.Stateful {
    public Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator(
            ' SELECT Id,Name,status__c,Candidate_Number__c,CreatedDate FROM Job_application__c where CreatedDate = TODAY');
       
        
    }
    public void execute(Database.BatchableContext bc, List<Job_application__c> scope){
        Set<Candidate__c> con = new Set<Candidate__c>();
       
        List<Candidate__c> can = [select Id,name,status__c from Candidate__c];
// there are onth two values in candidate status feild accepted and //rejected.
//there are four values in job application status .
//one candidate can have multiple applications so even one job //application status is accepted then status feild in candidate should //be updated to accepted. if none of his job appliaction are accepted //then his status in candidate should be updated to rejected.
        for(Job_application__c j:scope){
            for(Candidate__c c:can){
                if(j.status__c == 'Accepted'){
                    c.Status__c = 'Accepted';
                    con.add(c);
                }else if((j.status__c == 'Rejected') || (j.status__c == 'New') || (j.status__c == 'On hold')){
                   c.Status__c = 'Rejected' ;
                }
            }
        }
        List<Candidate__c> cond = new List<Candidate__c>();
        cond.addAll(con);
        update cond;
    }
    public void finish(Database.BatchableContext bc){
        
        
    }
}
Maharajan CMaharajan C
Do you have any relationship between Candidate and Application... If yes please post the lookup field api name from Application Object... 

Incase you don't have any relationship fields then how you are relating the Candidate and Application...

Thanks,
Maharajan.C
shrayas reddyshrayas reddy
YES THEY BOTH ARE CONNECTED BY LOOKUP RELATION. I TRIED SAME PROGRAM DIFFERENTLY
public class candidateStatusUpdate implements
Database.Batchable<sObject>,Database.Stateful {
    public Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator(
            'SELECT Id, name,status__c,(select Id,name,status__c from Job_applications__r) from Candidate__c');
        
        
    }
    public void execute(Database.BatchableContext bc, List<Candidate__c> scope){
        
        // process each batch of records
        List<Candidate__c> cand = new List<Candidate__c>();
       // Set<Candidate__c> myset = new Set<Candidate__c>();
        //List<Job_application__c> jobap = new List<Job_application__c>();
        for (Candidate__c can : scope) {
            
            for(Job_application__c ja: can.Job_applications__r){
                if(ja.status__c == 'Accepted'){
                    can.status__c = 'Accepted';
                    cand.add(can);
                    
                   
                }else {
                    can.Status__c = 'Rejected';
                    cand.add(can);
                }
            }
            
        }
        //cand.addall(myset);
        try{
        
        update cand;
        }catch(Exception e){
           System.debug(e);
        }
    }
    public void finish(Database.BatchableContext bc){
        
        
    }
}

 
Maharajan CMaharajan C
Hi Shrayas,

Please try the below code :
 
public class candidateStatusUpdate implements
Database.Batchable<sObject>,Database.Stateful {
    public Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator(
            'SELECT Id, name,status__c,(select Id,name,status__c from Job_applications__r) from Candidate__c');
	}
    public void execute(Database.BatchableContext bc, List<Candidate__c> scope){
        // process each batch of records
        List<Candidate__c> cand = new List<Candidate__c>();
        for (Candidate__c can : scope) {
			List<Job_application__c> applicationAcceptedList = new List<>(Job_application__c);
            for(Job_application__c ja: can.Job_applications__r){
                if(ja.status__c == 'Accepted'){
                    applicationAcceptedList.add(ja);
                }
            }
			if(can.Job_applications__r.size() > 0 && applicationAcceptedList.size() > 0){
				can.status__c = 'Accepted';
				cand.add(can);
			}
// If you want to reject the candidate even if there is no Job_applications then change the below else if to else
			else if(can.Job_applications__r.size() > 0 applicationAcceptedList.size() == 0){
				can.Status__c = 'Rejected';
				cand.add(can);
			}
        }
		
        try{
			update cand;
        }catch(Exception e){
           System.debug(e);
        }
    }
    public void finish(Database.BatchableContext bc){
        
        
    }
}


// If you want to reject the candidate even if there is no Job_applications found then use the below else instead of elseif
else { can.Status__c = 'Rejected'; cand.add(can); }

Thanks,
Maharajan.C