Transaction Finalizers in Salesforce Queueable Interface with variable to pass data to finalizer

Transaction Finalizers in Salesforce Queueable Interface with variable to pass data to finalizer

Sample Code:

public class SampleQueueableClassWithFinalizer implements Finalizer, Queueable {
    
    private String strError;
    
    public void execute( QueueableContext ctx ) {
        
        try {
            Contact objCon = new Contact( FirstName = 'Testing' );
            insert objCon;//This DML will fail since LastName is required while creating Contact
            
        } catch ( Exception e ) {
            
            SampleQueueableClassWithFinalizer f = new SampleQueueableClassWithFinalizer();
            System.attachFinalizer( f );        
            f.strError = e.getMessage();
            System.debug( 'Error executing the job is ' + f.strError );
            
        }
        
    }
    
    public void execute( FinalizerContext ctx ) {
        
        System.debug( 'Inside Finalizer' );
        System.debug( 'strError is ' + strError );
        
        if ( ctx.getResult() == ParentJobResult.SUCCESS) {
            
            System.debug( 'Queueable job completed successfully' );
            
        } else {
            
            System.debug( 'Queueable job failed due to ' + ctx.getException().getMessage() );            
            
        }
        
    }
    
}

To call the Queueable call:

System.enqueueJob( new SampleQueueableClassWithFinalizer() );

Output:

Leave a Reply