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
Sam WardSam Ward 

how do you check for a value change in scheduled apex

Hi, 

New to Apex so I dont fully understand how it all works however I have got the following peice of code working: 
 
global class batchExample implements Database.Batchable<sObject> {
    
    
    global Database.QueryLocator start(Database.BatchableContext BC) {
        // collect the batches of records or objects to be passed to execute
        
        String query = 'SELECT Id,Link_Type__c FROM Lead';
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext BC, List<Lead> LeadList) {
       
        // process each batch of records

        
        for(Lead L : LeadList)
        {        
            // Update the Account Name 
            //L.Link_Type__c = 'TEST';
            Pardot_Campaign__c PC = new Pardot_Campaign__c();
            PC.Lead__c = L.Id;
            insert PC;
            
            }
            
        try {
            // Update the Account Record
            update LeadList;
        
        } catch(Exception e) {
            System.debug(e);
        }
        
    }   
    
    global void finish(Database.BatchableContext BC) {
        // execute any post-processing operations
  }
}

That works and creates the additional records however I want it based on if a field has changed since it last checked it. 

Field name: Pardot Campaign 

This updates with a value if someone has clicked on a link from an email so I only want the leads that have changed prior to the scheduled apex running if that makes sense. I've been searching on how to look for a value change but couldn't find anything. 

Thanks 
David Zhu 🔥David Zhu 🔥
I think you can have two ways to implement this:
1. use a trigger to add addition Pardot Campaign record when Partdot Camaign field is changed on Lead.
2. If you have to use Apex batch, do some changes.
    Add a field on Lead to capture pardot campaign field changed, also add a trigger to do that.
    In the batch, only query lead record with the new field having correct value (changed), do what you need to do, then clear that field.

I would prefer using option 1.