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
Jake GharibJake Gharib 

Validation Rule Not Firing with OnClick JavaScript Button

Hi all,

We have a custom button below that changes Ownership and the Status of a Case record if a specific value is placed in one of our Locale fields.  We also have a few Validation Rules in place to make sure Ownership cannot change without details being filled in.  When attempting to change Ownership manually in Salesforce, the Validation Rules fire without issue.  However, when using the JavaScript Button, it's basically ignoring the Rules and changing Ownership anyway.  Does anyone know why this is or what we may be doing wrong?  I've included both the button and our Validation Rules below.  Thank you for any help in advance.

Custom Button:
{!REQUIRESCRIPT("/soap/ajax/26.0/connection.js")}

var query = "select Id, CSM_Locale__c, OwnerId, Status from Case where Id = '{!Case.Id}' limit 1";

var result = sforce.connection.query(query); 
var records = result.getArray("records"); 
        
var caseObj = records[0];

if(caseObj.CSM_Locale__c =="en-US"){
caseObj.OwnerId = '00GL0000003E78X';}
else{
caseObj.OwnerId = '00GL0000003E78d';}
caseObj.Status = "Customer Confirmed / Sent to Supply Chain";
var results = sforce.connection.update([caseObj]);
window.location.href=window.location.href;

Validation Rule 1:

AND(
RecordType.Name = "Customer Supplied Materials",
ISPICKVAL(Status, "New"),
ISCHANGED(OwnerId),
OR(
ISBLANK(AccountId),
ISBLANK(TEXT(Type)),
ISBLANK(TEXT(Supplying__c))
))

Validation Rule 2:

AND(
RecordType.Name = "Customer Supplied Materials",
ISPICKVAL(Status, "New"),
ISCHANGED(OwnerId),
ISPICKVAL(Type, "Stocked"),
OR(
ISBLANK(AccountId),
ISBLANK(Trade_Name_and_Grade_with_Colorant__c),
ISBLANK(Proprietary_Formulation__c),
ISBLANK(Colorant_Part_Number__c),
ISBLANK(Colorant_Description__c),
ISBLANK(Colorant_Mix__c),
ISBLANK(Safety_Stock_in_lbs__c),
ISBLANK(Supplier_Contact__c),
ISBLANK(Supplier_Name__c)
))
Best Answer chosen by Jake Gharib
David Zhu 🔥David Zhu 🔥
That is the behavior of Javascript button. 
You can add the following code to enhance it.

if (results[0].success == 'false')
{
    alert(results[0].errors.message);
}
else
{
    window.location.href=window.location.href;
}

All Answers

David Zhu 🔥David Zhu 🔥
Javascript changes the status to "Customer Confirmed / Sent to Supply Chain" before saving.
 caseObj.Status = "Customer Confirmed / Sent to Supply Chain";"

The validation rules only check "New" Status.
ISPICKVAL(Status, "New"),
 
Jake GharibJake Gharib

Hi David,
 

Thanks for the clarification.  I've updated the Rules without the Status and it seems to work.  Another question that popped up is now the button won't change the Ownership with the Validation Rule (which is good), but it won't display the "error message" telling them why.  Is there a way around that or is that the normal function?

David Zhu 🔥David Zhu 🔥
That is the behavior of Javascript button. 
You can add the following code to enhance it.

if (results[0].success == 'false')
{
    alert(results[0].errors.message);
}
else
{
    window.location.href=window.location.href;
}
This was selected as the best answer