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
Praveen BonaluPraveen Bonalu 

Disable the javascript button after it clicked once

Hi ,

I have written a javascript button to create a record on salesforce .when the user clicks once on the button the record is created .but he clicks again on the button another record is created.

I just dont want the user to click the button again and create a duplicate record.Is there any other way to disable the button after the user clicks once or else if the record is already created .he needs to be prompted  that  the record is already exixts.
 
{!requireScript("/soap/ajax/26.0/connection.js")} 

var req = new sforce.SObject("Candidate__c"); 
req.Id = "{!Candidate__c.Id}"; 
req.Job_Application__c="True"; 
req.Recruiting_Status__c="Possible Candidate" 
var aResult = sforce.connection.update([req]); 
if (aResult[0].getBoolean("success")) { 
console.log("Candidate Update Successful"); 
} 
var newJob = new sforce.SObject("Job_Application__c"); 
newJob.Candidate__c= "{!Candidate__c.Id}"; 
newJob.Position__c= "{!Candidate__c.PositionId__c}"; 
newJob.Status__c="open"; 

var cResult = sforce.connection.create([newJob]); 
if ( cResult[0].getBoolean( "success" ) ) 
{ 
alert("Candidate Created Sucessfully "); 
window.location.reload(); 
} 

else 
{ 
alert( cResult[0].errors.message); 
}

 
Best Answer chosen by Praveen Bonalu
Bhaswanthnaga vivek vutukuriBhaswanthnaga vivek vutukuri
{!requireScript("/soap/ajax/26.0/connection.js")} 

var candidateiD= "{!Candidate__c.Id}";
var checkJob= sforce.connection.query("select Candidate__c from  Job_Application__c Where Candidate__c ='" +"{!candidate__c.Id}"+ "'"); 
var records = checkJob.getArray("records");
if(records.length == 0){ 

var req = new sforce.SObject("Candidate__c"); 
req.Id = "{!Candidate__c.Id}"; 
req.Job_Application__c="True"; 
req.Recruiting_Status__c="Possible Candidate" 

var aResult = sforce.connection.update([req]); 
if (aResult[0].getBoolean("success")) { 
console.log("Candidate Update Successful"); 
} 
var newJob = new sforce.SObject("Job_Application__c"); 
newJob.Candidate__c= "{!Candidate__c.Id}"; 
newJob.Position__c= "{!Candidate__c.PositionId__c}"; 
newJob.Status__c="open"; 

var cResult = sforce.connection.create([newJob]); 
if ( cResult[0].getBoolean( "success" ) ) 
{ 
alert("Candidate Created Sucessfully "); 
window.location.reload(); 
} 

else 
{ 
alert( cResult[0].errors.message); 
}

}

else
{
    alert('Job already created');
}

Use this, if any prblm whats app me +642206205059 that's easy

All Answers

JAY_PJAY_P
Hey Praveen,
you can use render operation and try to write some code for render function that once is created the button is hidden like that.
Thank You

 
Bhaswanthnaga vivek vutukuriBhaswanthnaga vivek vutukuri
Unfortunately we can't disable the button, I think best option would be keeping a confirm box before entering into the logic OR query for the record which you have inserted first time if the record exist just escape the logic and display an alert message
 
Bhaswanthnaga vivek vutukuriBhaswanthnaga vivek vutukuri
{!requireScript("/soap/ajax/26.0/connection.js")} 

var candidateiD= "{!Candidate__c.Id}";
var checkJob= sforce.connection.query("select Candidate__c from  Job_Application__c Where Candidate__c =: candidateiD  LIMIT 1"); 
if(checkJob != null){ 

var req = new sforce.SObject("Candidate__c"); 
req.Id = "{!Candidate__c.Id}"; 
req.Job_Application__c="True"; 
req.Recruiting_Status__c="Possible Candidate" 

var aResult = sforce.connection.update([req]); 
if (aResult[0].getBoolean("success")) { 
console.log("Candidate Update Successful"); 
} 
var newJob = new sforce.SObject("Job_Application__c"); 
newJob.Candidate__c= "{!Candidate__c.Id}"; 
newJob.Position__c= "{!Candidate__c.PositionId__c}"; 
newJob.Status__c="open"; 

var cResult = sforce.connection.create([newJob]); 
if ( cResult[0].getBoolean( "success" ) ) 
{ 
alert("Candidate Created Sucessfully "); 
window.location.reload(); 
} 

else 
{ 
alert( cResult[0].errors.message); 
}

}

else
{
    alert('Job already created');
}
Praveen BonaluPraveen Bonalu
Hi vivek ,

I am getting the following error after implementing the code  can you please help me to resolve the issue 


Error

 
Bhaswanthnaga vivek vutukuriBhaswanthnaga vivek vutukuri

("select Candidate__c from  Job_Application__c Where Candidate__c =: " '+candidateiD+' "  LIMIT 1"):

please use this query instead.

Praveen BonaluPraveen Bonalu
the error is no more coming but,If i click the button it still creates a new record again .
venkat-Dvenkat-D
Another way you can acheive is chage record type of the record after clicking it and then remove that button in new wlayout. This way they wont see button after clicking once.
Bhaswanthnaga vivek vutukuriBhaswanthnaga vivek vutukuri
{!requireScript("/soap/ajax/26.0/connection.js")} 

var candidateiD= "{!Candidate__c.Id}";
var checkJob= sforce.connection.query("select Candidate__c from  Job_Application__c Where Candidate__c ='" +"{!candidate__c.Id}"+ "'"); 
var records = checkJob.getArray("records");
if(records.length == 0){ 

var req = new sforce.SObject("Candidate__c"); 
req.Id = "{!Candidate__c.Id}"; 
req.Job_Application__c="True"; 
req.Recruiting_Status__c="Possible Candidate" 

var aResult = sforce.connection.update([req]); 
if (aResult[0].getBoolean("success")) { 
console.log("Candidate Update Successful"); 
} 
var newJob = new sforce.SObject("Job_Application__c"); 
newJob.Candidate__c= "{!Candidate__c.Id}"; 
newJob.Position__c= "{!Candidate__c.PositionId__c}"; 
newJob.Status__c="open"; 

var cResult = sforce.connection.create([newJob]); 
if ( cResult[0].getBoolean( "success" ) ) 
{ 
alert("Candidate Created Sucessfully "); 
window.location.reload(); 
} 

else 
{ 
alert( cResult[0].errors.message); 
}

}

else
{
    alert('Job already created');
}

Use this, if any prblm whats app me +642206205059 that's easy
This was selected as the best answer
Praveen BonaluPraveen Bonalu
This code snippt worked ...
Thanks vivek.
 
Praveen BonaluPraveen Bonalu
Hi Vivek,

Do you any ideas how to achive this feature in salesforce .. please share you ideas on this funtionality 
https://developer.salesforce.com/forums/ForumsMain?id=906F0000000D6pjIAC

Thanks
Praveen