Salesforce Apex Interview Questions & Answers #salesforce #apex #interview

Tech Journey With Ankit
27 Jul 202425:13

Summary

TLDRIn this fifth installment of the Apex interview preparation series, the host, Jen, addresses frequently asked questions about Salesforce's Apex programming. Topics include the limitations of future methods, such as the inability to pass SObjects and the restriction on chaining or scheduling them. The video also covers batch Apex, explaining its methods, the difference between using Database.QueryLocator and Iterable, and the importance of stateful and stateless batch jobs. Additionally, Jen discusses error handling and the key limits of batch Apex, providing practical insights for interview preparation and real-world application.

Takeaways

  • 😀 The video is part of an interview preparation series focusing on Apex in Salesforce, addressing frequently asked questions from the audience.
  • 🔍 The first discussed topic is the limitation of the `@future` method not supporting the SObject data type due to the potential for data changes during asynchronous execution.
  • 🔑 A recommended workaround for the `@future` method's limitation is to pass the ID of the SObject and query the latest record within the method itself.
  • ❌ It is not advised to pass SObjects through serialization and deserialization in `@future` methods, though it's mentioned as an alternative for scenarios where the SObject is guaranteed not to change.
  • 🚫 The `@future` method cannot be called from another `@future` method or from a batch job due to Salesforce's restrictions on asynchronous operations.
  • 🔢 The limit on the number of `@future` method invocations per transaction is 50, which is a key consideration to avoid hitting execution limits.
  • 📅 Scheduled Apex does not support callouts, but there are workarounds involving the use of `@future` methods with `callout=true` or batch Apex.
  • 🔬 Batch Apex is introduced as a method for processing large sets of records with limitations on the number of jobs that can be scheduled and executed.
  • 🔄 Batch Apex has three key methods: `start`, `execute`, and `finish`, each serving a specific purpose in the batch process.
  • 🔍 Batch Apex can be stateless by default, but the `Database.Stateful` interface can be implemented to maintain state across multiple transactions.
  • 🚨 Error handling in Batch Apex can be achieved by implementing the `Database.ErrorPlatformEvent` interface, which is triggered automatically on errors or exceptions.

Q & A

  • Why doesn't the future method support the SObject data type?

    -The future method in Salesforce does not support SObject data types because it is not guaranteed that the SObject passed as an argument will remain unchanged by the time the future method executes. To handle this, Salesforce recommends passing the ID and querying the latest SObject record within the future method.

  • What is an alternative way to pass SObject data to a future method if you are certain it won't change?

    -As an alternative, you can pass the SObject by serializing it to a string and then deserializing it within the future method. However, this is not the recommended approach due to potential issues with data integrity and the complexity it introduces.

  • Can you call one future method from another future method?

    -No, Salesforce does not allow chaining of future methods. Each future method must be invoked independently, and this is one of the limitations of using future methods.

  • Is it possible to call a future method from a batch job?

    -No, Salesforce does not allow calling a future method from a batch job due to the risk of hitting the governor limits on the number of future method invocations per transaction.

  • What is the limit on the number of future method invocations per transaction?

    -The limit on the number of future method invocations per transaction is 50. This is to prevent hitting the governor limits that could cause the transaction to fail.

  • Can you schedule a future method to execute periodically?

    -No, future methods are designed for one-time asynchronous execution and cannot be scheduled to run periodically.

  • What are some key limitations of future methods that a developer should be aware of?

    -Key limitations of future methods include: they are not suitable for processing large sets of records, they do not support chaining, they only support primitive data types, and they do not allow for programmatic tracing of their execution status.

  • What is the maximum number of jobs that can be scheduled at a given time in Salesforce?

    -In Salesforce, you can schedule a maximum of 100 jobs at a given time.

  • How can you count the number of scheduled jobs in Salesforce?

    -To count the number of scheduled jobs in Salesforce, you can query the `CronTrigger` object where the `JobType` is equal to 'S', which represents Scheduled Apex.

  • Can you write a sample Batch Apex class?

    -A sample Batch Apex class should implement the `Database.Batchable<SObject>` interface and include the `start`, `execute`, and `finish` methods. The `start` method is responsible for collecting records, the `execute` method performs operations on these records, and the `finish` method is used for any post-processing tasks such as sending confirmation emails or submitting additional jobs.

  • What is the significance of the three methods in Batch Apex: start, execute, and finish?

    -The `start` method is called at the beginning of the Apex job to collect records for processing. The `execute` method performs the actual operations on the collected records. The `finish` method is called after all batches have been processed and can be used for tasks like sending confirmation emails or submitting additional jobs.

  • What are the two return types for the start method in Batch Apex and when should each be used?

    -The two return types for the start method in Batch Apex are `Database.QueryLocator` and `Iterable<SObject>`. The `Database.QueryLocator` is used when you need to query a large number of records directly from the database, up to 50 million. The `Iterable<SObject>` is used when you need custom control over how records are iterated and processed, such as when working with a custom collection of records.

  • What is the difference between using the Database.QueryLocator and the Iterable interface in Batch Apex?

    -The `Database.QueryLocator` allows for querying up to 50 million records directly from the database, bypassing the standard SQL query limits. The `Iterable<SObject>` interface provides custom control over iterating and processing records, which is useful when working with custom collections or CSV files, but it is subject to the standard governor limits for SOQL queries.

  • What is the purpose of the Database.StateMachine interface in Batch Apex?

    -The `Database.StateMachine` interface in Batch Apex is used to maintain state across multiple transactions within a batch job. This is useful when you need to share information or counts across batches, such as tracking the number of successful or failed records processed.

  • Can callouts be made within a Batch Apex job?

    -Yes, callouts can be made within a Batch Apex job by implementing the `Database.AllowCallouts` interface. This allows the batch job to make external HTTP requests as part of its processing.

  • How is error handling implemented in Batch Apex?

    -Error handling in Batch Apex is implemented by implementing the `Database.ErrorPlatformEvent` interface. This event is automatically fired whenever an error or exception occurs within the batch job, allowing you to handle errors in a centralized manner.

  • What are some key limits of Batch Apex that a developer should be aware of?

    -Key limits of Batch Apex include: a maximum of five jobs can be queued or active at a time, a maximum of 50 million records can be processed if using `Database.QueryLocator`, a maximum of 10 callouts can be made from each of the start, execute, and finish methods, a maximum of 250,000 batch executions are allowed per 24 hours, and only one batch Apex start method can run at a time in an organization.

  • What is the default batch size for Batch Apex if the optional scope parameter is not used?

    -The default batch size for Batch Apex, if the optional scope parameter is not specified, is determined by Salesforce and typically allows for efficient processing within the governor limits.

  • How can you execute a batch job from the developer console?

    -To execute a batch job from the developer console, you would typically use the `System.enqueueJob()` method, passing in an instance of your batch class.

  • How can you execute a schedulable job from the developer console?

    -To execute a schedulable job from the developer console, you would call the `System.schedule()` method with the appropriate parameters, such as the job's class and any required arguments.

