Different Ways of Invoking Apex:
1) Anonymous Code Block: An Anonymous block is Apex code that does not get stored in the metadata, but that can be compiled and executed using Developer Console. You can use anonymous blocks to quickly evaluate Apex on the fly
2) Apex Trigger: Call Apex using trigger
3) Visualforce Classes: Using controller
4) Asynchronous Apex
a) Future Methods: A future method runs in the background, asynchronously. You can call a future method for executing long-running operations or any operation you’d like to run in its own tread, on its own time. Each future method is queued and executes when system resources become available. To define future method, simply annotate it with the future annotate:
Global class FutureClass{
@future
Public static void runmyfuturemethod()
{
//Do operation
}
Points to Remember:
Methods with future annotation : i) Must be static methods
ii) Can only return a void type.
For details: http://arijitknowledge.blogspot.in/2016/04/future-annotation-use-future-annotation.html?spref=bl
b) Queueable Apex:
Take control of your asynchronous Apex processes by using the Queueable interface. This interface enables you to add jobs to the queue and monitor them, which is an enhanced way of running your asynchronous Apex code compared to using future methods.
For Apex processes that run for a long time, such as extensive database operations or external Web service callouts, you can run them asYou can use this ID to identify your job and monitor its progress, either through the Salesforce user interface in the Apex Jobs page, or programmatically by querying your record from AsyncApexJob.nchronously by implementing the Queueable interface and adding a job to the Apex job queue. In this way, your asynchronous Apex job runs in the background in its own thread and doesn’t delay the execution of your main Apex logic. Each queued job runs when system resources become available.
Difference between Future method and Queuable Apex is that Future method is for a specific method and Queueable apex is for class.
Queueable jobs are simillar to future methods in that they are both queued for execution. But they provide you with these additional benefits:
1. Getting an ID for your job: When you submit your job by invoking the System.enqueueJob method, the method returns the ID of the new job.
You can use this ID to identify your job and monitor its progress, either through the Salesforce user interface in the Apex Jobs page, or programmatically by querying your record from AsyncApexJob.
2. Using non-primitive types: Your queueable class can contain member variables of non-primitive data types, such as sObjects or custom Apex types. Those objects can be accessed when the job executes.
3. Chaining jobs: You can chain one job to another job by starting a second job from a running job. Chaining jobs is useful if you need to do some processing that depends on another process to have run first.
Example:
public class AsyncExecutionExample implements Queueable
{ public void execute(QueueableContext context)
{ Account a = new Account(Name='Acme',Phone='(415) 555-1212'); insert a; } }
To add this class as a job on the queue, call this method:
ID jobID =
System
.enqueueJob(
new
AsyncExecutionExample());
After you submit your queueable class for execution, the job is added to the queue and will be processed when system resources become available.
Testing Queueable Jobs
@isTest public class AsyncExecutionExampleTest { static testmethod void test1() { // startTest/stopTest block to force async processes // to run in the test. Test.startTest(); System.enqueueJob(new AsyncExecutionExample()); Test.stopTest(); // Validate that the job has run // by verifying that the record was created. // This query returns only the account created in test context by the // Queueable class method. Account acct = [SELECT Name,Phone FROM Account WHERE Name='Acme' LIMIT 1]; System.assertNotEquals(null, acct); System.assertEquals('(415) 555-1212', acct.Phone); } }
c) Apex Scheduler
To Invoke Apex Classes to run at specific times,
1) Implement the Schedulable Interface for the Class
2) Specify the Schedule using the Schedule Apex page in the Salesforce user interface
3) The Schedulable interface contains one method that must be implimented,
execute.global void execute(SchedulableContext Sc){}
Please Note:
1. Salesforce schedules the class for execution at the specific time
2. Actual execution may be delayed based on service availabilty.
d) Batch Apex
For Detail: http://arijitknowledge.blogspot.in/2016/04/batch-apex-in-salesforce-it-allows-you.html
https://www.janbasktraining.com/
ReplyDelete