Friday, March 14, 2025

Interview Questions on Salesforce Apex Part-1

 1) How do SOQL List and SOQL For Loop differ in terms of memory handling?

SOQL List loads all queried records into memory at once, which can quickly exceed the heap size limit if too many records are queried.

SOQL For Loop fetches records one at a time in smaller chunks, making it more efficient and reducing the risk of hitting the heap size limit.

2) What are the potential issues when querying large datasets using SOQL List?

Querying large datasets with SOQL List may result in heap size limit errors if the total size of the data exceeds 6 MB for synchronous operations. To avoid this, you should use SOQL For Loop, which processes records in smaller chunks.

3) How would querying parent-child data affect the use of SOQL For Loop?

When querying parent-child data in Salesforce, if there are more than 200 child records in a relationship, an error like "Aggregate query has too many rows for direct assignment" might occur. This can be avoided by using a nested loop structure.

Incorrect approach (will throw error if there are more than 200 child records):

for(Account obj : [SELECT id, (SELECT id, name FROM contacts) FROM account LIMIT 1]) {

    list<Contact> conList = obj.contacts;  // Error if more than 200 contacts

}

Correct approach (avoids error by iterating over child records inside the loop):

for(Account obj : [SELECT id, (SELECT id, name FROM contacts) FROM account LIMIT 1]) {

    for(Contact con : obj.contacts) {

        // Process each contact record

    }

}

4) Can you explain the heap size limit error with an example?

Let's say you're querying 50,000 records from the Contact object, and each record takes up 2 KB. The total memory required would be 50,000 * 2 KB = 100 MB. Since the maximum heap size limit for synchronous operations is 6 MB, this would exceed the limit, causing a heap size error. This is why SOQL For Loop should be used, as it processes each record individually.

5) Why are Governor Limits enforced in Apex?

Governor Limits are enforced in Apex to ensure that code runs efficiently in Salesforce's multitenant environment. They prevent one tenant from consuming too many resources, which could negatively impact others sharing the same instance.

7) What is the maximum number of records that can be returned by a SOQL query in a synchronous transaction?

A synchronous SOQL query can return a maximum of 50,000 records. If this limit is exceeded, a runtime exception will occur.

8) How many DML statements can be executed in a single transaction?

You can execute up to 150 DML statements in a single transaction, whether it's a synchronous or asynchronous operation.

9) What is the maximum heap size for synchronous operations in Apex?

The maximum heap size for synchronous operations in Apex is 6 MB. If the heap size exceeds this limit, an exception will occur.

10) How many future methods can be invoked in a single Apex transaction?

A maximum of 50 future methods can be invoked per Apex transaction, but for batch/future operations, only 50 queueable jobs can be invoked.

No comments:

Post a Comment