0% found this document useful (0 votes)
24 views4 pages

Material 07

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views4 pages

Material 07

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

TOPIC 07: Synchronous Apex:

Overview of Synchronous Apex


Overview:
 Definition: Synchronous Apex refers to the execution of Apex code where
the result is returned immediately to the user after the code is run. This
means the code runs in a single transaction and the user has to wait for the
processing to complete.
 Use Cases:
o When the result of the operation is needed immediately, such as
updating a record or calculating a value that needs to be displayed
immediately to the user.
o Performing real-time validations and operations during record
creation or updates.
 Characteristics:
o Immediate Execution: Code is executed and results are returned
immediately.
o Transaction Boundaries: Runs within a single transaction, meaning
all operations either succeed or fail together.
o Governor Limits: Subject to Salesforce's governor limits, which
include limits on CPU time, DML operations, and SOQL queries.

2. Writing Synchronous Apex Code


Key Concepts:
 Apex Methods: Write methods that execute immediately when called.
 DML Operations: Use DML (Data Manipulation Language) operations such
as insert, update, delete, and upsert to manipulate Salesforce records.
 SOQL Queries: Use Salesforce Object Query Language (SOQL) to retrieve
data synchronously.
 Error Handling: Implement proper error handling within the code to
manage exceptions and rollback operations if necessary.
Example Code:

public class AccountService {


public static void updateAccountIndustry(List<Account> accounts) {
for (Account acc : accounts) {
if (acc.AnnualRevenue > 1000000) {
acc.Industry = 'Large Enterprise';
} else {
acc.Industry = 'Small Business';
}
}
try {
update accounts; // DML operation to update records
} catch (DmlException e) {
System.debug('An error occurred: ' + e.getMessage());
// Handle exception, possibly rollback the transaction
}
}
}

Important Considerations:
 Governor Limits: Be mindful of limits on SOQL queries, DML operations,
and CPU time. Exceeding these limits will result in runtime exceptions.
 Bulkification: Ensure that code is bulkified to handle multiple records in a
single transaction.
 Transaction Control: Manage transaction boundaries carefully to ensure
data integrity.
3. Handling Transactions and Governor Limits
Transactions in Synchronous Apex:
 Single Transaction: All operations within a synchronous Apex method are
part of a single transaction.
 Atomicity: If any part of the transaction fails, the entire transaction is rolled
back.
 Commit: The transaction is committed only if all operations succeed without
errors.
Governor Limits:
 CPU Time: Limited to 10,000 milliseconds for synchronous operations.
 SOQL Queries: Limited to 100 SOQL queries per transaction.
 DML Statements: Limited to 150 DML statements per transaction.
 Heap Size: Limited to 6 MB for synchronous transactions.
Best Practices to Handle Limits:
 Bulkification: Write code that can handle multiple records efficiently to
avoid hitting limits.
 Efficient Queries: Optimize SOQL queries to return only the necessary data.
 Use Limits Class: Use the Limits class to monitor and debug governor
limits.

Integer queriesUsed = Limits.getQueries();


Integer queriesLimit = Limits.getLimitQueries();
System.debug('Queries used: ' + queriesUsed + ' out of ' + queriesLimit);
Example of Handling Governor Limits:

public class ContactService {


public static void processContacts(List<Contact> contacts) {
try {
List<Contact> toUpdate = new List<Contact>();
for (Contact con : contacts) {
if (con.LastName != null) {
con.Description = 'Processed';
toUpdate.add(con);
}
// Monitor SOQL query limit
if (Limits.getQueries() >= Limits.getLimitQueries() - 1) {
throw new LimitException('Query limit exceeded');
}
}
update toUpdate; // DML operation to update records
} catch (LimitException e) {
System.debug('Governor limit exceeded: ' + e.getMessage());
// Handle limit exception, possibly rollback the transaction
}
}
}

Thank You

You might also like