Outlines

00:00

🤖 Apex Interview Q&A Series Introduction

In this video segment, the speaker, Jen, introduces part five of the Apex interview preparation series on her YouTube channel, 'The Journey with Jen.' She outlines the content of the video, which includes answering frequently asked questions about asynchronous Apex based on community inputs from LinkedIn and YouTube comments. Jen encourages viewers to continue submitting questions and to connect with her on various platforms. The first topic discussed is the limitation of the 'future' method in Salesforce, specifically why it doesn't support 'SObject' data types.

05:01

🔄 Future Methods and Their Limitations

This paragraph delves into the restrictions and recommended practices when using 'future' methods in Salesforce. It explains why 'future' methods only accept primitive data types and not 'SObject' types, due to the potential for data changes during asynchronous execution. The recommended approach is to pass IDs and query the latest records within the 'future' method. An alternative, though not recommended, method involves serialization and deserialization of 'SObject' data. The paragraph also covers other 'future' method limitations, such as the inability to chain 'future' methods or call them from batch jobs, and provides the maximum number of 'future' method invocations allowed per transaction.

10:01

📚 Batch Apex Fundamentals and Best Practices

The speaker provides an overview of Batch Apex, discussing its structure, methods, and best practices. Batch Apex is composed of three mandatory methods: 'start', 'execute', and 'finish', each serving a specific purpose in the batch process. The 'start' method is responsible for record collection, 'execute' performs the operations on these records, and 'finish' is used for post-processing tasks. The paragraph also addresses the differences between 'Database.QueryLocator' and 'Iterable' interfaces in Batch Apex, explaining when to use each based on the scenario, such as processing large datasets or iterating over records in a customized manner.

15:03

🔍 Deep Dive into Batch Apex Interfaces and Error Handling

This section offers an in-depth look at the interfaces used in Batch Apex, such as 'Database.QueryLocator' and 'Iterable', and their specific use cases, like handling large datasets or custom record iteration. It also introduces the concept of 'stateless' and 'stateful' Batch Apex, explaining how stateful implementations can maintain information across multiple transactions. Additionally, the paragraph covers the ability to perform callouts within Batch Apex and the importance of error handling through the 'Database.PLATFORM_EVENT' interface.

20:04

🚀 Batch Apex Execution Limits and Practical Considerations

The final paragraph focuses on the key limitations and practical aspects of executing Batch Apex. It highlights the maximum number of jobs that can be queued or active at a time, the limits on record processing, and the constraints on callouts within the 'start', 'execute', and 'finish' methods. The paragraph also touches on the maximum number of batch executions allowed within a 24-hour period and the rule that only one Batch Apex 'start' method can run at a time, while multiple 'execute' methods can run in parallel.

Mindmap

Keywords

💡Apex

