[SalesForce] Lightning RefreshView event not working on record detail page

I found that the RefreshView event doesn't always reload record data.
I'm writing a Lightning Component that updates a record, then tries to reload the record details to show the changes, but sometimes the view is not updated and shows old values.

This usually happens on the first record interaction (on the first or second button click), or after a reload (F5).

Any idea why?

For example:
two buttons that update the record Status with different values. Buttons are rendered based on the server status (and this works well), but the record details sometimes are obsolete. I also double-checked with server data, and I can confirm that client view is obsolete, despite the cache Session Setting.

If I click on the button Confirm (Status 2), the component updates Case.Status = "Status 2" and reloads itself. Then the button Reset (Status 1) appears, but the record detail page still shows the old value (Status 1).
See the screen below:

Example1

This is the code of the Component:

<aura:component controller="SampleButtonCmpCnt" access="global" implements="flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,force:hasSObjectName">
    <aura:attribute access="global" name="recordId" type="ID" />
    <aura:attribute access="private" name="status" type="String" />
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
    <aura:handler event="force:refreshView" action="{!c.doInit}"/>

    <aura:if isTrue="{!v.status == 'Status 2'}">
        <ui:button press="{!c.reset}">Reset (Status 1)</ui:button>
    </aura:if>

    <aura:if isTrue="{!v.status == 'Status 1'}">
        <ui:button press="{!c.confirm}">Confirm (Status 2)</ui:button>
    </aura:if>
</aura:component>

The controller:

({
    doInit : function(component, event, helper){
        helper.setCurrentStatus(component, event, helper);
    },
    confirm : function(component, event, helper) {
        helper.updateRecordHelper(component, event, helper, 'confirm');
    },
    reset : function(component, event, helper) {
        helper.updateRecordHelper(component, event, helper, 'reset');
    }

})

The helper:

({
    setCurrentStatus : function(component, event, helper){
        component.set("v.status", null);
        var action = component.get("c.queryRecordStatus");
        action.setParams({
            sfid : component.get("v.recordId"),
        });
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                var status = response.getReturnValue();
                component.set("v.status", status);
            } else {
                alert('ERROR');
            }
        });
        $A.enqueueAction(action);
    }, 
    updateRecordHelper : function(component, event, helper, actionName) {
        var action = component.get("c.updateRecord");
        action.setParams({
            sfid : component.get("v.recordId"),
            actionName : actionName
        });
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                var result = response.getReturnValue();
                if(!result){
                    alert('Error occurred');
                }else{
                    $A.get('e.force:refreshView').fire();
                    this.setCurrentStatus(component, event, helper);
                }
            }
        });
        $A.enqueueAction(action);
    }
})

While this is the server-side controller:

@AuraEnabled
public static String queryRecordStatus(String sfid){
    return [Select Status From Case Where Id =:sfid][0].Status;
} 

@AuraEnabled
public static Boolean updateRecord(String sfid, String actionName){
    try{
        String objectName = ((Id)sfId).getSObjectType().getDescribe().getName();
        SObject sObj = Schema.getGlobalDescribe().get(ObjectName).newSObject() ;
        sObj.put('Id', sfid);
        if(actionName == 'Confirm'){
            sObj.put('Status', 'Status 2');
        }else if (actionName == 'Reset'){
            sObj.put('Status', 'Status 1');
        }
        update sObj;
    }catch(Exception e){
        System.debug(e.getMessage());
        return false;
    }
    return true;
} 

Best Answer

So now that I understand correctly your problem, I have a better answer. This is a known issue.

You can subscribe to it to receive a notification once it's fixed.

Related Topic