1

I am trying to pass Sobject from trigger to a queueable and then i am calling another queueable and trying to pass the same sObject. But i am unable to get the values in queueable .

Trigger -

trigger checkaccount on contact (after insert, after update) {
    List<Contact> cont = new List<Contact>();
    for(Contact record: Trigger.new) {
        cont.add(record);
    }
    System.enqueueJob(new firstqueablejob (cont)); 
}

firstqueablejob

public class firstqueablejob implements Queueable,Database.AllowsCallouts {

    List<Contact> cont = new List<Contact>();
    public firstqueablejob (List<Contact> cont) {
        cont = cont;
    }
    public void execute(QueueableContext context) { 
        system.debug('contfirst ' + cont); // I am getting null here
        System.enqueueJob(new secondqueuablejob (cont)); 
    }
}

secondqueuablejob

public class secondqueuablejob implements Queueable,Database.AllowsCallouts {

     List<Contact> cont = new List<Contact>();
    public secondqueuablejob (List<Contact> cont) {
        cont = cont;
    }
    public void execute(QueueableContext context) { 
        system.debug('contsecond' + cont); // I am getting null here
    }

}
1
  • Can you please try by changing the statement cont=cont to this.cont=cont at both queueables.
    – manjit5190
    Mar 5, 2020 at 16:13

1 Answer 1

4

Simple mistake here; you named the member variable and the parameter the same name. This causes a "shadow" effect. To assign the value to the member variable, use this.

public secondqueuablejob (List<Contact> cont) {
    this.cont = cont;
}

P.S. Instead of looping over all the elements, you could have just called List.deepClone...

System.enqueueJob(new firstqueablejob (Trigger.new.deepClone(true,true,true))); 

Or even just List.clone:

System.enqueueJob(new firstqueablejob (Trigger.new.clone())); 

Or just pass in Trigger.new directly (that's perfectly legal!):

System.enqueueJob(new firstqueablejob (Trigger.new)); 

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .