What is the difference in execution between . (dot) notation and getSObjects method? Omitting of course that I can parametrize with string getSObjects method.

for example:




Functionally, there is no difference. These two options return the same data. These functions are mostly the same.

From a performance perspective, the static approach is approximately 2.5x faster. I ran 25 trials each of 1,000 calls, and the static approach averaged 20.48 ms (20.48 µs per call), whereas the dynamic approach averaged 51.40 ms (51.40 µs per call).

Approach    Min CPU/iteration (µs)    Max CPU/iteration (µs)    Avg CPU/iteration (µs)
Static                       15.00                     32.00                     20.48
Dynamic                      40.00                     66.00                     51.40
  • 1
    Thanks for detailed test and answer! I did some tests myself but I was not sure. I got a similiar result. – patryk Mar 20 '17 at 17:29
  • 1
    @AdrianLarson - Not sure I have a better use case but the getSObjects is useful when you need to keep dependencies out of a managed package. otherwise I am hard pressed to find a reason to use it in this context vs the static approach. Any other use cases for using getSObjects – Eric Mar 20 '17 at 23:09
  • @Eric It seems like a useful method but I can't say I've found occasion to use it either. – Adrian Larson Mar 20 '17 at 23:27

I was bothered by null pointer exception when i was experimenting with my code. I changed static access method to getSObjects method and found a strange thing which could be important, when we use getSObjects method and related list is empty then returns null, opposite to static method which always returns a empty list. It happend to me when I was trying to access related list of object inside for loop

for(Account acc : accounts){
acc.getSObjects('AccountContactRoles'); // returns null
acc.AccountContactRoles;                // returns empty list
  • You are correct. getSObjects returns null if the subquery is empty. an important point to remember – Eric Mar 20 '17 at 23:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.