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
benwrigleybenwrigley 

Validation best practise

Hi There,

 

I need some advise on best practise wrt to buttons on page layouts.

 

I have a number of custom buttons that link to various VF pages. These buttons should only load the VF page if certain criteria are met e.g. sharing rules, authorisation etc.

 

The question I have is where to make these checks?

 

In the past I would have done the validation in an s-control but I think my options now are :

 

1) Make the button execute javascript and show an error if validation fails.

2) Have an interim VF page that checks validation through the controller and displays an error or redirects to the correct page.

3) Embed validation into 'rendered' fields of VF components

4) Continue using s-controls.

 

Is there any strong opinion on this?

 

TIA

Best Answer chosen by Admin (Salesforce Developers) 
Ispita_NavatarIspita_Navatar

I would go by the first two options:-

1. Make the button execute javascript and show an error if validation fails- as this would put the logic for access at the entry point and the bifurcation of navigation will reside at the entry point.

 

2) Have an interim VF page that checks validation through the controller and displays an error or redirects to the correct page. -> This approach is similar to the earlier one but it is based on the platform's native language hence is desirable .

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.

 

All Answers

Ispita_NavatarIspita_Navatar

I would go by the first two options:-

1. Make the button execute javascript and show an error if validation fails- as this would put the logic for access at the entry point and the bifurcation of navigation will reside at the entry point.

 

2) Have an interim VF page that checks validation through the controller and displays an error or redirects to the correct page. -> This approach is similar to the earlier one but it is based on the platform's native language hence is desirable .

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.

 

This was selected as the best answer
b-Forceb-Force

Ispita,

 

Second  option is more relaible , and with minimum code changes

All redirection logic at Centralized place is always good choice for maintaince purpose

 

Its good Idea to Create One "Permission Denied Page " and redirect user to that page if anything went wrong  :)

 

Thanks,

Bala

 

 

benwrigleybenwrigley

Hi There,

 

I've implemented option 2 by using the action attribute of the apex:Page component. This give me the option to call a validation method before displaying the page.

 

However, if validation fails, I am redirecting the user back to the Opportunity page that they came from, I want to display an error message on that Opportunity page. How can I do that?

 

I tried this as a test:

 

 

public PageReference validate(){

    Opportunity Opp = loadOpportunity();		
    Opp.addError('validation failed');
    PageReference oppPage = new  ApexPages.StandardController(Opp).view();
    return oppPage;
}

 

no error message is displayed.

 

Any thoughts?