[SalesForce] Custom related list Lightning component for History table related list – delay in fetching

I created a Custom related list (Component A) that fetches history table and displays. It has a button to refresh the history list.
In the same page, I have another lightning component (Component B) which calls Custom Apex controller to update the record. I do the following steps

  1. I update the record with Component B.
  2. I click refresh button on Component A. But the response does not pick up the latest changes from history table.I have wait for 3-4 seconds before the latest history records are fetched. At the same time, I can see the history table is already updated using SOQL.

This happens only with history tables. Has anyone observed this behaviour ? Is there a way to resolve this issue ?

Another question – Is "@AuraEnabled(cacheable = true)" mandatory for Apex Controller fetch methods called from Lightning Components ? If I remove "Cacheable=true" I get the below error – "Apex methods that are to be cached must be marked as @AuraEnabled(cacheable=true)"

Apex Controller 

@AuraEnabled(cacheable = true)
    public static Map<String,Object> getCMEngagementHistories(Id recordId){
        Map<String,Object> response = CM_EngagementService.getCMEngagementHistories(recordId);
        System.debug(response);
        return response;
    }

Apex Service

public static Map<String,Object> getCMEngagementHistories(Id recordId) {
        List<CM_Onboarding__History> onboardingHistories = [
                SELECT
                        CreatedBy.Name,
                        CreatedDate,
                        Field,
                        Id,
                        NewValue,
                        OldValue,
                        ParentId
                FROM CM_Onboarding__History
                where ParentId = :recordId
                order by createdDate desc
                limit 10
        ];
        List<CMHistoryDTO> newhistoryList = new List<CMHistoryDTO>();
        for(CM_Onboarding__History h:onboardingHistories){
            CMHistoryDTO hh = new CMHistoryDTO();
            hh.CreatedBy = h.CreatedBy.Name;
            hh.CreatedDate = h.CreatedDate;
            hh.Field = h.Field;
            hh.NewValue = h.NewValue;
            hh.OldValue = h.OldValue;

            if(hh.Field != null && Schema.getGlobalDescribe().get('CM_Onboarding__c').getDescribe().fields.getMap().get(hh.Field) != null){
                hh.Field = Schema.getGlobalDescribe().get('CM_Onboarding__c').getDescribe().fields.getMap().get(hh.Field).getDescribe().getLabel();

            }
            if (newhistoryList.size() < 5){
                if(h.OldValue == null || (h.OldValue != null && !(String.valueof(h.OldValue).startsWith('00')))){
                    newhistoryList.add(hh);
                }
            }
        }

        Map<String,Object> localResponse = new Map<String,Object>();
        localResponse.put(cmOnboardingHistoriesKey, newhistoryList);
        return localResponse;
    }


Best Answer

I believe once an apex method is set as cacheable=true, salesforce at runtime always expects the cacheable as true. If I remove cacheable, at runtime I get the above error in lightning component. When I rename the method and do not add cacheable then it works fine. I have observed this behaviour couple of times now. So resolution would be simply to rename the method and remove cacheable

Related Topic