Development questions
How do you directly populate a Map<Id, SObject> from a SOQL query without using a for loop? (new Map<Id, Account>([SELECT Id, Name FROM Account])).
Pass the query directly into the Map constructor. Salesforce uses each record's Id as the key.
apex
Map<Id, Account> accMap = new Map<Id, Account>([SELECT Id, Name FROM Account WHERE Industry = 'Tech']);
Account a = accMap.get(someId);
This avoids manual loops, is concise, and counts as one SOQL query. It works only for keys that are Ids; for other keys use a loop.
soqlapexcollections
More Development questions
- What is an Apex Trigger in Salesforce and when should you use it over Flows?
- What is the difference between before triggers and after triggers?
- Explain all Trigger context variables (isInsert, isUpdate, isDelete, isUndelete, isBefore, isAfter, isExecuting, size, operationType).
- What is the difference between Trigger.new and Trigger.old?
- In which trigger events are Trigger.new and Trigger.newMap available?
- In which trigger events are Trigger.old and Trigger.oldMap available?