[SalesForce] delay a refresh of a page using lightning component

I have a lightning component, activated with a quick action that updates an object (lead) status. Then it refreshes the record page.

The issue I am having is that the refresh does NOT show the new status, but the previous one, if I wait a couple of seconds, and manually refreshes the page again, the change is displayed.

  1. is there a way to 'Delay' the refresh of the page by a certain time?
  2. Maybe a different approach?

Here is my code:

LeadSendToConvert.cmp

<aura:component controller="LeadSendToConvert_LC" implements="force:lightningQuickAction,force:hasRecordId" access="global">
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    Updating lead status to Convert by Partner ... </aura:component>

LeadSendToConvertController.js

({
    doInit : function(component, event, helper) {
        helper.updateLeadStatus(component, event, helper);
    }
})

LeadSendToConvertHelper.js

({
    updateLeadStatus : function(component, event, helper) {
        var action = component.get("c.updateLeadStatusSendToConvert");
        var leadId = component.get("v.recordId");
        action.setParams({
            leadRecordId : leadId
        });

        action.setCallback(this, function(response) {    
            var state = response.getState();

            if(state == "SUCCESS" && component.isValid()) {
                var leadRecordId = response.getReturnValue();
                helper.openRecord(leadId);
            }
            else
                helper.openRecord(leadId);
        });

        $A.enqueueAction(action);
    },

    openRecord : function(leadRecordId) {
        var urlEvent = $A.get("e.force:navigateToURL");
        if(urlEvent) {
            urlEvent.setParams({
                "url": "/" + leadRecordId
            });
            urlEvent.fire();
        } else {
            alert('Outside of lightning scope.');
        }

        setTimeout(function() {
            $A.get('e.force:refreshView').fire();
            $A.get("e.force:closeQuickAction").fire();
        }, 500);
    } 
})

LeadSendToConvert_LC.cls

public class LeadSendToConvert_LC {
    @AuraEnabled

    public static String updateLeadStatusSendToConvert(String leadRecordId) {
        String updateLeadId = null;
        try {
            lead iLead = new lead();
            iLead.id = leadRecordId;
            iLead.Status = 'Partner Only - Waiting to Convert';
            update iLead;
        } catch(Exception e) {
            throw new AuraHandledException('Cannot update lead status: ' + updateLeadId + ' ::: error: ' +  e.getMessage());
        }

        return updateLeadId;
    }
}

Answer to @pranay – Currently there are no future/queuable actions in Lead Trigger.

I removed the Else (also tweaked code a bit) – Same result. field is updated in the background, but when record page is refreshed, the change is not displaeyd. only after a second manual refresh.

Best Answer

You don't need a separate openRecord method to refresh the page. You can fire both 'e.force:closeQuickAction' & 'e.force:refreshView' from the callback method to get the refreshed view of the updated record.


Added by @saariko

Thank you, here is my solution with your suggestion:

({
    updateLeadStatus : function(component, event, helper) {
        var action = component.get("c.updateLeadStatusSendToConvert");
        var leadId = component.get("v.recordId");
        action.setParams({
            leadRecordId : leadId
        });

        action.setCallback(this, function(response) {    
            var state = response.getState();
            if(state == "SUCCESS" && component.isValid()) {

                $A.get("e.force:refreshView").fire();
                $A.get("e.force:closeQuickAction").fire();
            }
        });

        $A.enqueueAction(action);
    } 
})
Related Topic