Apex is a strongly typed, object-oriented programming language that is used to extend the capabilities of the Salesforce platform. In the video, Apex is the main focus as the script discusses various aspects of Apex interview preparation, including its methods, limitations, and best practices.

💡Future Method

A future method in Apex is a way to execute code asynchronously, which means it does not block the main application thread. The script mentions that future methods do not support SObject data types due to the potential for data changes during execution, and it provides alternative approaches to handle this limitation.

💡SObject

SObject is a term in Salesforce used to refer to any record or object within the system, such as Account, Contact, or Custom_Object. The script explains that SObjects cannot be passed to future methods due to the risk of data inconsistency, and instead, IDs should be passed and queried within the future method.

💡Asynchronous Execution

Asynchronous execution in Apex refers to running code in the background without waiting for the process to complete. The video discusses the use of future methods and batch Apex for asynchronous processing, highlighting their differences and appropriate use cases.

💡Batch Apex

Batch Apex is a way to process large sets of data asynchronously in chunks, known as batches. The script provides an overview of batch Apex, including its methods (start, execute, finish), limitations, and the use of the Database.Batchable interface.

💡Database.QueryLocator

Database.QueryLocator is a way to return a query from the start method of a Batch Apex job, allowing for the processing of up to 50 million records. The script explains its use for collecting records for batch processing and its advantages over standard SOQL queries.

💡Iterable

Iterable is an interface in Apex that allows for custom iteration over a set of records. The script discusses the use of the Iterable interface in Batch Apex for scenarios where custom control over record processing is needed, such as processing records from a CSV file.

💡Stateful

Stateful in Batch Apex refers to maintaining state across multiple batches of execution. The script explains how to implement the Database.Stateful interface to keep track of information like success and failure counts across batches.

💡Callout

A callout in Apex is a network request made to an external service or web application. The script clarifies that Batch Apex supports callouts and that the Database.AllowsCallouts interface must be implemented to perform such actions.

💡Error Handling

Error handling in Batch Apex is crucial for managing exceptions and ensuring data integrity. The script describes the implementation of the Database.ErrorPlatformEvent interface to automatically handle errors that occur during batch execution.

💡Governor Limits

Governor limits in Apex are constraints that prevent resource exhaustion and ensure equitable use of the platform. The script mentions these limits in the context of Batch Apex, explaining that they affect the number of records that can be processed and callouts that can be made.

Highlights

Introduction to part five of the Apex interview preparation series with Jen.

Discussion of frequently asked questions based on asynchronous Apex.

Explanation of why Future methods do not support SObject data types.

Recommendation to pass IDs instead of SObjects in Future methods.

Alternative approach using serialization and deserialization of SObjects in Future methods.

Clarification that chaining Future methods is not possible.

Limitation that Future methods cannot be called from a batch job.

The limit of 50 Future method invocations per transaction.

Inability to schedule Future methods for periodic execution.

Key limitations of Future methods, including primitive data type support and lack of tracing.

Introduction to Scheduled Apex, including the maximum number of jobs that can be scheduled.

How to find scheduled jobs in Salesforce and the use of the Cron Trigger object.

Scheduled Apex does not support call-outs, but workarounds are available.

Explanation of how to count the number of scheduled jobs using the Cron Trigger object.

Sample Batch Apex class example for beginners, emphasizing the importance of implementing interfaces and methods.

Description of the three methods in Batch Apex: start, execute, and finish.

Difference between Database.QueryLocator and Iterable in Batch Apex.

Use case for Iterable interface when processing CSV files in Batch Apex.

Stateful vs. stateless Batch Apex and when to use each.

How to implement call-outs in Batch Apex using the Database.AllowsCallouts interface.

Error handling in Batch Apex through the Database.ErrorPlatformEvent interface.

Key limits of Batch Apex, including the number of queued jobs and record processing limits.

Invitation for viewers to attempt answering additional questions related to Batch Apex.

Closing remarks and call for comments and further questions from the audience.

Transcripts

play00:01

[Music]

play00:06

hello everyone my name is an Jen and I

play00:09

welcome you all to my channel te Journey

play00:11

with

play00:12

anit this is part five of our Apex

play00:14

interview pration series and in this

play00:16

part we'll be discussing the frequently

play00:18

asked questions based on

play00:20

asynchronous before this part we have

play00:22

published few more interview PR series

play00:24

part and in this series we are

play00:26

discussing all those questions that you

play00:28

folks only have shared with me either on

play00:31

LinkedIn or on my YouTube comments

play00:33

videos as well

play00:35

so in case there are a few more

play00:38

questions that you want me to cover do

play00:40

let me know again in the comment section

play00:42

I will make sure that to cover those in

play00:43

the future videos as well so if you

play00:46

folks like to connect with me you can

play00:48

definitely subscribe to my YouTube

play00:49

channel you can connect with me on the

play00:51

LinkedIn and also join our telegram

play00:53

