+ Start a Discussion
DennoPDennoP 

How do I add validation to a custom button

I have added a custom button to a custom object (employee__c).  The button takes to user to a visualforce page based on a URL and userid field held in one of the fields on the employee__c record.

 

 

I would like to know how I can make sure that if the field is blank the user is not taken anywhere by the custom button, or even better the custom button gives a usefull message e.g. "The user field is blank".

 

Does anyone have any idea how this sort of valiation can be added on the button.

 

Any help is appretiated  :smileyhappy:

Best Answer chosen by Admin (Salesforce Developers) 
DennoPDennoP

I started with you example, but had a couple of errors.  This amended version (fixed by a friend, not me) seems to work now.

 

 

if ({!ISNULL( Employee__c.Salesforce_User__c )}) { alert('The user field is blank!');} else { location.replace('/apex/userupdate?id={!Employee__c.Salesforce_UserId__c}&previd={!Employee__c.Employee_Record_ID__c}');}

 

Thanks 

All Answers

werewolfwerewolf

Set the custom button to execute Javascript instead of navigate the page, and then put a JS if statement at the beginning, like

 

 

if ('{Employee__c.User__c}'=='') { alert('The user field is blank!'); } else { location.replace('/apex/MyVisualforcePage'); }

 

 

 

DennoPDennoP

I started with you example, but had a couple of errors.  This amended version (fixed by a friend, not me) seems to work now.

 

 

if ({!ISNULL( Employee__c.Salesforce_User__c )}) { alert('The user field is blank!');} else { location.replace('/apex/userupdate?id={!Employee__c.Salesforce_UserId__c}&previd={!Employee__c.Employee_Record_ID__c}');}

 

Thanks 

This was selected as the best answer