So I have a button on a Custom Object that simply updates a checkbox field. I am trying to add an if statement to this button so that if the checkbox was already checked when the button was clicked it displays a window stating it was already selected and cannot be selected again. Below is what I have so far. Any and all help would be greatly appreciated.
{!REQUIRESCRIPT("/soap/ajax/33.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/33.0/apex.js")}
var bo = new sforce.SObject("Buyer__c");
if(bo.Put_Trucks_on_48_Hour_Hold__c = 'True')
{
alert('Error : This Buyers Order has already been placed within a 48 Hour Hold and cannot be placed within one again.');
}
else{
bo.Id = '{!Buyer__c.Id }';
bo.Put_Trucks_on_48_Hour_Hold__c = 'True';
var result = sforce.connection.update([bo]);
if(result[0].getBoolean("success"))
{
window.location.reload();
}
else{
alert('Error : '+result);
}
}
Best Answer
Your code has a few problems.
bo
is simply a record "in memory" and has no relation to anything in the database. Your alert will never happen because you haven't loaded the data.=
is assignment, not comparison. Use==
for comparison.True
or'True'
will never match. In JavaScript, it is always justtrue
.Don't compare a Boolean to true or false. It's already true or false.
The apex library is only needed for the likes of sforce.apex.execute or sforce.apex.executeAnonymous. You don't need it here.
The code you're looking for would be more like this:
Note: It's been a while since I've used the Toolkit, so the result handling might not be exact. Please check the documentation.