channels as well where we discussed all

play00:56

the different types of questions that

play00:57

you folks only encounter in your

play01:00

preparation

play01:01

Series so the first question that we'll

play01:04

be discussing for today is why does the

play01:06

future method not support the S object

play01:09

data type we all know that in the future

play01:11

method we can only pass the primitive

play01:13

data type that we all know but most of

play01:15

the times I have seen that people not

play01:17

able to answer why the Salesforce

play01:20

restrict to not pass the S object data

play01:22

type as an argument the reason being

play01:25

here is it is not guaranteed that when

play01:27

your future method will be execute so

play01:30

that the S objects that you are passing

play01:32

it as an argument might change when the

play01:34

future method will be executed and for

play01:37

that reason Sal force does not allow you

play01:39

to pass the S object type as a

play01:41

parameter the way you should handle this

play01:43

kind of a scenario here is you should

play01:45

pass the ID and in your future method

play01:48

you should go and make the query on

play01:49

those record as you can see in this

play01:51

example what we are doing here is we are

play01:53

passing here the ID and after passing

play01:56

the ID we are fing the latest s object

play01:58

record so that is the recommended

play01:59

approach from the Salesforce whenever

play02:01

you have to work on the S object but

play02:04

many times I have seen that people do

play02:05

apply one alternate way to handle this

play02:08

kind of a scenario where they are sure

play02:10

that your s object will not be changing

play02:12

although I'll not recommend to go for

play02:14

that approach but in case in your

play02:15

interview they have asked for the

play02:17

alternate way you should also say that

play02:19

by using the d uh serializing and the

play02:21

deserializing approach you can pass the

play02:24

S object the way you should do this here

play02:26

is in your future method you will accept

play02:28

the input as a string and as soon as you

play02:30

receed the input as a string you should

play02:32

go and deserialize it however while

play02:35

invoking the future method in case you

play02:38

have to pass any s object pass it in the

play02:40

form of serialized way so as soon as you

play02:42

are receiving the serialized string in

play02:44

the first statement in your future

play02:46

method should be go and deserialize that

play02:48

and process the S object again I'm

play02:51

repeating it's not the recommended way

play02:53

it just an alternative way to do

play02:55

that so there are few frequently asked

play02:58

questions mostly a oneliner question

play02:59

questions that you should know uh if you

play03:02

are appearing for the interview for

play03:04

example can we call a future method from

play03:06

another future method the answer is no

play03:08

the chaining is not possible with the

play03:10

help of future method and that is

play03:11

another limitation of the future method

play03:14

so we cannot call another future method

play03:16

from one future method another question

play03:18

here is can we call a future method from

play03:20

a bad job again the answer is no we

play03:23

cannot call a future method from a bad

play03:26

job as well the reason being we

play03:28

discussed in the last session or I

play03:30

believe we discussed in the today's

play03:32

session as well that maximum 50 future

play03:34

jobs can be invoked in one transaction

play03:36

if you are invoking the future methods

play03:40

from the batch Apex if there's high

play03:42

probability of hitting the limits that's

play03:44

why sales for does not allow you to do

play03:46

that again there's a workaround for here

play03:48

as well uh I have seen manyu of blogs

play03:51

many blogs over the web where they have

play03:52

put a work around that you can do that

play03:55

using the web service personally I have

play03:56

never tried so I cannot comment on that

play03:59

but yes that is also one of the approach

play04:01

that is possible next one here is what

play04:04

is the limit on the number of future

play04:05

methods invocation per transaction just

play04:08

now I'm talking about the limit of

play04:10

future methods that you can invoke in

play04:12

per transaction is 50 and because of

play04:14

this limit only Salesforce does not

play04:16

allow you to invoke the future methods

play04:18

from batch UPS next one can you schedule

play04:21

a future method to execute for

play04:23

periodically no again we cannot schedule

play04:25

the future methods they are designed for

play04:27

the onetime asynchronous execution ution

play04:30

approach so scheduling of future method

play04:32

to execute periodically is also not

play04:36

possible next one here is what are the

play04:39

key limitations of the future methods

play04:41

that you should keep in mind first one

play04:43

here is future methods are not something

play04:45

which you should use whenever you have

play04:47

to process the large set of Records

play04:49

process the large set of Records

play04:51

Salesforce have a specific asynchronous

play04:53

Opex approach that is the batch so you

play04:55

should go for that option only as I said

play04:58

we cannot invoke one future method from

play05:00

another future method so chaining is

play05:02

also not possible with the help of

play05:04

future method next this one also we

play05:07

discuss just now it only supports the

play05:09

primitive data types you cannot pass s

play05:11

objects or collection of s objects as a

play05:14

parameter whenever you are working on

play05:16

the future method and the tracing is not

play05:19

possible with the future method

play05:21

programmatically you cannot track

play05:23

whether the future method is completed

play05:25

or not this is possible to achieve in

play05:28

other asynchronous approach like the

