4

I am using the Platform Cache to store the Id of the customer the salesperson is speaking with on the phone, such that different Lightning Components automatically load the customer's details as the salesperson navigates around Salesforce Lightning.

Example Apex code

public class CallerSession {

private static final String PARTITION_NAME = 'local.xxxx';
private static final String KEY_CALLER = 'CallerId';

public static Id getCallerId(){

    if (getPartition().contains(KEY_CALLER)){
        return (Id)getPartition().get(KEY_CALLER);    
    }
    return null;
}

public static void setCallerId(Id callerId){
    if (callerId != null){
        getPartition().put(KEY_CALLER, callerId);    
    }
}

public static void clearCache(){
    getPartition().remove(KEY_CALLER);
}

private static Cache.SessionPartition getPartition(){
    return Cache.Session.getPartition(PARTITION_NAME);
}

public static String getAsJson() {

    Map<String, String> ids = new Map<String, String>();
    ids.put('callerId', getCallerId());
    return JSON.serialize(ids, true);
}
}

Apex Controller - getting from cache

@AuraEnabled
public static String getTriageSessionInfo(){
    return CallerSession.getAsJson();
}

Apex Controller - storing into Cache

@AuraEnabled
public static Account getCaller(String accountId){

    if (accountId == null){
        return null;
    }

    CallerSession.setCallerId(accountId);

    return getAccount(accountId);
}

public static Account getAccount(String accountId){

    return [SELECT Id, 
            FirstName, 
            LastName, 
            PersonBirthdate, 
            PersonEmail, 
            PersonHomePhone, 
            PersonMobilePhone, 
            PersonMailingStreet, 
            PersonMailingCity, 
            PersonMailingState, 
            PersonMailingPostalCode, 
            PersonMailingLatitude, 
            PersonMailingLongitude
            FROM Account 
            WHERE IsPersonAccount = true 
            AND Id =:accountId];
}

One Lightning Component stores into the Cache, all other components get from the cache.

I recently set up a Sales Console app, but the Lightning Component do not Save\Load the Id from the Platform Cache

I cannot find any documentation explaining this behaviour.

Is Platform Cache compatible with Sales Console?

6
  • It should be , Can you provide relevant code? – Pranay Jaiswal Dec 14 '18 at 13:23
  • @PranayJaiswal code added – Robs Dec 14 '18 at 13:26
  • 1
    how are you putting Id in cache? Is it the same class that Aura Apex controller is? I can see from documentation The cache put calls are not allowed in a partition that the invoking class doesn’t own. – Pranay Jaiswal Dec 14 '18 at 13:35
  • @PranayJaiswal updated with more code and further explanation – Robs Dec 14 '18 at 13:55
  • @PranayJaiswal what does the cache put calls are not allowed in a partition that the invoking class doesn’t own actually mean? – Robs Dec 14 '18 at 15:26
0

I suspect this may be related to using the Session Cache rather than the Org Cache. The session cache is tied to the individual user sessions.

When a user is signed in the actually have a few different active sessions that depend on how they are using Salesforce. See User Session Types and Different Session Ids in Different contexts.


Another possibility is that the data isn't being persisted in the cache until it is requested. This might happen if there is no storage space allocated to the partition or if additional data being added to the partition caused it to be dropped.

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.