Asynchronous Apex Interview Questions
Asynchronous Apex Interview Questions

Asynchronous Apex Interview Questions

Asynchronous Apex Interview Questions

Salesforce Apex’s asynchronous process is one of its most important parts. The asynchronous process lets us do long-running tasks in the background so that they don’t slow down the speed of the programme. This post is about Salesforce interview questions for the asynchronous apex.

Let’s look at Asynchronous Apex’s most important problems.

Q.1: What is Asynchronous Apex?

As we work on the Salesforce application, we store a lot of data in our org, and every year, that data will keep growing. If we have to run a job that will take a while, we will get a limit error, heap size error, or timeout error with synchronous apex. To avoid these problems, we can use Asynchronous apex to do long-running or time-consuming tasks. These tasks will run on a different thread and won’t affect the main thread. So, Asynchronous Apex is used to run the process later in a different thread.

Q.2: Where can Asynchronous Apex be used?

Asynchronous Apex can be used for tasks that take a long time to finish, like

  • The user’s email is sent.
  • Using Apex code to make complicated reports
  • Getting various records from an outside system
  • Set a job up for a certain time
  • Chaining the operation of Apex code, which could use an API call
  • Re-calculating Together

Q3: What are the perks of understanding things at different times?

When the machine is ready to work, an asynchronous process starts. It doesn’t stop people from working in the programme. This will help the programme in the following ways.

  • Make users more effective
  • More heap size and a higher ceiling for the governor
  • Better ability to grow

Q.4: What kinds of Asynchronous Apex are there?

There are four types of Asynchronous Apex that Salesforce currently offers.

  • Apex Batch
  • Future methods
  • Queueable Apex
  • Planned Peak

Q.5: How long after being enqueued will an asynchronous request be processed?

Asynchronous processing is less important than interactions that happen in real-time through the browser and API. This means it will constantly operate in the background, processing asynchronous tasks as they become available. We can’t choose when the job will run; that is up to the computer. It can also happen right away.

Q.6: Can you use a trigger to call out to a web service?

To avoid blocking the database transaction while waiting for the callout to finish, future methods must be used instead of synchronous triggers when interacting with external web services.

Q 7: Can a future method invoke a non-future method to do work, such as handling callouts, and have that non-future method return data to the future method?

  • No, we can’t use the future method to call a future method.

Q 8: What are the benefits of Batch Apex?

  • At the start of each batch activity, a new set of governor boundaries is used.
  • The number of batches for records is split by the system itself.
  • If one batch doesn’t work, the others will still run, and the good groups will still be added to the database. When one batch fails, the good ones are not rolled back.

Q.9: Why use Batch Apex instead of regular Apex in Salesforce?

Different things make Batch Apex better than regular Apex.

  • SOQL queries: normal Apex execution consumes 100 records each cycle.
  • SOQL searches. Batch Apex, on the other hand, does the same thing 200 records at a time.
  • SOQL searches can be recovered. Normal Apex can retrieve up to 50,000 SOQL queries, but Batch Apex can retrieve up to 50,000,000 SOQL queries.
  • The heap size of Normal Apex is 6 MB, while the heap size of Batch Apex is 12 MB.
  • Error: When running a large number of records, Normal Apex classes are more likely to run into problems than Batch Apex classes.

Q.10: What are some of the best ways to use batch apex?

  • If you only need to run a few records, use regular Apex instead of Batch Apex.
  • When calling a batch job from a trigger, be very careful. The trigger shouldn’t add more batch jobs than the limit.
  • Classes that implement the Database can’t have methods that are marked as “future.”Interface for batching.
  • From a batch Apex class, you can’t call methods that are marked as “future.”
  • All of the class’s methods must be either global or public.
  • Reduce the time it takes to call a web service.
  • Adjust searches that are used in batch Apex code.
  • Reduce the number of asynchronous calls to make sure there aren’t too many pauses.

Q. 11: How many Apex jobs that can be scheduled can you have at once? In other words, how many Apex classes at most can be planned at the same time?

100 Apex jobs that can be planned can be arranged.

Q. 12: Does Scheduled Apex allow web service callouts that happen at the same time?

No. The callout will be supported, though, if Scheduled Apex calls a Batch Apex job that then makes a web service callout.

Q.13: What are the best ways to do things in the future?

  • Every call to a future method adds one request to the delayed queue. Avoid design trends that add a lot of future requests quickly.
  • Make sure that the next ways run as quickly as possible.
  • If you’re using Web service callouts, try to do them all from the same future method instead of using a different future method for each callout.
  • If you want to process a lot of records without waiting, you might want to use Batch Apex instead of future ways.

Q. 14: Why don’t future functions work with sObject parameters?

Since the sObject could change between when we call the method and when it runs, we can’t pass sObjects as inputs to future methods. In this case, the old sObject values will be passed to the future method, which may replace them.

Q.15: Can I link a Job that doesn’t have allowsCallouts to one that does?

Yes, chained queueable jobs can also have callouts.

Q.16: Can I use a batch to call Queueable?

Yes, but it can only be used on one system at a time.enqueueJob call in the Database for each job that is run.Groupable class. Salesforce has put this limit in place to stop operations from going too fast.

Q.17: When can’t a future method be called from a batch job?

In the Execute method, you can’t call a future method. However, you can call a web service. An @future method can also be called from a web service. So, we can set up a web service with a future method calling and call it from Batch Job’s run method.

Q18: How does the Future way help avoid Mixed DML errors?

In Salesforce, there are two kinds of sObjects.

  • Account, chance, etc. that are not set up
  • Setup: User, groups, Queue etc

If we try to do DML on both kinds of sObject in the same transaction, the system won’t let us. Instead, it will throw an error called a “Mixed DML exception,” which says that a transaction can’t have both set up and non-setup DML operations.

We can fix this mistake by putting certain kinds of DML processes in the Future scope. Since neither DML process affects the other, the deal does not fail.

Q.19 Let’s say we’ve started a peak batch to process 2,000 records, and the batch size is 200. Now, a mistake happened when DML was being done on the 298th record, In that case, what will happen?

In batches, if the first transaction works but the second one doesn’t, the first transaction’s changes to the database are not undone.

Since the batch size is 200, the first batch will be fully handled and all of the data will be saved to the database. If we use normal DML lines like enter and update to commit records in the second batch, the whole batch will be rolled back. So records 201 through 400 won’t be handled.

Database DML actions, such as Database. insert, may be performed with AllOrNone set to false, allowing for partial commits in which just the 298th record in a batch is skipped over for processing. Also, the other batch will keep running.

Q.20: What is a Database? QueryLocator and Iterable?

We use a simple query (SELECT) in Database.QueryLocator to get the scope of objects. The governor’s limit on how many records SOQL searches can bring back is ignored, so it can bring back up to 50 million records.

With Iterable, we can make a special processing area, which we couldn’t do with SOQL where clauses. The governor cap on how many records SOQL searches can get is still applied.

Learn more about asynchronous apex here.

You might also be interested in

Check Also

Salesforce Certification cost

Salesforce Certifications in 2023 and it’s Price Tag

  Salesforce is offering more Salesforce Certifications in 2023 than ever before, and if you’re …

Leave a Reply

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