+ Start a Discussion
Luke Higgins 22Luke Higgins 22 

Batch class that flags object

I'm trying to write a batch class that queries for timesheets (jstcl__TG_Timesheet__c) that has its lookup related placement occur twice or more from the query, and then use the list of flagged duplicate timesheets to update a checkbox field on them. My thought was to go through the queried results, add them to a list and then iterate through that list in to see if they already have been anaylized in order to flag them as having the same related placement.

Here is the code:
global class placementTimecardAuditFlag implements Database.Batchable<sObject>, Database.Stateful {

  List<jstcl__TG_Timesheet__c> query = [SELECT jstcl__Placement__r.Name FROM jstcl__TG_Timesheet__c WHERE jstcl__Placement__r.ts2__Status__c ='Active' AND jstcl__Week_Ending__c = LAST_N_DAYS:15 AND jstcl__Status__c = 'Pending'];
    List<jstcl__TG_Timesheet__c> myList = new List<jstcl__TG_Timesheet__c>();
    List<jstcl__TG_Timesheet__c> myList2 = new List<jstcl__TG_Timesheet__c>();

global Database.QueryLocator start(Database.BatchableContext bc) {
        for(jstcl__TG_Timesheet__c item : query){
    if(myList.contains(item)){
        myList2.add(item);
    }
    else{myList.add(item);}
    }
      return Database.getQueryLocator('SELECT Id,Checkbox1__c FROM jstcl__TG_Timesheet__c WHERE jstcl__Placement__r.Name IN :myList2');}
      

 global void execute(Database.BatchableContext BC, List<jstcl__TG_Timesheet__c> a){
    for(jstcl__TG_Timesheet__c b : a) {
        if(b.Checkbox1__c = True){
           b.Checkbox1__c = False;
        }
        else{b.Checkbox1__c = True;}}
update a;        
    }
global void finish(Database.BatchableContext BC){}
}
Thank you
Best Answer chosen by Luke Higgins 22
Luke Higgins 22Luke Higgins 22
global class placementTimecardAuditFlag implements Database.Batchable<sObject>, Database.Stateful {

  List<jstcl__TG_Timesheet__c> query = [SELECT jstcl__Placement__r.Name FROM jstcl__TG_Timesheet__c WHERE jstcl__Placement__r.ts2__Status__c ='Active' AND jstcl__Placement__r.jstcl__Timesheet_Period__c != 'Weekly Split' AND jstcl__Week_Ending__c = LAST_N_DAYS:15 AND jstcl__Status__c = 'Pending'];
    Set<String> encounteredNames = new Set<String>();
    Set<String> duplicateNames = new Set<String>();

global Database.QueryLocator start(Database.BatchableContext bc) {
        for(jstcl__TG_Timesheet__c item : query){
    if(encounteredNames.contains(item.jstcl__Placement__r.Name)){
        duplicateNames.add(item.jstcl__Placement__r.Name);
    }
    else{encounteredNames.add(item.jstcl__Placement__r.Name);}
    }
      return Database.getQueryLocator('SELECT Id,Checkbox1__c FROM jstcl__TG_Timesheet__c WHERE jstcl__Placement__r.Name IN :duplicateNames');}
      

 global void execute(Database.BatchableContext BC, List<jstcl__TG_Timesheet__c> a){
    for(jstcl__TG_Timesheet__c b : a) {
        if(b.Checkbox1__c = True){
           b.Checkbox1__c = False;
        }
        else{b.Checkbox1__c = True;}}
update a;        
    }
global void finish(Database.BatchableContext BC){}
}

Here's what I figured out to work.