10

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:

Account.AccountContactRoles 

and

Account.getSObjects('AccountContactRoles');
0

3 Answers 3

17

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
3
  • 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, 2017 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, 2017 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, 2017 at 23:27
6

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
}
1
  • You are correct. getSObjects returns null if the subquery is empty. an important point to remember
    – Eric
    Mar 20, 2017 at 23:02
5

I tried to retest the performance of the 3 approaches (added getSObjects(SObjectField) approach).

Org Params:

ApiVersion: 51.0
InstanceName: CS100
IsSandbox: true
OrganizationType: Developer Edition

Results for 50 trials each of 20.000 calls:

Approach MIN CPU (ms) MAX CPU (ms) AVG CPU (ms) STD (σ)
acc.Contacts 10.00 16.00 12.63 1.83
acc.getSObjects('Contacts') 16.00 24.00 16.93 2.19
acc.getSObjects(Contact.AccountId) 30.00 57.00 38.70 6.10

enter image description here

The static approach is still expectedly the fastest and approximately 1.35x faster than dynamic. SObject.getSObjects(SObjectType) approach not only the slowest one, but also the most "unstable" having the greatest value of STD.

The result of benchmarking always depends on many factors, so in your case the results may differ. In this case, I was interested in comparing relative results.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .