Salesforce Question

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 18

What are the limitations of batch apex in Salesforce?

Batch Apex in Salesforce is designed to efficiently handle large data sets while respecting the
platform's governor limits. Key limits include: Total number of records retrieved by SOQL
queries: 50 million records. Maximum batch size: Default is 200 records per batch, but can be
set up to 2,000

What are the SOQL limitations in Apex?

When working with SOQL, you'll most likely be using it within Apex, which has a few more
restrictions around how you can use SOQL: Maximum 100 queries in a synchronous
transaction. Maximum 200 queries in an asynchronous transaction. Maximum 50000 rows
returned per transaction.

How many batches can run at a time?

Batch Apex jobs are limited to five tasks running simultaneously, whereas queueable jobs can
run up to 100!

What are the limitations of future methods in Salesforce apex?

The maximum number of asynchronous Apex method executions (batch Apex, future methods,
Queueable Apex, and scheduled Apex) per a 24-hour period is 250,000 or the number of user
licenses in your organization multiplied by 200, whichever is greater.

What is per day limit of batch apex?

A maximum of five batch jobs can be submitted in case of a running test. There can be up to
250,000 Batch Apex method executions per day or the number of user licenses in an
organization multiplied by 200 — whichever is greater.

Can we call batch apex from batch?

: Yes, we can call another batch class from the Finish method.
How do I query more than 50000 records in Salesforce?

You can get more than 50,000 using a Query Locator. Use Salesforce workbench to query such
records. Workbench uses the salesforce rest/tooling API that doesn't have 50K record limitation.
It might happen that it will fail for 4-5 times due to huge amount of data but it would work.

What is Apex heap size limit Salesforce?

Salesforce enforces an Apex Heap Size Limit of 6MB for synchronous transactions and 12MB
for asynchronous transactions. The "Apex heap size too large" error occurs when too much data
is being stored in memory during processing.
Batch Apex Limits
Batch Apex in Salesforce is designed to efficiently handle large data sets
while respecting the platform’s governor limits. Key limits include:

 Total number of records retrieved by SOQL queries: 50 million records.


 Maximum batch size: Default is 200 records per batch, but can be set up
to 2,000.
 Total number of methods with the @future annotation allowed: 50.
 Heap size limit: 12 MB per synchronous transaction and 6 MB per
asynchronous transaction.

It’s crucial to design Batch Apex classes considering these limits to ensure
efficient and error-free execution.

Advantages of Using Batch Apex in Salesforce


Batch Apex allows users to process large data sets in batches, thus reducing
the strain on the Salesforce servers and improving performance.

1. Automation: Batch Apex enables users to automate complex operations


without writing complex code. It can be used to automate repetitive tasks
such as data cleansing, periodic data updates, and much more.
2. Asynchronous Execution: Batch Apex allows users to schedule batch
jobs to run at regular intervals or during specific times, thus freeing up
resources for other activities. The governor limits are reset for each chunk
of data processed by the execute method because each instance of the
execute method runs in a different context.
3. Efficient Error Handling: The framework provided by Batch Apex provides
users with efficient error-handling capabilities that make it easier to identify
and troubleshoot any issues that may arise during batch processing.
4. Simplified Code Development: Batch Apex eliminates the need for
complex code development, which makes it easier for developers to quickly
implement the required functionality.

Use Cases
Below are a few use cases of Batch Apex:
1. Scheduled Processing: Batch Apex can be used to schedule periodic
tasks, such as sending out emails or cleaning up records that are no longer
needed.
2. Parallel Processing: Batch Apex can be used to execute multiple
operations in parallel and make better use of system resources.
3. Automation: Batch Apex can be used to automate processes in
Salesforce, such as creating new records or deleting old ones.
4. Complex Operations: Batch Apex can be used to perform complex
operations that cannot be done through the standard Salesforce interface,
such as generating complex reports or integrating data from multiple
sources.

Best Practices in Apex batch


1. Keep in mind the governor limits when writing your Batch Apex, as Batch
Apex code can easily hit governor limits if not monitored properly.
2. Avoid using DML statements or SOQL queries inside for loops, as this can
quickly cause errors and result in your Batch Apex hitting governor limits.
3. Use the Database class instead of simple DML statements to manage bulk
records, as this is more efficient than making several DML calls.
4. Test your Batch Apex code thoroughly in a Sandbox environment before
deploying it to production. This will help you identify potential issues before
they affect your production environment.
5. Monitor your Batch Apex jobs closely by leveraging the Salesforce
Monitoring tools in setup to ensure that all jobs are running successfully.
6. Keep your Batch Apex code up to date, and consider adding more batches
as your data grows to ensure that your data is processed quickly and
efficiently.
7. Finally, use try-catch blocks whenever possible, as this will help you catch
any errors that may occur during the execution of your Batch Apex code.

