1

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.

7
  • 1
    is there any future / queuable method in lead trigger? Dec 24, 2018 at 9:26
  • 1
    Also can you remove the else part in updateLeadStatus method and see if it works? Dec 24, 2018 at 9:27
  • 3
    I don't see any issues in the provided code. Even 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.
    – Saroj Bera
    Dec 24, 2018 at 10:20
  • Thanks @SarojBera - I also didn't see issues in code, but using your method, fixed the issue. please add as answer so I can accept.
    – Saariko
    Dec 24, 2018 at 12:33
  • 1
    @Saariko I know there's a way to cause the page to refresh once the DML has completed in Lightning. I don't recall what it is at the moment, but will post it once I recall or find the link with the info that was shared with me a few months ago.
    – crmprogdev
    Dec 25, 2018 at 15:55

2 Answers 2

2

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

I think there may be a couple of reasons why the update isn't showing up...

  1. To call a helper method from the ComponentNameHelper.js file you use this.functionName() instead of helper.functionName() (which is for calling helper functions from ComponentNameController.js). helper.openRecord(leadId); is not being called and is failing silently (as with a lot of salesforce lightning errors).
  2. When you use setTimeout you would need to use $A.getCallback, since you are...

    Modifying Components Outside the Framework Lifecycle

    Please see this link for further info.

As you mentioned @Saroj Bera's solution should work fine, since a delay is not why it wasn't working.

You must log in to answer this question.

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