function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Walter@AdicioWalter@Adicio 

can i run an action when the user closes the browser or tab?

for example if i set some values to a record when you enter a visualforce page. can i work on these values if a user just closes the browser or the tab.

 

i have a visualforce page used to edit a record and when a user is editing the record i save some values to the record so no other users can use the visualforce page to edit the same record at the same time.  to " lock " the record for editing. there is a button users have to click to " unlock " the record before they close the page. can i automate the " unlock " button action if they close the browser or the tab?

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

You can use the onunload javascript event for this.  However, this will also fire when the user navigates to another page as well as when the window is closed.

 

There's more information here: http://www.w3schools.com/jsref/event_onunload.asp

All Answers

bob_buzzardbob_buzzard

You can use the onunload javascript event for this.  However, this will also fire when the user navigates to another page as well as when the window is closed.

 

There's more information here: http://www.w3schools.com/jsref/event_onunload.asp

This was selected as the best answer
Walter@AdicioWalter@Adicio

thank you 

Darryl SinghDarryl Singh

Easy solution works in IE and Chrome.  Added Locked_By__c User lookup field to Quote object, then:

 

VF Markup:

 

<apex:page standardController="Quote" extensions="SOS_Maintenance_Controller" action="{!lockQuote}">
     <script type="text/javascript" >
          window.onbeforeunload = unloadPage;
          function unloadPage(){cancel();}
     </script>
     <apex:form >
     <apex:actionFunction name="cancel" action="{!cancel}" />

 

etc.

 

And the pertinent Apex methods:

 

// Lock the quote . . .
public void lockQuote() {
     isLocked = false;
     Quote qtLock = [select Id, Locked_By__c from Quote where Id = :ApexPages.CurrentPage().getParameters().get('id')];
     if(qtLock.Locked_By__c == null) {
          qtLock.Locked_By__c = UserInfo.getUserId();
          database.update(qtLock);
          lockUser = null;
     } else {lockUser = [select Id, Name from User where Id = :qtLock.Locked_By__c].name; isLocked = true;}
}

 

// Unlock the quote . . .
public void unLockQuote() {
     Quote qtLock = [select Id, Locked_By__c from Quote where Id = :ApexPages.CurrentPage().getParameters().get('id')];
     if(qtLock.Locked_By__c == UserInfo.getUserId() & !isLocked) {qtLock.Locked_By__c = null; database.update(qtLock);}
}

 


// Back to quote detail page on Cancel.
public PageReference cancel() {
     unLockQuote();
     PageReference qtPage = new ApexPages.StandardController(Qt).view();
     return qtPage;
}