0% found this document useful (0 votes)
12 views

Salesforce Apex Coding Scenario-based Interview Questions

The document provides a series of scenario-based Apex coding interview questions and answers, covering topics such as using @testSetup, validating Opportunity amounts, handling DML limits, and scheduling jobs. It also includes common mistakes to avoid and best practices for writing Batch Apex. Additionally, it promotes interview preparation services offered by Prominent Academy for Salesforce roles.

Uploaded by

pamahiw740
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)
12 views

Salesforce Apex Coding Scenario-based Interview Questions

The document provides a series of scenario-based Apex coding interview questions and answers, covering topics such as using @testSetup, validating Opportunity amounts, handling DML limits, and scheduling jobs. It also includes common mistakes to avoid and best practices for writing Batch Apex. Additionally, it promotes interview preparation services offered by Prominent Academy for Salesforce roles.

Uploaded by

pamahiw740
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/ 7

Apex Coding

scenario-based
interview
questions
www.prominentacademy.in

+91 98604 38743


Scenario

How do you use @testSetup and when?

Answer:
@testSetup is used to insert common test data used across
multiple test methods to avoid code duplication.
apex

@testSetup
static void setupData() {
Account acc = new Account(Name='Test');
insert acc;
}

Scenario
How do you validate that an Opportunity’s amount is
greater than its related Account’s AnnualRevenue?
Logic:
Use before insert or before update trigger on Opportunity
and query related Account.
apex

trigger ValidateOppAmount on Opportunity (before insert, before update) {


Set<Id> accountIds = new Set<Id>();
for (Opportunity opp : Trigger.new) {
if (opp.AccountId != null) accountIds.add(opp.AccountId);
}

Map<Id, Account> accounts = new Map<Id, Account>(


[SELECT Id, AnnualRevenue FROM Account WHERE Id IN :accountIds]
);

for (Opportunity opp : Trigger.new) {


if (opp.AccountId != null && opp.Amount != null &&
opp.Amount > accounts.get(opp.AccountId).AnnualRevenue) {
opp.addError('Opportunity amount cannot exceed Account annual revenue.');
}
}
}

Your next opportunity is closer than you think. Let’s get you there!
📞 Don’t wait—call us at +91 98604 38743 today
Scenario

What happens if a trigger executes more than 150 DML


operations?

Logic:
Salesforce enforces a limit of 150 DML statements per
transaction. Exceeding this causes runtime errors.

Solution:
Bulkify your logic by collecting records into a list and
performing a single update or insert.

Common Mistake:
❌ Performing DML inside a loop.

Scenario
How to handle NULL in SOQL WHERE clause?
apex

List<Account> accs = [SELECT Id FROM Account WHERE Industry = null];

Common Mistake:
❌ Using = NULL instead of = null in Apex context or using
== directly in SOQL string.

Your next opportunity is closer than you think. Let’s get you there!
📞 Don’t wait—call us at +91 98604 38743 today
Scenario
How do you create a roll-up summary for objects with a
Lookup relationship?

Logic:
Use Apex Trigger to calculate totals and update parent.
apex

trigger RollupContacts on Contact (after insert, after delete, after


update) {
Set<Id> accIds = new Set<Id>();
if(Trigger.isInsert || Trigger.isUpdate){
for(Contact c : Trigger.new){
if(c.AccountId != null) accIds.add(c.AccountId);
}
}
if(Trigger.isDelete){
for(Contact c : Trigger.old){
if(c.AccountId != null) accIds.add(c.AccountId);
}
}

Map<Id, Integer> contactCountMap = new Map<Id, Integer>();


for(AggregateResult ar : [SELECT AccountId accId, COUNT(Id)
contactCount FROM Contact WHERE AccountId IN :accIds GROUP BY
AccountId]){
contactCountMap.put((Id)ar.get('accId'),
(Integer)ar.get('contactCount'));
}

List<Account> accToUpdate = new List<Account>();


for(Id accId : contactCountMap.keySet()){
accToUpdate.add(new Account(Id = accId,
Number_of_Contacts__c = contactCountMap.get(accId)));
}
update accToUpdate;
}

Your next opportunity is closer than you think. Let’s get you there!
📞 Don’t wait—call us at +91 98604 38743 today
Scenario
How do you schedule a class to run every Monday at 9 AM?

Logic:
Use System.schedule() with a CRON expression.

apex

public class MyScheduledJob implements Schedulable {


public void execute(SchedulableContext sc){
// logic
}
}

// Schedule
String cronExp = '0 0 9 ? * MON'; // every Monday 9 AM
System.schedule('Weekly Job', cronExp, new MyScheduledJob());

Scenario

How do you build and run SOQL queries dynamically?

Logic:
Use Database.query(String) to execute SOQL from a string.

apex

String query = 'SELECT Id, Name FROM Account WHERE Name LIKE
\'%Test%\'';
List<Account> accList = Database.query(query);

Common Mistake:
❌ Improper escaping of quotes, leading to malformed SOQL.

Your next opportunity is closer than you think. Let’s get you there!
📞 Don’t wait—call us at +91 98604 38743 today
Scenario
How do you use aggregate functions like GROUP BY in Apex
SOQL?

Logic:
Use AggregateResult class to capture the results.

apex

List<AggregateResult> groupedResults = [
SELECT AccountId, COUNT(Id) total FROM Contact GROUP BY
AccountId
];
for (AggregateResult ar : groupedResults) {
System.debug('Account: ' + ar.get('AccountId') + ', Total: ' +
ar.get('total'));
}

Common Mistake:
❌ Trying to cast AggregateResult directly into custom objects
or failing to use .get() properly.

Scenario
What are key best practices while writing Batch Apex?
Logic:
Use smaller batch sizes if large data sets.
Avoid SOQL/DML in loops.
Implement all 3 methods (start, execute, finish).
Test thoroughly with Test.startTest() and Test.stopTest().
Common Mistake:
❌ Not testing batch properly or doing DML inside the start()
method.

Your next opportunity is closer than you think. Let’s get you there!
📞 Don’t wait—call us at +91 98604 38743 today
Think your skills are enough?
Think again—these salesforce
questions could cost you your
Salesforce job.
Looking to crack your Salesforce interviews and land
your dream job? 💼 We've got you covered! At
Prominent Academy, we specialize in providing end-to-
end interview preparation that ensures you're not just
ready—but confident! 💪
💡 What We Cover:
✅ Mock Interviews tailored to Salesforce roles
✅ Real-world scenario-based questions for Admin,
Developer, CPQ, and Architect tracks
✅ Guidance on resume building and LinkedIn
optimization
✅ In-depth coverage of Salesforce core concepts,
integrations, and projects
✅ Latest Salesforce certification tips and tricks
✅ Unlimited interview calls with top companies
🎯 Whether you're a fresher or an experienced
professional transitioning to Salesforce, we provide
personalized guidance to help you shine in interviews and
stand out in the competitive market

📞call us at +91 98604 38743 to learn more.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy