+ Start a Discussion
Debasish Paul 11Debasish Paul 11 

Can anybody please explain the query caching in salesforce with example?

Please explain query caching with example.
Tintu_BabuTintu_Babu
Hi,
You can use a static variable in a helper class to cache the result while running a single transaction.
public class UserProfileHelper {
private static Profile userProfile;
            public static Profile getProfile() {
                       if (userProfile == null)
                             userProfile = [select Id, Name from Profile where Id = :userInfo.getProfileId()];
                             return userProfile;
              }
}
Reference : https://salesforce.stackexchange.com/questions/71621/caching-soql-query-results
 
Shruti khodaboleShruti khodabole
Hi,
1) You can use the global variable "Useinfo.getProfileId()" if you only need profile id.
2) You can use static variable to cache profile you query. see code:
 
public static Profile CachingProfile;
 public constructor method(){ 
CachingProfile = [Select id, name From Profile Where id=:userInfo.getProfileId()]; 
}

Thanks,
Shruti Khodabole
Salesforce Developer
http://www.zen4orce.com