0

I have a method in apex class

    public static void test(List<contact> newOppProdList,Map<Id,contact> oldOppProdList){
    
    List<test__c> testprod= [Select Id,name from test__c];
    for(test__c objProjectProd : testprod){
code operations here}
           

I am transferring the method to queuable apex class:

public class Queueabletest implements Queueable { 
    public void execute(QueueableContext context) {

I am replacing the query here.

        List<test__c> testprod= [Select Id,name from test__c];
         return testprod;
}

can i remove the method query in apex class method and i can use same testprod variable in that method ,it will work? and then method parameter values (List<opportunitylineitem> newOppProdList,Map<Id,opportunitylineitem> oldOppProdList) is coming for the input as trigger in both after insert and after update.

How can i give the input for queuable class?

for after insert, contactTriggerHandler.test(trigger.new,null);

for after update, contactTriggerHandler.test(trigger.new,trigger.oldMap);

1 Answer 1

1

In your example there appears to be no input for the Queueable class.

But in general, you can pass input to a Queueable class by defining properties on an instance of the class before you enqueue it. That data is automatically available when the execute method is invoked some time later.

public class Queueabletest implements Queueable {

    private String v1;
    private Integer v2;

    // Constructor executes now
    public Queueabletest(String v1, Integer v2) {
        this.v1 = v1;
        this.v2 = v2;
    }

    // This method will be executed in the future
    public void execute(QueueableContext context) {
        // v1 and v2 can be referenced in here
    }
}

(The instance is serialized and deserialized to achieve this and not all types are serializable, though the simple types certainly are.)

To run:

System.enqueueJob(new Queueabletest('Hello', 123));
1
  • 1
    Yes you can pass the Trigger.new list into your Queueable.
    – Keith C
    Commented Apr 10, 2021 at 13:10

You must log in to answer this question.