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
Tom SineTom Sine 

Prevent Custom Button from being used more than once

This question may be very basic, but I am not a developer so my experience is very limited.  I created a custom button to populate a date field in my object.  Now I want to add to the code to prevent someone from using the button again.  Below is the code for the button.  What do I need to add to prevent the button from being used again?

- Thanks TS

{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")} 
 
var newRecords = [];
 
var c = new sforce.SObject("Custom_Quote_Request__c"); 
c.id ="{!Custom_Quote_Request__c.Id}";
c.Submit_Quote_Request_Date__c = new Date();
newRecords.push(c); 
 
result = sforce.connection.update(newRecords); 
 
window.location.reload();
Rajiv Bhatt 16Rajiv Bhatt 16
how about creating another custom checkbox field and marking it as checked after you have set the date. also, check this checkbox before you update the date, that way the button would execute the code only once. Does that help?
Tom SineTom Sine
I take your word that this would prevent the date from populating again, but I am not clear how the code would look.  I was able to create the above code by scanning posts from other people.  Trial and Error.  Thanks
Nagesh ENagesh E
{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")} 
 
var newRecords = [];
 
var c = new sforce.SObject("Custom_Quote_Request__c"); 
if(c.Submit_Quote_Request_Date__c =='')
{
c.id ="{!Custom_Quote_Request__c.Id}";
c.Submit_Quote_Request_Date__c = new Date();
newRecords.push(c); 
 
result = sforce.connection.update(newRecords); 
 
window.location.reload();
}
else
{
alert('Quote is already requestes');
}
Abhi_TripathiAbhi_Tripathi
You can make the button read only after the first click,
You can do this by performing the below process
 
<script> 
function myFunction() {

 //CALL YOU APEX METHOD THAT YOU WANT TO CALL FROM THE BUTTON 
document.getElementById("myBtn").disabled = true; 
}
 </script> 

<apex:commandButton id="myBtn" value="my button" onclick="myFunction()"/>

 
Tom SineTom Sine
Thank you everyone for the quick response. The above suggestion gets me exactly what I was looking for.
Tom SineTom Sine
I guess I should have tested this some more.  Nash5254 I tried your solution and it appeared to work.  I had a record with the date already populated and the error message appeared.  I did not test this on a record that needed to be submitted for the first time however.  I am getting the error message before the date is populated.  Hopefully this is something that can be easily addressed.  Any help you can provide is appreciated.
Nagesh ENagesh E
What is the error message you are getting. 
Tom SineTom Sine
That it's already been populated.  'Quote is already requested'.  You can see below that the date field is blank for this record, but I am getting the message that it's already been submitted.
User-added image'
Nagesh ENagesh E
Update if condition with 
if(c.Submit_Quote_Request_Date__c ==''  or c.Submit_Quote_Request_Date__c == null)
Tom SineTom Sine
I updated the if condition as seen below and I get an error that says "A problem with the OnClick JavaScript for this button or link was encountered: Unexpected identifier


{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")} 
 
var newRecords = [];
 
var c = new sforce.SObject("Custom_Quote_Request__c"); 
if(c.Submit_Quote_Request_Date__c =='' or c.Submit_Quote_Request_Date__c == null)
{
c.id ="{!Custom_Quote_Request__c.Id}";
c.Submit_Quote_Request_Date__c = new Date();
newRecords.push(c); 
 
result = sforce.connection.update(newRecords); 
 
window.location.reload();
}
else
{
alert('Quote is already requested');
}