You need to sign in to do that
Don't have an account?
shrayas 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){
}
}
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){
}
}
Incase you don't have any relationship fields then how you are relating the Candidate and Application...
Thanks,
Maharajan.C
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){
}
}
Please try the below code :
// 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