Common Scenario-Based Questions
1. What are Entitlements and Milestones?
Answer :
Entitlements define customers' support rights (e.g., how many cases they can submit, response time SLAs). Milestones are required steps within an entitlement process with time targets (e.g., 'First Response within 2 hours'). Entitlement processes apply to cases and work orders.
2. Account has N contacts. Balance field = Saving / No. of Contacts. How to populate?
Answer :
Use Batch Apex since N is unconfirmed (could be millions). Process accounts in chunks, calculate for each batch.
global class BalanceBatch implements Database.Batchable<sObject> {
global Database.QueryLocator start(Database.BatchableContext bc) {
return Database.getQueryLocator('SELECT Id, Saving__c, (SELECT Id FROM Contacts) FROM Account');
}
global void execute(Database.BatchableContext bc, List<Account> accounts) {
Set<Id> accIds = new Map<Id, Account>(accounts).keySet();
Map<Id, Integer> countMap = new Map<Id, Integer>();
for(AggregateResult ar : [SELECT AccountId, COUNT(Id) cnt FROM Contact WHERE AccountId IN :accIds GROUP BY AccountId]) {
countMap.put((Id)ar.get('AccountId'), (Integer)ar.get('cnt'));
}
List<Contact> toUpdate = new List<Contact>();
for(Account acc : accounts) {
Integer cnt = countMap.containsKey(acc.Id) ? countMap.get(acc.Id) : 0;
if(cnt > 0) {
for(Contact c : [SELECT Id FROM Contact WHERE AccountId = :acc.Id]) {
toUpdate.add(new Contact(Id = c.Id, Balance__c = acc.Saving__c / cnt));
}
}
}
update toUpdate;
}
global void finish(Database.BatchableContext bc) {}
}
3. Map<String, Integer> — count accounts by Industry without SOQL aggregation.
Answer :
public static Map<String, Integer> countAccountsByIndustry(List<Account> accounts) {
Map<String, Integer> result = new Map<String, Integer>();
for(Account acc : accounts) {
String ind = acc.Industry != null ? acc.Industry : 'Unknown';
result.put(ind, (result.containsKey(ind) ? result.get(ind) : 0) + 1);
}
return result;
}
4. Map<String, List<String>> — Account Name to list of Contact Names.
Answer :
public static Map<String, List<String>> getAccountContacts(List<String> accountNames) {
Map<String, List<String>> result = new Map<String, List<String>>();
for(Account acc : [SELECT Name, (SELECT FirstName, LastName FROM Contacts) FROM Account WHERE Name IN :accountNames]) {
List<String> names = new List<String>();
for(Contact c : acc.Contacts) names.add(c.FirstName + ' ' + c.LastName);
result.put(acc.Name, names);
}
return result;
}
5. Account rating based on number of Opportunities.
Answer :
trigger AccountRatingTrigger on Opportunity(after insert, after update, after delete, after undelete) {
Set<Id> accIds = new Set<Id>();
for(Opportunity o : Trigger.isDelete ? Trigger.old : Trigger.new) if(o.AccountId != null) accIds.add(o.AccountId);
Map<Id, Integer> countMap = new Map<Id, Integer>();
for(AggregateResult ar : [SELECT AccountId, COUNT(Id) cnt FROM Opportunity WHERE AccountId IN :accIds GROUP BY AccountId]) {
countMap.put((Id)ar.get('AccountId'), (Integer)ar.get('cnt'));
}
List<Account> toUpdate = new List<Account>();
for(Id id : accIds) {
Integer cnt = countMap.containsKey(id) ? countMap.get(id) : 0;
String rating = cnt < 3 ? 'Cold' : cnt <= 5 ? 'Warm' : 'Hot';
toUpdate.add(new Account(Id = id, Rating = rating));
}
update toUpdate;
}
6. When Contact is created, check if Account exists with name 'ContactLastName-Account'. If yes, tag contact. Else create Account and tag.
Answer :
trigger ContactTrigger on Contact(before insert) {
Set<String> accNames = new Set<String>();
for(Contact c : Trigger.new) if(c.LastName != null) accNames.add(c.LastName + '-Account');
Map<String, Account> existingAccs = new Map<String, Account>();
for(Account a : [SELECT Id, Name FROM Account WHERE Name IN :accNames]) existingAccs.put(a.Name, a);
List<Account> toInsert = new List<Account>();
for(Contact c : Trigger.new) {
String accName = c.LastName + '-Account';
if(existingAccs.containsKey(accName)) { c.AccountId = existingAccs.get(accName).Id; }
else { Account newAcc = new Account(Name = accName); toInsert.add(newAcc); existingAccs.put(accName, newAcc); }
}
insert toInsert;
for(Contact c : Trigger.new) if(c.AccountId == null) c.AccountId = existingAccs.get(c.LastName + '-Account').Id;
}
💬 Community Chatter
Loading...🔐
Join the Community Discussion
Login to ask questions, share answers, and connect with other Salesforce professionals.