You are currently viewing Salesforce Developer Interview Questions Part8

Salesforce Developer Interview Questions Part8

Salesforce Developer Interview Questions – Asynchronous Apex – Future Method

Asynchronous Apex

86. What is asynchronous apex?

Answer: 

Asynchronous Apex is used to run processes in a separate thread, at a later time.

An asynchronous process is a process or function that executes a task “in the background” without the user having to wait for the task to finish.

87. What are different asynchronous features available in Salesforce?

Answer: 

  • Future Methods
  • Batch Apex
  • Scheduled Apex
  • Queueable Apex

88.When to use asynchronous apex?

Answer: 

Asynchronous apex featureWhen to use
Future MethodsWhen we make callouts to external web serviceWhen we have a long-running method and need to prevent delaying an apex transactionTo segregate DML operations and bypass the mixed save DML error
Batch ApexFor long-running jobs with large data volumes that need to be performed in batches, such as database maintenance jobsFor jobs that need larger query results than regular transactions allow
Scheduled ApexTo schedule an apex class to run on a specific schedule
Queueable ApexTo start a long-running operation and get an ID for the jobTo pass complex types to job (like sobjects or custom apex types etc)To chain jobs

Future Methods

89. What is a future method?

Answer:

  • Future method is one of the asynchronous apex types which runs in the background or in a separate thread.
  • Future method is same as any other apex method with @future annotation
  • With future methods we get some increased governors limits lie SOQL query limits and heap size limits.
  • Future methods must be static and can only return void type.

90. Does the future method support all the data types as parameters or arguments?

Answer:

No, future methods only support primitive data types or collections and do not support sObjects as arguments.

91. Why does the future method not support sObject data types as arguments?

Answer:

The reason why sObject can’t be passed as arguments to future methods is because the sObject might change between the time we call the method and the time it executes. In this case ,the future method will get the old sObject values and might overwrite them.

92. What if we have the requirement to work with sObjects in the future method?

Answer:

To work with sObjects that already exist in the database, pass the sObject ID instead (or collection of IDs) and use the ID to perform a query for the most up-to-date record.

93. Can we make a callout to external web services using future methods and how to do it?

Answer:

Yes we can make a callout to external web services using future methods . Use an extra parameter (callout =true) with future annotation

Like @future(callout=true)

94. Write a sample code utilizing a future method?

Answer:

The following code uses a normal method to insert and account record and future method to insert user record. This can also be an example to avoid mixed dml error.

Main method:

Future Method

Leave a Reply