- The future method is a part of asynchronous Apex.
- A future method runs in the background, asynchronously.
- You can call a future method for executing long-running operations, such as callouts to external Web services or any operation you’d like to run in its own thread, on its own time.
- future methods can also be used to avoid mixed DML errors.
- Future methods provide the benefits of not blocking the users from performing other operations and providing higher governor and execution limits for the processes.
Future method syntax:
- Use @future annotation before method declaration.
- Future methods must be static methods, and can only return a void type.
- The specified parameters must be primitive data types, arrays of primitive data types, or collections of primitive data types.
- Future methods can’t take standard or custom objects as arguments.
- A common pattern is to pass the method a list of record IDs that you want to process asynchronously.
- You can invoke future methods the same way you invoke any other method. However, a future method can’t invoke another future method.
- Within the transaction, we can call 50 future methods
Drawbacks In Future method
- Future Method will not return JobId in the apex code.
- The future method can not be called from another future method.
- The future method will not support SObjects as parameters.
Mixed DML Exception in Apex Can be avoided by future Methods.
- In an apex method when you try to perform DML operations on both setup and non-setup objects, then it throws a Mixed DML Exception.
Setup Object:-
- User
- Grou
- Group member
- Permission set
- Object Permissions
- User Role
Non Setup Object:-
- Account.
- contact.
- customobject__c.
- opportunity.
Example How to Avoid Mixed DML Error
For more details please refer to the official link
Like our post & bookmark this blog for your future learning.
Any suggestions and improvements are most welcome, please comment your suggestions and improvement in the comment box.
Happy Coding Sharing Is caring. 😀😀😀
إرسال تعليق