Salesforce Platform Developer I (legacy) — Question 96
A method is passed a list of generic sObjects as a parameter.
What should the developer do to determine which object type (Account, Lead, or Contact, for example) to cast each sObject?
Answer options
- A. Use the first three characters of the sObject ID to determine the sObject type.
- B. Use the getSObjectType method on each generic sObject to retrieve the sObject token.
- C. Use the getSObjectName method on the sObject class to get the sObject name.
- D. Use a try-catch construct to cast the sObject into one of the three sObject types.
Correct answer: B
Explanation
The correct answer is B because the getSObjectType method provides a direct way to retrieve the sObject token, which identifies the specific type of sObject. Option A is incorrect as the first three characters of the sObject ID do not reliably indicate the object type. Option C is also not correct since getSObjectName does not provide the token required for casting. Option D is not a good practice as it relies on exceptions rather than proper type identification.