play05:30

quable schedulable or the batch appex

play05:32

but it is not possible to do that

play05:34

because whenever your future method is

play05:36

submitting it is not returning the job

play05:38

ID and as you don't have the job ID it

play05:41

is not possible to do that tracing next

play05:43

one here is it can't be called from the

play05:46

batch that is another limitation of the

play05:48

future method it cannot be called from

play05:51

the

play05:53

batch another few questions and these

play05:56

questions are specifically based on the

play05:57

scheduel Apex now what is the maximum

play06:00

number of jobs that can be scheduled at

play06:02

a given time so in your hug you can only

play06:06

schedule maximum 100 jobs and how you

play06:09

will find which jobs are scheduled so

play06:11

for that you have to go to the setup and

play06:13

in the setup you will put the scheduled

play06:16

jobs you will see all the scheduled jobs

play06:18

in your Aug another question here is

play06:21

does scheduled effect support the call

play06:23

out again the answer is no your

play06:26

scheduled aex does not support the call

play06:28

out but there are two work arounds that

play06:29

you can do that you can do that by

play06:32

invoking the future method where the

play06:34

future method is annotated as call out

play06:36

is equal to true you can also do that

play06:38

from the batch aex as well next one here

play06:41

is how to count the number of schedule

play06:43

jobs so whenever you have to count any

play06:46

schedule job you have to go for the

play06:48

object that is the crown job details

play06:50

object and in sorry in the you have to

play06:52

go to the crown trigger object and in

play06:54

that Crown trigger object you will look

play06:56

for the crown job details. job type is

play06:58

equal to

play07:00

s here represents the scheduel Apex so

play07:02

whenever you have to programmatically

play07:04

count how many number of schedule jobs

play07:06

has been configured you will make a

play07:08

query to the crown trigger object okay

play07:11

this is how you can achieve it next one

play07:15

can you write a sample batch PEX class

play07:18

again many times I have seen that

play07:20

specifically for the freshers this

play07:21

question has been asked uh where they

play07:24

will ask you to write down the batch

play07:26

effect again here the intent is not to

play07:28

check the tax but to remember whether

play07:31

you should remember to implement the

play07:33

particular interface or not whether you

play07:35

should remember to write down the

play07:36

particular method names or not right

play07:39

start execute and finish method whether

play07:41

you are able to write down these methods

play07:43

or not right whether you are able to put

play07:45

the correct return return type for the

play07:46

start method or not okay so your start

play07:49

method have the two return types either

play07:51

you can go for the databasequery locator

play07:54

or you can go for the itable as well so

play07:57

here I'm giving an example of a database

play07:59

do query locator where I'll be putting

play08:01

the query and return those records as

play08:03

well okay so this is how the typical

play08:05

batch Apex class will look like so make

play08:07

sure that this you should definitely

play08:09

practice by writing down the batch aex

play08:10

class by yourself you can keep writing

play08:13

the class something like this make sure

play08:14

you executing it as

play08:18

well next question can you explain about

play08:20

the three methods of the batch appex as

play08:22

I shown you we do have the three methods

play08:24

available in the batch appas one is the

play08:26

start method second is the execute

play08:28

method and third is the finish method

play08:30

now the question here is what is the

play08:32

significance of each and every method

play08:34

which is available in the batch Apex so

play08:36

the first method that we do have here is

play08:38

the start method this method will be

play08:40

automatically called at the beginning of

play08:42

the Apex job the first method of the

play08:45

batch appex that will be called here is

play08:47

the start method this method is

play08:49

basically responsible to collect the

play08:51

records that needs to be processed by

play08:53

the batch Tex so the main purpose of the

play08:55

start method here is the collection

play08:57

method collection of Rec second one here

play09:00

is the execute method execute method

play09:02

performs an operation which we want to

play09:05

perform on those record so whatever the

play09:07

uh business logic that you have to

play09:09

execute on the selected records you will

play09:11

put that business logic into the execute

play09:14

method and the third one is the Finish

play09:16

method finish method will execute after

play09:19

all the batches has been processed in

play09:21

case you have to send any confirmation

play09:23

email or you have to submit another bad

play09:26

job you can do all those stuff under the

play09:28

Finish method again these methods are

play09:31

mandatory to implement whenever you are

play09:34

using the database. batchable interface

play09:37

if you are not implementing any of this

play09:39

method you will get a compile time

play09:43

error next one as I said your start

play09:46

method can have the two return types one

play09:48

is the database do query locator and

play09:50

another one here is the iterable as well

play09:53

so first let's go and talk about what is

play09:55

database. query locator when you should

play09:58

go for the database do query locator and

play10:00

when you should go for the itable

play10:03

interface so in your start method if you

play10:05

are to include the code that can collect

play10:07

the records method and then pass those

play10:09

collected records to the execute method

play10:12

then you should go for the database.

play10:14

query locator the key part to identify

play10:17

with the database. query locator here is

