0

I am basically from java backgroud and new to salesforce. Need suggestion on following query:

Suppose we have two custom objects - A and B . A has lookup field to B .

We have three objects data for A - A1, A2, A3. Following is the lookup filed mapping:

  • A1's lookup field maps to B1
  • A2's lookup field maps to B2
  • A3's lookup field maps to B1

Now using below query, I get the object A1 and it related data B1

[SELECT prop1,prop2 (SELECT propx,propy FROM B_rel__r ) FROM A__c where name=:name]

Question : Does salesforce caches the object it load from SOQL ? If next time I use SOQL to retrieve object A3 (listed above) and its related object B1 (which is same as retrieved from previous query), then salesforce would provide the same object B1 from cache or would it freshly retrieve the object B1 from their database. I am asking this from performance point of view. Do we need to handle the caching in the above case ?

1

As Andrei.Z writes, there isn't any automatic caching in Apex of query results, but there may be some (transactionally correct) caching in layers closer to the database. And perhaps there is caching in some layer explicitly for UI lookups but that is not something you have control of.

You should firstly write your code so that it is clean. If adding caching makes the code less clean don't do it unless you have a clear reason. And because of governor limits in Salesforce you should also consider bulkification. The latter could be considered a type of caching, because typically you query the data for a set of records once and then lookup the values from usually an in-memory map.

Execution context also matters. For example, caching a lot of data in a controller can have a negative effect if the data is included in the view state: just holding Ids in the view state and requerying can be a better way to go.

2

Salesforce will actually perform a real SOQL every time you will do a SOQL ( a fresh retreive from database). Selected Sobjects are not cached, at least not in a way that SOQLs are not performed.

You can try following: A - child object B - parent object

  1. Create a formula field X on child A that points to any field from parent B.
  2. Create and select instances of parent B1 and child A1. Field X on parent = 1 and formula field selected on child also = 1.
  3. We update B with field X = 2.
  4. A1 still says Formula field = 1.
  5. Re select A1 into variable A2. (A1, as itself, was never updated). Formula field on A2 will show X=2, on A1 it is still X=1.

So, in your example, it is even possible to have 2 different instances of related object B1, for example if you do a DML between selecting them.

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.