Part 9: Salesforce Interview Questions (Asynchronous Apex)

81. How to use @future Annotation in Apex?

Future method is used to run the process in a separate thread at a later point of time(when resources are available). 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 static void myMethod(String a, Integer i)

{

System.debug(‘Method called with: ‘ + a + ‘ and ‘ + i);

// Perform long-running code

}

}

82. What are the different considerations while implementing future method?

  • Remember that any method using the future annotation requires special consideration because the method does not necessarily execute in the same order it is called.
  • 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.

83. When do we use Batch Apex?

When we have large volumes of data and do not require business logic to be processed in realtime we can use or implement Batch apex. We can process upto 50 million records using Batch Apex.

84. What are the different methods in Batch Apex?

Batch apex runs in Asynchronous mode and consists of 3 methods.

Start – Start method is used to query the list of records

Execute – Execute method is used to process business logic

Finish – Finish method is used to send email or call a different batch job or do nothing.

85. What is the purpose of QueryLocator in Batch Apex?

Query locator is used in Start method of Batch apex and it gives the ability to process upto 50 million records.

86. What are the different types of interfaces used in Batch Apex?

There are 3 different interfaces in Batch Apex

1. Database.Batchable

2. Schedulable

87. What is batch job chaining?

We can launch other Batchjob from finish method of the first job. This is called batch job chaining.

88. How many batchjobs can be run in parallel?

We can run a maximum of 5 batchjobs parallelly.

89. How many batchjobs can be in Apex flex queued at any given point of time?

With the Apex flex queue, you can submit up to 100 batch jobs.

90. How can we invoke batchjob from Developer console?

Example: We have a batch job AccountProcessBatch that needs to be executed with a default size. Then we can use below command.

Database.executebatch(new AccountprocessBatch());

Dev Tip 3: Execute Batchjob from Anonymous Window/Dev Console

 

sfdclightning

Related post