0

I'm running a batch class over the Timesheet object and for the timesheets that meet the criteria, I want to update the related Contact record.

I have a jstcl__TG_Timesheet__c object with a field jstcl__Consultant__c relating jstcl__TG_Timesheet__c to the Contact object. I'm trying to run a batch class over the timesheets and for the ones that meet the criteria, I want to update the Previous_Timesheet_Status__c field on the related Contact record. What I have now runs successfully, however it doesn't actually update the field.

public class batchClass implements Database.Batchable<sObject>{
   public Database.QueryLocator start(Database.BatchableContext bc) {
      String query = 'SELECT Id, jstcl__Consultant__r.Previous_Timesheet_Status__c FROM jstcl__TG_Timesheet__c WHERE jstcl__Placement__r.ts2__Status__c =\'Active\' AND jstcl__Placement__r.VMS__c = \'\' AND jstcl__Week_Ending__c = LAST_N_DAYS:4 AND jstcl__Status__c = \'Pending\'';
      return Database.getQueryLocator(query);
    }
   public void execute(Database.BatchableContext BC, List<jstcl__TG_Timesheet__c> a){
      for(jstcl__TG_Timesheet__c b : a){
         b.jstcl__Consultant__r.Previous_Timesheet_Status__c = 'Pending';
      }
    update a;
   }
   public void finish(Database.BatchableContext BC){}
}
1

The issue here is that your update statement is updating the list of timesheet records, whereas you need it to update a list of contacts. I would rewrite your code as follows:

public class batchClass implements Database.Batchable<sObject>{
   public Database.QueryLocator start(Database.BatchableContext bc) {
      String query = 'SELECT Id, jstcl__Consultant__r.Previous_Timesheet_Status__c FROM jstcl__TG_Timesheet__c WHERE jstcl__Placement__r.ts2__Status__c =\'Active\' AND jstcl__Placement__r.VMS__c = \'\' AND jstcl__Week_Ending__c = LAST_N_DAYS:4 AND jstcl__Status__c = \'Pending\'';
      return Database.getQueryLocator(query);
    }
   public void execute(Database.BatchableContext BC, List<jstcl__TG_Timesheet__c> timesheets){
      List<Contact> consultants = new List<Contact>();
      for(jstcl__TG_Timesheet__c timesheet : timesheets){
         Contact consultant = timesheet.jstcl__Consultant__r;
         if (consultant == null) continue;
         consultant.Previous_Timesheet_Status__c = 'Pending';
         consultants.add(consultant);
      }
      update consultants;
   }
   public void finish(Database.BatchableContext BC){}
}

I took the liberty of assigning some more intuitive variable names than 'a' and 'b'; this can help other people reading your code more easily understand what it does.

2
  • 1
    N.B. SFSE etiquette is to not solicit for marking answers if it helps the questioner. See Meta Post on this subject – cropredy Jan 31 '20 at 18:03
  • Aah, apologies; I thought I saw that as a thing on another SE :-). Thanks for the heads-up; deleted – Nathanael Schmolze Jan 31 '20 at 21:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.