Future Method in Salesforce

By | March 19, 2020

FUTURE METHOD

  1. If a transaction has any long running operation/statements and if this operations is not having any dependency on rest of the operation then we can run the operation independent from the rest of the operation by using future method.
  2. Long running operation are called Bulk DML operations,webservice callouts.

Rules to define future methods :

  1. All the future method should have @future annotation.
  • @Future
  • Public static void futurecall(parameters){
  • }
  • Every future method should be static.
  • All the future methods should have void as return type. [void is used when we don’t want return type].
  • Future method accepts only primitive parameters.
  • [Because we can’t use sobject here, the data is future method will be assigned is Queue, so if any updation mode is sobjects, while data is in queue, the updated data will not be considered in queue. This is because future method accepts only primitive variables not sObjects.].
  • When we invoked the future method they will be added is queue and from the queue they will be executed.
  • If you want to invoked web services from the future method then define @future(callout=true).
  • Any Asynchronous job that is running is salesforce will be registered with AsyncApexJob object.
  • All the asynchronous jobs that are running they are added to queue.
  • How to track the status of the future method.
  • Write a soql queue on AsyncApexJob. (if we know jobid)
  • Declaration way to check the status.
  • Setup—> moniter—>Jobs—>Apex Jobs.
  • Future methods can be used increase the governing limits.
  • @future(limits=dml*2).

MIXED DML EXCEPTION

If you make a DML operation on setup object and non-setup objects is a single transaction then it throws mixed-DML-Exception.

  1. Setup object.
  2. Non-setup object.

DML operation on some of the Sobjects are referred as setup objects.

1.Setup objects :

DML operation on this sobjects will effect the record level access for users.

Example: Users, Group, Groupmembers, Permissionset, objectpremissions, userRole, Permission set Assignment.

2.Non-setup objects:

Rest of the objects are called Non-setup.

Example:  Account,  Contact,  Customer__c

DRAWBACKS:

  1. Future method will not return jobid in the apex code.
  2. Future method cannot be called from another future method
  3. Future method will not support subject as parameters.

Limits:

With in a transaction we call 50 future methods.

Use cases:

  1. Handle mixed DML operations.
  2. To involk Asynchronous webservices from trigger.
  3. To involk Asynchronous webservices from schedule Apex.
  4. To handle bulk DML operations

Workaround use cases:

  1. When we have required to pass subject as parameters to future method, how will you
  2. Pass RecordId instead of record.
  3. Serialize the subject as string using JSON/XML and pass string as parameter to future method.
Share this...
Share on Facebook
Facebook
Email this to someone
email

Leave a Reply

Your email address will not be published. Required fields are marked *