[SalesForce] OnClick JavaScript Validation Rule Help

I have a button that when clicked will update a checkbox field in the back end of a custom object that will then fire a workflow to send out a company wide email. I want to make it so the user can only click that button if certain criteria is met.

I created two validation rules that make this work, but the only issue is that the error message doesn't display. So the user would be unable to tell that their email was not sent out. Below is all the detail about the button and the validation rules I have created. My question is do I need to code some validation into the button itself? Or am I missing something with the VR rules?

Button:

{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")} 
var ss = new sforce.SObject('Success_Story__c'); 
ss.id = "{!Success_Story__c.Id}"; 
ss.Send_Email__c = "TRUE"; 
result = sforce.connection.update([ss]); 
location.reload(true);

Validation Rule 1:

AND( 
ISBLANK(Amount__c), 
RecordType.Id = "01231000001Idj3", 
Send_Email__c = TRUE)

Validation Rule 2:

AND( 
ISBLANK(Number_of_Hired_Drivers__c), 
RecordTypeId = "01231000001Idj8", 
Send_Email__c = TRUE)

Also I should add that Send_Email__c is the checkbox that is being updated to true by the button.

Thanks,

Best Answer

You have to options:

  1. do the validations on the fly using javascript; or
  2. handle the salesforce update call like this:
{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")} 
var ss = new sforce.SObject('Success_Story__c'); 
ss.id = "{!Success_Story__c.Id}"; 
ss.Send_Email__c = "TRUE"; 
result = sforce.connection.update([ss]);

if (result[0].getBoolean("success")) {
  log("Success story with id " + result[0].id + " updated");
  location.reload(true);
} else {
  alert("failed to update Success story " + result[0]);
}

You can take a look at the Ajax Toolkit Developer Guide for more examples.

Related Topic