For example, you can use the future annotation when making an asynchronous Web service callout to an external service. Without the annotation, the Web service callout is made from the same thread that is executing the Apex code, and no additional processing can occur until the callout is complete (synchronous processing).
global class MyFutureClass {
@future
public static void myMethod() {
EmailClass.SendEmailNotification();
//do callout, other long running code
}
}
Email Class
global class EmailClass{
WebService static void SendEmailNotification() {
//create a mail object to send a single email.
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
//set the email properties
mail.setToAddresses(new string[] {'wahib.idris@xyz.com'});
mail.setSenderDisplayName('SF.com Email Agent');
mail.setSubject('A new reminder');
mail.setHtmlBody('Password');
//send the email
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail } );
}
}
Points to Remember about future method :
- No more than 10 method calls per Apex invocation.
- The specified parameters must be primitive data types, arrays of primitive data types, or collections of primitive data types.
- Methods with the future annotation cannot take sObjects or objects as arguments.
- Methods with the future annotation cannot be used in Visualforce controllers in either getMethodName or setMethodName methods, nor in the constructor.
- You cannot call a method annotated with future from a method that also has the future annotation. Nor can you call a trigger from an annotated method that calls another annotated method.
- @future(callout = true) means that the method has ability to invoke external web services.
its really very understanble view
ReplyDeleteThanks ! Cheers
ReplyDeleteThanks Kovalan !!
ReplyDeleteReally awesome blog. Your blog is really useful for me. Thanks for sharing this informative blog. Keep update your blog.
ReplyDeleteJ2EE training in chennai