Future Method:
A future method runs in background, Asynchronously
each method is queued and executes when system resource becomes available
To define future method simply annotate it with future annotation
========================================================
global class FutureClass{
@future
public static void myFutureMethod(){
}
}
=======================================================
future Method:
Method with future annotation
1.Must be static methods
2.and only return a void type
specified parameter must be primitive data types
or arrays of primitive data type
It doesn't except sObject as parameter. sObject might change between the time you call the method and time it executes
To work with sObject that already exist in the database pass the sObject ID
=============================================================
Simple Code Snippets:
global class FutureMethodRecordProcessing{
@future
public static void processRecords(List<id>recordids){
List<account> accts= [SELECT name FROM account WHERE id in:recordids];
}
}
==============================================================
Queueable Apex:
Take control of your asynchronous process by using queueable interface
To monitor Queueable Apex:
setup-->Apex Jobs
Queueable jobs are similar to future methods in that they are both queued for execution
Additional benefits:
1.Getting the id of the job
2.using non primitive datatypes
3.chaining Jobs
================================================================
public class c1 implements Queueable{
public void execute(QueueableContext context){
account a=newaccount();
a.name='test';
a.phone='8147285030';
insert a;
}
}
=================================================================
//how do you call this class
ID jobid=system.enqueueJob(new c1());
=================================================================
Apex scheduler:
Suppose we want to execute job at regular interval.=================================================================
public class s1 implements Scheduleable{
public void execute(ScheduleableContext sc){
//code here
}
}
=================================================================
Please Note:
1.Code executes at the specified time
2.Actually execution may be delayed based on service availability
3.you can only have 100 scheduled apex at one time
goto apex classes-->scheduled apex--> you can specified time as well and start date & end date
No comments:
Post a Comment