Queueable Apex

If you are after the asynchronous process then Queueable apex will do the work for you.

It combines the best of both Batch Apex and future methods. In other words, if you are after chaining jobs then Queueable apex is the one you should be looking for. You can use non-primitive data types like Sobjects or custom apex types.

Let create a simple Queueable demo. Let say you want to append the name of every account with '-BaconBone' in an asynchronous way. Then Queueable is the one for you. We won't be using any callout operation to keep this demo super simple.

Create a class 
QueueAcccClass


public class QueueAcccClass implements Queueable 
{
    public void execute( QueueableContext cont)
    {
        try{
            list<account> act = [select id, name from account limit 5];
            list<account> actupdate = new list<account>();
            for(account a : act)
            {
                a.name = a.name+'-BaconBone';
                actupdate.add(a);
                
            }
            
            database.update(actupdate, false);
        }
        catch(exception ex)
        {
            system.debug(ex.getMessage());
        }
        
        
    }
}


You can run this class using following code 

id jobid = system.enqueueJob(new QueueAcccClass());


Please ensure you turn on the logging to look for issues if there is one.


Comments

Popular posts from this blog

The Salesforce Certification Frenzy: Balancing Quantity with Quality in a Changing Market

The Ultimate Guide to Building a Salesforce Career: Roles, Skills, and Strategies for Success