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
salesforcemicky125salesforcemicky125 

how to convert the Validation rule from Server side to client side using Java Script in salesforce

Hi ,

I have  stuck with the Validation Rules using the Java Script , Can anyone help me out from this.

I have implemeted the Validation rule for the Server Side , but i  have stuck with it in implemeting it in Java Script.

This is the Validation rule that i have implemented for Server Side for Multiple Stage and the same i want to implemet it in Client Side using the Java scripe

Here is the Server side Validation Rule 


OR
(
AND(OR(RecordTypeId ="01250000000UKjw",RecordTypeId="01250000000UKjv", RecordTypeId ="01250000000UMZm"),OR( ISPICKVAL(CRM_Stage__c,"Stage 3: National/CEO Interviews")), CRM_Gender__c =""),
AND(OR(RecordTypeId ="01250000000UKjw",RecordTypeId="01250000000UKjv", RecordTypeId ="01250000000UMZm"),OR(ISPICKVAL(CRM_Stage__c,"Stage 4: Board Interviews")), CRM_Gender__c =""),
AND(OR(RecordTypeId ="01250000000UKjw",RecordTypeId="01250000000UKjv", RecordTypeId ="01250000000UMZm"),OR(ISPICKVAL(CRM_Stage__c,"Stage 5: Board Package Finalization")), CRM_Gender__c =""),
AND(OR(RecordTypeId ="01250000000UKjw",RecordTypeId="01250000000UKjv", RecordTypeId ="01250000000UMZm"),OR( ISPICKVAL(CRM_Stage__c,"Stage 6: Package Review")), CRM_Gender__c =""),
AND(OR(RecordTypeId ="01250000000UKjw",RecordTypeId="01250000000UKjv", RecordTypeId ="01250000000UMZm"),OR(ISPICKVAL(CRM_Stage__c,"Stage 7: Approved/Agreement Finalization")), CRM_Gender__c =""),
AND(OR(RecordTypeId ="01250000000UKjw",RecordTypeId="01250000000UKjv", RecordTypeId ="01250000000UMZm"),OR(ISPICKVAL(CRM_Stage__c,"Stage 8: Provisioning/On-Boarding")), CRM_Gender__c =""),
AND(OR(RecordTypeId ="01250000000UKjw",RecordTypeId="01250000000UKjv", RecordTypeId ="01250000000UMZm"),OR(ISPICKVAL(CRM_Stage__c,"Stage 9: On-Board")), CRM_Gender__c =""))


And here is my Java script cod efor Validation rule

else if(voptygender== "" && (voptystage == 'Stage 3: National/CEO Interviews'))
{
    alert("Race is a required for Stage 3: National/CEO Interviews");
  document.getElementById('{!$Component.formid.op.keyid.optyrace}').focus();
}

This is working for only one Stage , but when i have implemeted it for the Multiple Stages it is throwing error whe it is in blank stage that is fine , but when i have save it with without blank with Specified message it has to save it but still it is throwing error

Here is the Code that i have implented for the Multiple stages using  Java Script

else if(voptygender == "" && (voptystage == 'Stage 3: National/CEO Interviews') || (voptystage == 'Stage 4: Board Interviews') || (voptystage == 'Stage 5: Board Package Finalization') || (voptystage == 'Stage 6: Package Review') || ( voptystage == 'Stage 7: Approved/Agreement Finalization') || (voptystage == 'Stage 8: Provisioning/On-Boarding') || ( voptystage== 'Stage 9: On-Board'))
{
  alert("Gender is a required for Stage 3: National/CEO Interviews");
  document.getElementById('{!$Component.formid.op.keyid.optygender}').focus();
}

Can Anyone correct me  where exactly i gone wrong 


Reagrds
Micky
Ashish_SFDCAshish_SFDC
Hi Micky, 

What is the error that you are getting, and on which line and which keyword you are getting the error. 

Did you try debug logs?

As you said it is working fine for one stage, i believe the sysntax for ispick val - if else has to be checked once. 

Regards,
Ashish
salesforcemicky125salesforcemicky125
Hi Ashish, This is the Code that is implemented for One Stage in Java Script and i want it to implement it for the Multiple Stages Here we go for single Stage code using the Java Script else if(voptygender== "" && (voptystage == 'Stage 3: National/CEO Interviews')) { alert("Race is a required for Stage 3: National/CEO Interviews"); document.getElementById('{!$Component.formid.op.keyid.optyrace}').focus(); } like this there are so many stage are there for my requirement which are yet to be implemented for above java script code i have implemented for multiple stages like this else if(voptygender == "" && (voptystage == 'Stage 3: National/CEO Interviews') || (voptystage == 'Stage 4: Board Interviews') || (voptystage == 'Stage 5: Board Package Finalization') || (voptystage == 'Stage 6: Package Review') || ( voptystage == 'Stage 7: Approved/Agreement Finalization') || (voptystage == 'Stage 8: Provisioning/On-Boarding') || ( voptystage== 'Stage 9: On-Board')) { alert("Gender is a required for Stage 3: National/CEO Interviews"); document.getElementById('{!$Component.formid.op.keyid. optygender}').focus(); } This code works fine for only Stage 3 Only, i.e the Code works some how for other stages but not fully When a Stage is blank it has to throw the Error that is working fine for all Stages , but when i save it with some text it saving for only Stage 3 but not for other Stages . from Stage 4 - Stage 9 it is not Saving the Record and Nor it is throwing the Error Message. Regards Micky
Ashish_SFDCAshish_SFDC
Hi Mickey,


Use this code,

else if

(voptygender == "" &&
                (
                         (voptystage== 'Stage 3: National/CEO Interviews') ||
                         (voptystage == 'Stage 4: Board Interviews') ||
                         (voptystage == 'Stage 5: Board Package Finalization') ||
                         (voptystage == 'Stage 6: Package Review') ||
                         (voptystage == 'Stage 7: Approved/Agreement Finalization') ||
                         (voptystage == 'Stage 8: Provisioning/On-Boarding') ||
                         (voptystage== 'Stage 9: On-Board')
                  )
)

{ alert("Gender is a required for Stage 3: National/CEO Interviews"); document.getElementById('{!$Component.formid.op.keyid. optygender}').focus(); }

Looks like, after the && it is checking only the first condition.

I added a extra set of braces to the 2nd conditioin, if any of the option returns true as a whole it will work.

Reply back if this goes through .


Regards,
Ashish