Slaesforce FAQ

can i process 500000 records using queueable apex salesforce

by Enrique Towne Published 2 years ago Updated 2 years ago
image

Using a Batch Job with the Query Locator, it’s possible to process up to 50 million records (50,000,000). For example, we could implement a batch job that looks like this: We can then start the batch processing by calling it through Execute Anonymous or maybe through other Apex Code.

Full Answer

When to use queueable apex in Salesforce?

It comes in handy when we need to have both the operations of Batch and Future method and it should implement Queueable Interface. Queueable apex can be called from the Future and Batch class. Moreover Queueable apex supports getContent/getContentAsPDF () method.

Can we use another asynchronous apex queueable to handle lot of records?

So what will be efficient solution for our use case. Can we use another asynchronous apex as we have lot of records to process? Yes, we have to use another asynchronous apex Queueable to handle this. We can query all pending records in one parent queueable class and then process records in another queueable class which will support callout.

How many jobs can I add to the queue in Salesforce?

You can add up to 50 jobs to the queue with System.enqueueJob in a single transaction. In asynchronous transactions (for example, from a batch Apex job), you can add only one job to the queue with System.enqueueJob.

How do I run multiple jobs sequentially in Salesforce apex?

If you ever need to run jobs sequentially, Queueable Apex could make your life much easier. To chain a job to another job, submit the second job from the execute () method of your queueable class. You can add only one job from an executing job, which means that only one child job can exist for each parent job.

image

What is Asynchronous Apex?

Asynchronous Apex allows us to run processes in a separate thread at a later time without having the user sit around waiting for a task to finish. The benefits of asynchronous apex are User Efficiency, Scalability and higher limits.

Future Methods

A Future Method is an asynchronous method that runs in the background on another thread. We should think of a future method as a quick way to do a relatively small task on a small number of records.

Batch Apex

Batch apex runs asynchronously based on available resources and can chunk up the data per batch OR we can provide our own batch sizes.

Queueable Apex

Queueable Apex allows us to add jobs to the Queue and to be able to monitor them. Queueable Jobs are very similar to Future Methods but have a future other benefits.

Scheduled Apex

Scheduled Apex is Apex that is set to run at a certain time – it could be set to run every hour, every day, every week or etc. The main point is that Scheduled Apex enters the queue at the scheduled time and executes at some point after that.

Wrapping it Up

In this blog post, we’ve covered the various ways that we can execute asynchronous apex which has a lot less limits then synchronous apex. The future method is a good first step, but there’s a lot more scalable ways to build out your system.

Example

This example is an implementation of the Queueable interface. The execute method in this example inserts a new account.

Testing Queueable Jobs

This example shows how to test the execution of a queueable job in a test method. A queueable job is an asynchronous process. To ensure that this process runs within the test method, the job is submitted to the queue between the Test.startTest and Test.stopTest block.

Chaining Jobs

If you need to run a job after some other processing is done first by another job, you can chain queueable jobs. To chain a job to another job, submit the second job from the execute () method of your queueable class. You can add only one job from an executing job, which means that only one child job can exist for each parent job.

What is queueable apex?

It beats sliced bread hands down! Queueable Apex allows you to submit jobs for asynchronous processing similar to future methods with the following additional benefits: Non-primitive types: Your Queueable class can contain member variables of non-primitive data types, such as sObjects or custom Apex types.

Why use future methods instead of queueable?

Another reason to use future methods instead of queueable is when your functionality is sometimes executed synchronously, and sometimes asynchronously. It’s much easier to refactor a method in this manner than converting to a queueable class.

Can you chain queueable jobs in Apex?

Once again, testing has a slightly different pattern. You can’t chain queueable jobs in an Apex test, doing so results in an error. To avoid nasty errors, you can check if Apex is running in test context by calling Test.isRunningTest () before chaining jobs.

INTRODUCTION

Queueable Apex is an apex code that runs Asynchronously like a future method.

Queueable is a interface whereas future is a method

These are used to run the long-running operations like external web-services call and bulk database operation asynchronously.

What is batch apex?

Batch apex is used to run large jobs (like millions) that would exceed normal processing units. Using Batch apex, we can process records asynchronously in batches. Batch apex processes up to 50million records in the background, unlike Future and Queueable.

Can queueable handle millions of records?

Queueable can't handle millions of records in one job. Only one job can be scheduled at a time. In the developer edition stack depth for chaining the jobs is limited to 5 including the parent job.

Use Case To Solve

We have around 30K+ account records of health care providers, we need to validate address of all records using address API (any API like SmartyStreet, Google Places, ZipCode ). After API call we need to update record with outcome of API call like address validation is successful or failed in ValidationStatus field.

Solution

To solve this use case we can use batch apex which will handle 200 records (100 in case of callout in batch) in a single batch and batch job will be created for all records. As it will run in batch, we have to put some condition in batch SOQL so that it will not process same record again and again. So we can use query like

Why 99 records in one batch?

As we can call only 100 records in one batch in asynchronous apex. I have used 99 records instead of 100 but you can use 100 as well.

Reference

If you need more discussion on this topic, let us connect for discussing and implementing this pattern.

image
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 1 2 3 4 5 6 7 8 9