play10:19

by using this you can query up to 50

play10:22

million records using this you can query

play10:25

up to 50 million record so in case you

play10:28

do have a requirement where where you

play10:29

have to directly make a query on the S

play10:31

objects and F those records prefer to go

play10:35

for the database do query

play10:37

locator because by using this database.

play10:40

query locator you are passing the limit

play10:42

of the SQL queries you guys have to let

play10:46

me know in the comment section how many

play10:48

records we can Fage by using the SQL

play10:50

query right and that limit we are

play10:52

bypassing by using the database. query

play10:56

locator so if the start method returns

play10:58

at query Loc

play11:00

optional scope parameter this parameter

play11:02

is also very important whenever we are

play11:05

executing the batch aex we always pass

play11:07

the optional scope parameter right it's

play11:10

not mandatory in case you are not

play11:11

passing then what is the default value

play11:13

again do let me know in the comment

play11:14

section I believe I do have that covered

play11:16

in the upcoming slides as well but in

play11:18

case you have watched till here do let

play11:20

me know what is the default value of the

play11:22

optional scope parameter so if you are

play11:24

not passing that value it will work on

play11:26

that value but in case you are passing

play11:29

that value then the maximum value that

play11:31

you can put here is the 2,000 so in the

play11:34

case of database. query locator the

play11:37

maximum value of the optional scope

play11:39

parameter here is 2,000 these are the

play11:42

three key point that you should be

play11:44

answering whenever anyone ask you about

play11:46

the database do query locator and if

play11:49

this they ask you to write down the

play11:50

syntax this is how it will look like you

play11:52

have the database do query locator as

play11:54

the return type in the start method and

play11:57

how to return those records for that we

play11:58

have to to execute the method database.

play12:01

getquery locator okay and here you can f

play12:04

up to the 50 million

play12:07

R next one here is the iterable

play12:10

interface in the batch appex now what is

play12:12

the need of having the two interfaces

play12:14

one is the database. query locator and

play12:16

second is the itable I have seen many of

play12:19

the people not worked on this itable

play12:21

interface and this question specifically

play12:23

comes for the experiened professional

play12:25

right so they might ask you whether you

play12:27

have worked on the ital interface and if

play12:30

you said yes they they might ask you the

play12:32

specific scenario okay so the specific

play12:35

scenario that I work in my experience

play12:37

here was whenever I have to go and

play12:40

process the CSV file into the batch Apex

play12:43

I have preferred to use the itable

play12:46

interface okay so in the start method

play12:49

you can include the code provided the

play12:51

developers with the power to iterate

play12:53

over the record so where you should go

play12:55

and Define this interface in the start

play12:57

method you should go and Define that

play12:59

interface you will provide the code uh

play13:02

with the power to iterate over the

play13:04

records in a highly customized manner in

play13:06

the case of database. query locator what

play13:09

you are doing you are making the records

play13:12

queried from the S object but what if

play13:14

you already getting let's say collection

play13:16

or in my scenario as I explained I am

play13:18

already having the CSV file I have to

play13:20

process that collection or the CSV file

play13:23

it is not possible to do that by using

play13:25

the database. query locator for example

play13:28

I have to put an highly customized way

play13:30

of iterating the records again that is

play13:32

not possible with the database. query

play13:34

locator so in such scenarios we will go

play13:36

for the iterable interface so using the

play13:40

iterator interface you can create a

play13:42

custom set of instructions that's what

play13:44

it is allow it will allow you to write

play13:46

down the custom set of instructions for

play13:48

traversing a list through the loop okay

play13:51

you can Traverse any correction for

play13:53

example as I said you do have the huge

play13:55

number of Records available in the

play13:57

collection and you have to process those

play13:59

records one after the other using the

play14:01

batch appex your the option for you is

play14:03

to go for the itable interface if you

play14:07

use an itable again that is this is what

play14:09

you have to keep you remember with

play14:11

respect to the governor limit if you are

play14:13

using the itable interface then you

play14:15

still have the governor limits imposed

play14:17

by the S soql query so the point here is

play14:20

if you are using the iterable interface

play14:22

you cannot make or you cannot fish or

play14:25

cannot process 50 million

play14:27

records okay that option is not possible

play14:30

if you are using the itable interface so

play14:33

50 million record option is only

play14:35

possible whenever you are going for the

play14:37

database do query locator it is not

play14:39

possible with the iterable interface so

play14:42

this is how the typical syntax of the

play14:43

iterable interface will look like here

play14:45

the return type is

play14:47

itable right here the return type is

play14:50

iterable and what you have to return

play14:51

here here is your itable class so for

play14:54

example in this scenario this is my

play14:56

itable class and I am returning the

play14:58

instance of the iterable class now how

play15:00

to implement this iterable class so I

play15:02

have put some snippet here for your

play15:04

reference so for example here you can

play15:06

see this is my itable class which is

play15:08

implementing itable

play15:10