1. Why do we use batch apex in Salesforce?

Batch Apex in Salesforce processes large volumes of data more


efficiently and manageably. It is particularly useful when performing
complex operations or transactions on many records, like data updates,
cleansing, or calculations, without hitting Salesforce’s governor limits.

2. What is the difference between batch apex and Apex?

 Salesforce uses the programming language Apex for bespoke


development. “Apex” refers to synchronous code executed when users
interact with the Salesforce interface, such as triggers, controllers, and
classes.
 On the other hand, “Batch Apex” is a specific type of Apex code used for
processing large data sets asynchronously.
 The key difference is that Batch Apex is designed to handle large-scale
data processing tasks without causing performance issues or hitting
governor limits.

What are the types of batch apex?


The primary types of Batch Apex are:

1. Database.Batchable: This is the main interface you implement to allow for


batch processing. Classes that implement this interface can be executed in
batch.
2. Database.BatchableContext: This interface contains methods that provide
access to job ID or the asyncApexJob object, which can be useful for
tracking the progress of your batch jobs.
3. Database.Stateful: If your batch class implements this interface,
Salesforce ensures that the class variables retain their values between
transactions. This is useful when you need to accumulate data across
multiple batches.
4. Database.AllowsCallouts: Implementing this interface in your batch class
allows you to make callouts (HTTP requests) from your batch class.

4. What are the three methods of batch apex in Salesforce?


Batch Apex requires implementing three essential methods in the Batch Apex
class:

1. start(): Gathers the initial record set for processing, returning a query
locator or iterable defining the record scope.
2. execute(): Processes records in batches (up to 200 at a time), where data
processing logic is implemented.
3. finish(): Executes after all batches are processed, used for cleanup or
post-processing tasks.

How to write batch apex in Salesforce?

5. How to write batch apex in Salesforce?


To create a Batch Apex class in Salesforce:
1. Implement Database.Batchable Interface: Start by creating a new Apex
class that implements the Database.Batchable interface.
2. Define Methods: Implement the start(), execute(), and finish() methods,
outlining the specific logic for each.
3. Deploy and Execute: Use the Salesforce Developer Console or an
Integrated Development Environment (IDE) to deploy and run your Batch
Apex class.

6. How to schedule batch apex in Salesforce?


To schedule a Batch Apex job in Salesforce:

1. Navigate to Setup > Apex Classes.


2. Locate your Batch Apex class and click the “Schedule Apex” button.
3. Set the schedule by choosing the frequency and timing of the job.
4. Save your scheduling settings.

7. How to stop batch apex in Salesforce?


To stop Batch Apex jobs in Salesforce, you have these options:

1. Manual Abortion: Go to Setup > Jobs > Apex Jobs, select the job, and
click the “Abort” button.
2. Programmatic Stop: Use Apex to invoke
the Database.BatchableContext method stop() within the execute() method.
3. Automatic Timeout: Salesforce will automatically abort the job if it runs for
an extended period without completion.

8. How to execute batch apex in Salesforce?

1. Navigate to Setup > Apex Classes.


2. Locate your Batch Apex class and click the “Execute” button.
3. Set any necessary parameters or settings, then start the execution.

What is the difference between SQL and SOQL in


Salesforce?
SOQL vs SOSL

SOQL SOSL

Salesforce Object Salesforce Object Search


Full Name
Query Language Language
List Views, Reports, (Global, Sidebar, Advanced) Search,
Used In:
Apex Apex

Happens Asynchronously. Usually


Synchronously (Can
Indexing 2-3 min. If over 9000 records loaded
have Custom Indexes or
Happens: at one time, the excess are moved to
Standard Indexes)
the bulk index queue. (slower).

Accuracy. Gives full set Relevance & Speed. Similar to


Search
of results that match Google Search. Weightage placed on
Focus:
criteria. recently viewed records.

Search Can search 1 object at a Can search multiple objects at a


Scope time. time.

7 types of apex trigger


Apex triggers enable you to perform custom actions before or
after changes to Salesforce records, such as insertions, updates,
or deletions.

Syntax of Trigger
 Before update: Executes the trigger before a record is updated
 Before insert: Executes the trigger before a record is inserted
 Before delete: Executes the trigger before a record is deleted
 After delete: Executes the trigger after a record is deleted
 After insert: Executes the trigger after a record is inserted
 After update: Executes the trigger after a record is updated
 After undelete: Executes the trigger to recover a deleted record from
