Does a session object exist in SalesForce ?

I mean an object that I can initialize when user logs in and would be available in all pages until he logs out.

I want to avoid several database requests if I can do only one when he logs in an then use this object.

Something like a SessionBean in Java for example.

2 Answers 2


Salesforce does not have a server-side session object: anything you want to preserve on the server has to be put in the database. The cost of such calls relative to the overall request/response cycle is not as prohibitive as you might expect.

The lack of state at the server-side means only active requests occupy server-side memory and that requests can be routed to whichever processor has capacity without the server-side state having to be moved about. So the approach is key to allowing Salesforce to operate at its 2 billion transactions a day scale.

Also take a look at the Custom Settings feature; custom settings are cached in memory by the platform.

  • Ok, thank you. So you think I can make a database request every time I need it without problems ?
    – SF_user
    Sep 4, 2014 at 7:55
  • No, You can use custom settings as a workaround. Sep 4, 2014 at 8:01
  • Yes, but I need to access to the database to read/write the values, so it's the same no ?
    – SF_user
    Sep 4, 2014 at 8:30
  • @SF_user Custom settings are cached by Salesforce so on the read they can be quick to access. But yes to change them you are doing a database write (and also kicking off a flush of the caches which adds more cost).
    – Keith C
    Sep 4, 2014 at 9:31

If you are expecting sesstion as global variable like PHP that you can set on page1 and use it any on any other page till user log out then there is no session variable available of that nature.

There is one workaround available that is custom setting which allows you to avoid DB QUERY


Here is how you can Read value from custom setting
List custom setting

List<Games__C> mcs = Games__c.getall().values();
boolean textField = null;
if (mcs[0].GameType__c == 'PC') {
  textField = true;
system.assertEquals(textField, true);

OR hierarchy custom setting

Foundation_Countries__c myCS1 = Foundation_Countries__c.getValues('United States');
String myCCVal = myCS1.Country_code__c;
Foundation_Countries__c myCS2 = Foundation_Countries__c.getInstance('United States');
String myCCInst = myCS2.Country_code__c;
system.assertEquals(myCCinst, myCCVal);

Here is how you can write value to custom setting
Hierarchy Setting

//fetches or creates a Custom Setting at the organization level 
HierarchySetting__c setting = HierarchySetting__c.getOrgDefaults();
if (setting == null) setting = new HierarchySetting__c();

setting.CustomField__c = 'https://salesforce.stackexchange.com/';
upsert setting;

Or List Setting

upsert new ListSetting__c(
    Name = 'sandbox',
    CustomField__c = 'https://test.salesforce.com/'

For more details take look at:

Populating Custom Settings field using Apex


  • Thank you for your help. I think I will keep the database requests to get datas.
    – SF_user
    Sep 4, 2014 at 9:35

You must log in to answer this question.

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