interface and as we are implementing

play15:12

this itable interface it is mandatory to

play15:15

implement the

play15:16

iterator it is mandatory to implement

play15:19

the iterator now here I have implemented

play15:22

the iterator method and what is the

play15:23

return type of my iterator it is the

play15:26

iterator of account

play15:29

what is the return type here is the

play15:30

iterator of account and what I am

play15:32

returning from this method is the

play15:34

iterator of account and this is how my

play15:36

iterator method will look like so here

play15:38

you can see that I do have a class which

play15:40

is implementing this iterator method and

play15:42

now I'm implementing the iterator method

play15:45

it is mandatory to implement the two

play15:47

methods this is the first method that is

play15:49

has next and second one here is the next

play15:52

method has next method will decide

play15:54

whether there's a next record is

play15:56

available to process or not that will be

play15:58

def by the has next method what needs to

play16:01

be processed whether the record needs to

play16:04

be processed or not what business logic

play16:06

needs to be process you will put that

play16:07

logic under the next

play16:09

method okay so for example if I have to

play16:12

run this example what I am doing in my

play16:14

iterator example here is I am making the

play16:16

query on the

play16:18

account okay and what I have to do here

play16:21

is I have to only process the eight

play16:22

records a hypothetical scenario

play16:25

definitely many of you will say that it

play16:27

is possible to achieve using the limit

play16:29

but just for our understanding I don't

play16:31

want to do it with the limit uh I want

play16:34

to do it as an iterator interface here

play16:36

so what I am doing here is I have put

play16:38

here I is equal to 8 I checking when the

play16:41

I value is incrementing to 8 or not I

play16:43

have initialized this value to zero so

play16:45

when my first time this method will

play16:47

execute right it will check whether the

play16:49

I value is greater than the account size

play16:51

the answer is

play16:53

yes it will go and return me that

play16:56

account right like this I am checking

play16:59

unless and until I am meeting this

play17:01

criteria if I meet this criteria then

play17:03

and then only I will stop

play17:06

processing so this is how you can go and

play17:09

use the iterator method whenever you

play17:11

want to have the custom control how to

play17:14

process

play17:17

those now next question here is what is

play17:20

the state in the batch appex or what is

play17:23

database.

play17:25

stateful very frequently Asked question

play17:27

specifically for the PR and also for the

play17:30

experience professional as well what is

play17:32

state in the batch appex so do remember

play17:34

that by default your batch appex is

play17:37

stateless what does the stateless means

play17:40

if the batch appex have processed some

play17:42

information in the first batch of

play17:44

execution it will not be carried forward

play17:46

in the next batch so that's what I have

play17:49

put up here if a batch process and needs

play17:51

information that is shared across

play17:53

transaction then you have to go and use

play17:56

the stateful interface by default it is

play17:58

stat list if you are putting the stat

play18:01

list means the information that you have

play18:03

processed in the previous batch it is

play18:04

not carried forward in the next batch if

play18:07

you have to make sure that the

play18:08

information is needs to be shared across

play18:10

the multiple transactions then go and

play18:13

use the database. stateful interface if

play18:17

you specify the database. stateful in

play18:19

the class definition you can maintain

play18:22

the state across this

play18:24

transaction across the transaction you

play18:26

have to maintain the state go and

play18:28

implement this for example if your

play18:30

batchable job is executing one logic and

play18:33

you need to send an email at the end of

play18:35

the batch job where you have to go and

play18:37

put how many successful records and the

play18:40

failed records so what you have to do

play18:41

here is here you have to go and send the

play18:43

summary of how many successful records

play18:45

that you have processed how many failed

play18:47

records that you have processed and you

play18:49

want that count should be coming up

play18:51

across the batches right not from one

play18:54

single batch then in such scenarios you

play18:57

go and implement the dat database.

play18:59

stateful interface so this is an example

play19:02

how you should do that so here you will

play19:03

be putting up my bad job right and here

play19:07

you have to make sure that you are

play19:08

implementing this interface if you are

play19:11

implementing this interface whatever the

play19:13

primitive data types that you have

play19:15

defined here this Prem data types will

play19:18

hold the values across the transaction

play19:20

so here you can see that I have

play19:22

initialized that value as zero and in my

play19:24

multiple batches I am incrementing the

play19:27

value so what will happen how many

play19:29

records that has been processed by this

play19:31

particular variable if I have to check

play19:33

under the Finish method I can easily do

play19:35

that because now I have implemented the

play19:38

database. stateful that means the value

play19:41

of this variable will be persisted

play19:43

across the multiple batches as well very

play19:46

helpful interface very frequently we use

play19:49

in our real world examples as

play19:51

well next one can we do the call outs in

play19:55

the batch ofex again the answer to this

play19:58

is yes we can do the call outs in the

play20:00

batch aex as well and to do the call

play20:02

outs we have to implement another

play20:04

interface that is the database do

play20:06

callout

play20:07