recycle bin
https://salesforcegeek.in/salesforce-apex-trigger-examples-part-1/

Synchronous Process
In simple words we can say executing a class based on single thread that is nothing but
execute one method at a time either using top down or bottom to top approach.

Example: Imagine you have a requirement like when you insert a contact or delete a
contact update the account with number of contacts and then you need to perform web
service callout for that account. Using the synchronous apex is no-no situation because
we always need to deal with the governor limits and apex CPU time. So how can we
solve this situation.

Can we solve this problem using the asynchronous process?

Asynchronous process
The answer is yes, asynchronous process will execute in its own thread it is
independent. So how many asynchronous flavors we have in salesforce?

I would say as of today we have total of 4 flavors. Out of 4 do we have any


asynchronous process that is annotation based. So, we need not to write a separate
class and handle the asynchronous process.

Do not worry we have that in Salesforce. It is nothing but @future annotation how cool
it is right. Now we are ready to deep dive into learn about the future asynchronous
process.
FUTURE APEX QUEUEABLE APEX
It is a class which implements’ queueable
It is annotation based so we can use the same apex class to
interface. Syntax: public class CLASS_NAME
write the future method. Syntax: @future
implements Queueable{}

We cannot monitor the jobs We can monitor the jobs based on the job Id.

We can chain the Queueable jobs and the


we cannot call a future from another future or batch apex.
stack depth in developer org is 5 and in
The limit on future method for single apex invocation is 50.
enterprise edition you can chain 50 jobs.

Queueable supports both primitive and non-


Future method supports only primitive datatypes
primitive data types.

Queueable Jobs can contain the member


Future will never use to work on SObjects or object variable as SObjects or custom Apex
types. Types.
When using queueable jobs it will make
When using the future method we cannot monitor the the AsyncApexJob which we can monitor
jobs which are in process. like Scheduled jobs.
Using Queueable Apex will chain up to
queueable jobs and in Developer Edition it
The future method will never be queued. is only 5 Jobs.

when we used batch apex in salesforce?

The Batch class is used to process millions of records within normal


processing limits. With Batch Apex, we can process records
asynchronously to stay within platform limits. If you have a lot of
records to process, for example, data cleansing or archiving, Batch
Apex is probably your best solution.
Why we use batch apex instead of data loader?

Batch apex allows you to define a job that can be divided into manageable chunks,
where each chunk can be proceed separately. In batch apex, it will fetch all records
which you want perform the field update and divide them into list of 200 records and
every 200 records operation is performed separately.

What is the difference between data loader and apex data loader?
Basically Apex data loader is a desktop tool and it is meant for data migration between
two systems. In other words, when you want to move data between SFDC and some
other environment, you go with data loader.

12 Salesforce Apex Best Practices

1. Bulkify Apex Code


2. Avoid SOQL & DML inside for Loop
3. Querying Large Data Sets
4. Use of Map of Sobject
5. Use of the Limits Apex Methods
6. Avoid Hardcoding IDs
7. Use Database Methods while doing DML operation
8. Exception Handling in Apex Code
9. Write One Trigger per Object per event
10. Use Asynchronous Apex
11. Security and Sharing in Apex Code
12. Make reusability of Apex Code
13. Code coverage
14. Return Early Pattern
15. Avoid nesting loops within loops
16. Don’t mix Apex, Process Builders, Workflow Rules, and Record-Triggered flows
17. Naming Conventions.
18. Setup Code review checklist and Code Review process.

12 best practices of trigger in salesforce.

1. One trigger per object


2. Logic Less Trigger
3. Context-specific handler methods
4. Avoid SOQL Query inside for loop
5. Avoid hardcoding IDs
6. Avoid nested for loop
7. Avoid DML inside for loop
8. Bulkify Your Code
9. Enforced Sharing in Salesforce
10. Use @future Appropriately
11. Use WHERE Clause in SOQL Query
12. Use Test-Driven Development

Execution process of apex trigger .

Step 1: Load the original record or initialize on insert.

Step 2: Override the old record values with the new values.

Step 3: Execute all before triggers.

Step 4: Run the system & user-defined validation rules.

Step 5: Save the record but do not commit the record to the database.

Step 6: Execute all after triggers.

Step 7: Execute the assignment rules.

Step 8: Execute the auto-response rules.

Step 9: Execute the workflow rules.

Step 10: If there are workflow field updates then execute the field update.

Step 11: If the record was updated with a workflow field update then execute before and after
triggers created on the object in the context again but only once.

Step 12: Execute the processes and flows on that record.

Step 13: Execute the escalation rules.

