0

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;
    }


7
  • Can you add the Apex code that is performing the query Oct 24, 2019 at 15:22
  • @BryanAnderson Added the code Oct 24, 2019 at 15:25
  • The cacheable annotation could be causing a delay between the client side and server side determining whether to query the database again. What happens if you remove the cacheable annotation. Are you doing setStorable() in your Aura component? Oct 24, 2019 at 15:28
  • If I remove cacheable, I get below error "Apex methods that are to be cached must be marked as @AuraEnabled(cacheable=true)". I did not use setStorable() in the lightning component Oct 24, 2019 at 15:30
  • 1
    @BryanAnderson I got the issue resolved. Somehow got the clue from salesforce.stackexchange.com/questions/212552/… . When I rename the controller method not to start with "get" and remove Cacheable, then I am getting the response swiftly. If the controller method prefixed as "get" then I need to add "cacheable = true". Otherwise I get error response in lightning component. Oct 24, 2019 at 15:38

1 Answer 1

1

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

You must log in to answer this question.

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