+ Start a Discussion
John AthitakisJohn Athitakis 

Simple Code: Changing from @future to queueable?

So here is my situation. I recently began to realize I need to async more of our code, and learned all about @future (WEEE!) but have quickly come to realize that because we have many moving pieces that @future has limitations (specifically when one @future might trigger another @future).

In queueable, which seems to be the silver knight I have wanted but, I'm having a misunderstanding as to how to go about this (most examples I find show the queuable apex class which I understand but, not understanding how to pass it the values of triggering ID's).

So here is my simple code:

Trigger

 

trigger SalesAreaRelatedOpportunityUpdate on Sales_Area__c (after insert,after update) {
    SalesAreaRelatedOpportunityUpdateClass.processOpportunities(Trigger.newMap.keySet());

}

 

Class

global class SalesAreaRelatedOpportunityUpdateClass {
@future
    public static void processOpportunities(Set<ID> salesAreaId){

    List <Opportunity> updateOps = [SELECT Id FROM Opportunity WHERE IsClosed = False AND (Sales_Area__c in :salesAreaId OR Secondary_Sales_Area__c in :salesAreaId)];
    if(!updateOps.isEmpty()) {
        update updateOps;   
        
    }
                                                                
}
}

What this code does is merely update the related opportunities from a custom object which in turn, triggers other code. Some of that other code due to load needs to be async (and currently is @future, although once I figure out this queueable business I may change it as well).

My question is this---how would I change this pretty basic code to something queable? I'm having some troubles understnading how to pass the keySet in particular. 

Best Answer chosen by John Athitakis
Richard Jimenez 9Richard Jimenez 9
Hi John,

There is a really good trailhead on async apex with some good tutorials you can try out so you can learn about the syntax required from queueable apex. You can pass sobjects into queueable jobs and chain jobs.

See: https://developer.salesforce.com/trailhead/asynchronous_apex/async_apex_queueable

Thanks,
Richard.

All Answers

Richard Jimenez 9Richard Jimenez 9
Hi John,

There is a really good trailhead on async apex with some good tutorials you can try out so you can learn about the syntax required from queueable apex. You can pass sobjects into queueable jobs and chain jobs.

See: https://developer.salesforce.com/trailhead/asynchronous_apex/async_apex_queueable

Thanks,
Richard.
This was selected as the best answer
John AthitakisJohn Athitakis

Thank you! Based on what I saw, I changed my code to the following. I can see it working in the job queue without error, but it doesnt seem to actually be updating any records. 

 

My new trigger

 

trigger SalesAreaRelatedOpportunityUpdate on Sales_Area__c (after insert,after update) {
   Set<ID> salesAreaId = new Set<ID>();      
        for (Sales_Area__c sa : Trigger.new){
        salesAreaId.add(sa.id);
        }
    List <Opportunity> updateOps = [SELECT Id FROM Opportunity WHERE IsClosed = False AND (Sales_Area__c in :salesAreaId OR Secondary_Sales_Area__c in :salesAreaId)];
    
    SalesAreaRelatedOpportunityUpdateClass updateJob = new SalesAreaRelatedOpportunityUpdateClass(updateOps);
    ID jobID=System.enqueueJob(updateJob);

}

My new class. I created the system update time field just for testing. The goal is here to just to update my records which will cause other code to begin and process. What em I missing? No errors in my apex jobs. 
 
public class SalesAreaRelatedOpportunityUpdateClass implements Queueable {

    private List<Opportunity> opportunities;
    
    public SalesAreaRelatedOpportunityUpdateClass(List<Opportunity> records){
    this.opportunities=records;    
    }
    
    public void execute(QueueableContext context){
        for(Opportunity opportunity : opportunities){
            opportunity.System_Update_Time__c = datetime.now();
        }
        update opportunities;
    }
   
}

 
John AthitakisJohn Athitakis

Hello Richard and Mahesh,

The seeming error was an oversight on my part. My select ignored Closed Opportunities and unknowingly, I had been testing on Closed Opportunities. Please excuse the run around! 

Thank you guys!