Step 14: Update the roll-up summary fields & cross-object formula fields.
Step 15: Repeat the same process with the affected parent or grand-parent records.

Step 16: Evaluate criteria-based sharing rules.

Step 17: Commit all DML operations to the database.

Step 18: Execute post commit logic such as sending emails

How to Use Validation Rules in Salesforce


Validation Rules in Salesforce verify that the data entered by a user meets certain
criteria before the user can save the record. Salesforce Admins set up the rules as statements,
which act like yes/no questions – the answer must be no to all of them. If not, an error message
appears, where the admin has explained what the user must do to correct the record, before
clicking “Save” again.

Use validation rules to maintain user input data or system modified records.

 User input data: Where users may be prone to making mistakes or cutting corners, or when you need data to be in a certain format.
 System modified records: Changes to data triggered by imports, automated processes, integrated systems, etc. will need to abide by validation
rules*.

How to Create a Validation Rule in Salesforce


Validation rules can be broken down into three parts:
1. Name and Description
2. The Rule (the Error Condition Formula)
3. The Error Message

Master-Detail Lookup
Child and Parent exist
independently (field is
Child cannot exist without Parent (field is required not required and can be
and can’t be null or blank) null or blank)
Child and Parent
security settings are set
Child inherits security settings from Parent independently
Cascade delete (deleting Parent deletes all Child Deletion of Parent does
records) not delete Child records
No rollup summary
(appexchange tools
Rollup summary (i.e. count, sum, min, max) available)
Standard objects can be
Standard objects cannot be the child of custom the child of custom
objects objects
Up to 40 lookups on a
custom object (child can
have up to 40 parents)
Can have up to two master-details on an object (child and 25 lookups on a
can have up to two parents) standard object

What is a Junction Object in Salesforce?

Salesforce Junction Objects give you a way to create a many-to-many relationship between
Salesforce objects. They are created using a custom object to relate two other objects via two
master-detail relationships. This is necessary when modeling certain data schemas. Let’s dive
into a couple of examples:

Junction Object Examples


Take a recruitment process where you have a job position and candidate object. As it is a
requirement for candidates to apply to multiple job positions, a standard parent-child relationship
would not work. You need a junction object, called something along the lines of ‘Application’, to
allow many candidates to apply to multiple job positions.
There is also an example within standard Salesforce objects with Opportunity Products. If you
want to assign Products to a certain Salesforce Opportunity, you will need to use the Opportunity
Line items object (a junction object). This is required as, without the Junction Object, you would
only ever be able to associate one product to one opportunity.

What are workflows in Salesforce?


Workflow in Salesforce is basically a container or business logic engine which automates
certain actions based on particular criteria. If the criteria are met, the actions get executed.
When they are not met, records will get saved but no action will get executed.

What are Decorators in Lightning Web Component?


A Decorator is a design pattern that allows adding behaviors to Javascript Objects.
Decorators which are part of ECMAScript are used to dynamically alter or modify the
functionality.

Type of Decorators in Lightning Web Component

https://dev.to/shivamkapasia0/decorators-in-
lightning-web-componentlwc-3379
There are three type of Decorators in Lightning web components.

1. Api
2. Track
3. Wire
4. @api: It is used to expose a variable or functions publically and
make properties reactive.
5. @track: It is used to make variable private but reactive. Tracked
properties are also called private reactive properties.
6. @wire: To read Salesforce data, Lightning web components use
a reactive wire service. When the wire service provisions data, the
component rerenders. Components use @wire in their JavaScript
class to specify a wire adaptor or an Apex method.

@api
To expose a public property or a public method, decorate with @api. Public properties
are reactive, also known as public reactive properties since if a value of property
changes then component is re-rendered
 Public properties define API of a component whereas public methods are part of a
component’s API
 A Component is re-rendered when the value of a referenced public property is modified
or changed
 To pass data from parent component to child component, @api decorator in the child
component exposes a property by making it public so that parent component can
update it
 @api properties can be exposed in an App builder

@track
To expose a private property or a private method, declare with @track. Also known as
Private reactive properties


 To track a private property and to re-render component when that property changes,
use @track decorator (Available only for the component where it is declared)
 Fields which are using @track decorator that contains an object or an array, tells the
framework to observe changes to the properties of an object or elements of an array
 After Salesforce Spring ’20, all the fields in a Lightning Web Component are reactive. If a
field is used in a template & value changes, the component re-renders and displays a
new value by default

. @wire
 Reactive wire service is utilized in Lightning Web Components to read the Salesforce
data from apex class into Lightning web components
 Component is re-rendered when wire service provisions the data from apex class. The
output from apex method is set to a property.

You might also like