interface we have to go and Implement

play20:10

another interface that is the database.

play20:13

Hout interface and this is how the

play20:14

syntax will look like here you have to

play20:16

go and specify database. allow Callum

play20:20

sorry it's it's a typo from my side

play20:22

right but this is the correct interface

play20:24

name that you have to implement

play20:26

database. allows column

play20:31

next one how to do the error handling in

play20:33

batch app and this is again one of the

play20:34

favorite question specifically for the

play20:36

experience professional how you can do

play20:39

the error handling in the batch X and

play20:42

the very good part here is it is very

play20:44

easy to implement so any batch Apex can

play20:46

Implement an interface here that is the

play20:49

database. race platform event so this

play20:52

event will be automatically invoked

play20:55

automatically fired whenever your batch

play20:57

upex encounter any error or exception so

play21:00

your answer should be to implement the

play21:02

error handling in the batch appex we

play21:04

should go and Implement an interface

play21:06

that is database. R platform event and

play21:09

this interface will be sorry this event

play21:11

will be automatically invoked whenever

play21:14

any error or exception occurred in our

play21:17

code how to implement that it's very

play21:20

simple database. R

play21:23

platform I have put a very detailed

play21:25

video how you can go and do the error

play21:27

handling with the help of of batch aex

play21:29

under my platform event series if you

play21:32

are preparing for it and want to know

play21:34

more about the error handling in the

play21:35

batch aex make sure that you folks check

play21:38

that out as well here there I have

play21:39

written down the code and actually shown

play21:41

you the scenario how the multiple task

play21:43

will be created in case any error Ed in

play21:46

your batch Appice so do check that

play21:49

out now what are the key limits of the

play21:52

batch aex that you should know for the

play21:55

batch aex the key limit that you should

play21:57

know here is to five job can be queued

play22:01

or at a time active for the batch up to

play22:05

five cued or active jobs are allowed for

play22:08

the batch your batch appex can process

play22:12

million record if you are using the

play22:14

database. query locator in case there

play22:17

are more than 50 million records again

play22:19

your batch appas will terminate

play22:22

immediately and will Mark that complete

play22:25

process as failed next one here is the

play22:28

start execute and finish method can

play22:31

Implement 10 call outs each so in the

play22:34

start method execute method and the

play22:37

Finish method you can implement the 10

play22:40

call out each next one this limit is

play22:43

very important the maximum number of

play22:45

batch execution here is

play22:48

250,000 250,000 for 24

play22:52

hours the maximum number of batch

play22:55

execution here is 250,000 per 20 4 hours

play22:59

again if you are hitting the limit your

play23:01

batch appex will not be processed and

play23:03

the last one here is which is again not

play23:05

known to many people only one batch aex

play23:07

job start method can run at a time in an

play23:10

organization in your or at a time only

play23:14

the one patch effect start method is

play23:16

running in case that method is running

play23:19

other methods will be into the queue

play23:21

again they they will not fail they will

play23:23

be stay they will be in the queue right

play23:27

however multiple exec methods can run in

play23:29

parallel so keep a note that only one

play23:31

start method can be running at a time

play23:34

batch aex that haven't started yet

play23:36

remain in the queue until they are

play23:40

start now these are the few questions

play23:43

that I want you guys to attempt and let

play23:45

me know the answers in the comment

play23:47

section the first one here is what is

play23:49

the batch size if we don't use the

play23:51

optional scope parameter what is the

play23:53

default bat size for the batch effects

play23:57

next one here is

play23:59

how you can execute the badge job which

play24:02

method you will use to execute the badge

play24:04

job which method you will use to execute

play24:06

the quable job and which method you will

play24:08

use to execute the shable job the

play24:11

schedulable job can be executed by a two

play24:14

approach first one from your UI approach

play24:17

so I'm not asking here about from the UI

play24:19

I'm asking about from the developer

play24:22

console if I ask you to execute the

play24:24

schedule level appex which command you

play24:26

will use and what parameters you will

play24:28

pass

play24:29

again I do insist do try to attempt this

play24:31

questions the reason I give this

play24:33

question as I mentioned in the past

play24:34

videos as well so that you can you you

play24:37

are also encouraged to do your own self

play24:39

study as well I hope you folks found

play24:43

this video helpful do let me know in the

play24:45

comment section if you do have any

play24:46

questions or any queries or your thought

play24:49

about this series as

play24:51

well moreover any other question that

play24:54

you think we have not covered in this

play24:56

series and we should be covering it

play24:58

again again let me know in the comment

play25:01

section thank you folks that's all I do

play25:03

have for today bye-bye

play25:09

[Music]

Rate This

5.0 / 5 (0 votes)

الوسوم ذات الصلة
Apex InterviewAsynchronousSalesforceFAQsBatch ApexFuture MethodsInterview TipsCalloutsError HandlingDeveloper Console
هل تحتاج إلى تلخيص باللغة الإنجليزية؟