2

As a follow up to this question:

Add URL Parameter or another method to prevent action on browser refresh - Lightning Out / Component

The solution does not work in lightning. The basics of what I am trying to achieve is as follows:

  1. URL Button on Lightning Page - Prefer not to do quick actions as we have many extension packages and would have to duplicate them all over. a URL button can goto a single page in our base package

  2. On click of that button user is directed to the VF page in lightning and an action is performed

Issues

  1. If they refresh the browser while on that page the action is performed again.
  2. If the refresh the browser during the action (can take 5-10 seconds) the action is performed again. This means checking for result records will not work as a gate keeper)

So the question becomes:

  1. How to prevent the VF page from taking action if it is currently being displayed and the browser is refreshed? Is there a way to detect, and persist that detection, directly or indirectly a refresh
  2. I can use lightning components out in the solution if necessary
  3. Is there a way to detect refresh in lightning like there is in classic with window.performance which does not detect reload in lightning

I have tried using URL parameters but they are not maintained in lightning due to the iframe. And window .performance does not seems to work

1 Answer 1

2

To be honest, I don't think the solution will be on the client end.

Even if you come up with a bullet-proof way to handle browser refreshes, there's always the possibility that the users will have the same page open on different tabs or different machines, and they end up pushing the button twice within that 5-10 second range.

I feel like there needs to be a way to keep track of the state of whether or not the action is already being performed, and in Apex, that will involve the database.

If you maintain a table with an entry for each user, you could use the SOQL FOR UPDATE keyword to throw an error for any attempts to call the action while it is already underway.

@AuraEnabled
public static void performAction(){
    // check to see if the action is already running
    list<Lock__c> llist  = [
        SELECT Id,Name 
        FROM Lock__c 
        WHERE Name = :UserInfo.getUserId() 
        FOR UPDATE
    ];            

    // perform a long-running action
    for (Integer i = 0 ; i < 1000000 ; i++) Integer ii = 0;
}
2
  • 1
    I ended up going with a quick action and replicating it across packages. Going to see if I can consolidate into one global action in base package. Not worried about multiple tabs. Just a refresh in the current tabs context. Was trying to replace JS buttons that charged a credit card. A refresh of the page would have sent multiple charges. Seems quick action is only viable solution as refresh of lightning page destroys it
    – Eric
    Commented Jul 7, 2017 at 1:06
  • I will digest your answer in a bit though. Thanks for that. I agree we need a way to keep track of these things
    – Eric
    Commented Jul 7, 2017 at 1:07

You must log in to answer this question.

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