Is it possible to wrap a standard page (Opportunity, Lead, Contact) with a custom page... and load it only if a certain condition is met? For example, I want to load any opportunity page only if the "Amount" is below a certain value. Otherwise, redirect the user to an error page.

  • You can override the detail page with a VF page and use <apex:detail> tag to show opportunity details then have a render condition to display the error msg in the same page.
    – Ranga
    Jul 12 '18 at 6:35
  • @Ranga, thank you very much, can I have a reference or example code? if so, please post your answer as an answer so it can assist others. Thank you! Jul 12 '18 at 6:55
<apex:page standardController="Opportunity" lightningStylesheets="true">
    <apex:detail subject="{!Opportunity.Id}" relatedList="true" title="true" rendered="{!Opportunity.Amount < 10}" />
    <apex:pageBlock rendered="{!Opportunity.Amount > 10}">
        <apex:pageBlockSection columns="1">
            <apex:pageMessage summary="Amount is less than 10" severity="warning" strength="3" />
  • Try wrapping your page message in a apex page block. eg. <apex:pageBlock><apex:pageBlockSection columns ="1"><apex:pageMessage ..../></apex:pageBlockSection><apex:pageBlock>
    – Ranga
    Jul 12 '18 at 8:07
  • One other thing, have you set lightningStylesheets=true in your VF page? If this doesn't work you may need to consider different approach for lightning. You can override detail view with a lightning component as well. developer.salesforce.com/docs/atlas.en-us.lightning.meta/…
    – Ranga
    Jul 12 '18 at 8:26
  • Other option I could think is applying a custom style(css) to <apex:pageMessage> when it is in lightning using {!$User.UIThemeDisplayed}
    – Ranga
    Jul 12 '18 at 8:41
  • Just tested the same code and it seem to be working fine for me. What exactly not working? Is it not redirecting to the apex page in lighting experience or the error message not aligned properly?
    – Ranga
    Jul 12 '18 at 12:19
  • Ah great! glad it worked out :)
    – Ranga
    Jul 12 '18 at 12:36

For your usecase, you could use an in-between redirection strategy.

By that I mean that you can set up a simple vfp with controller, or a flow, to do the redirection, and override the "View" button of your object (Opportunity i.e.) with it.

Here's a vfp example:

<apex:page standardController="Opportunity" extensions="OpportunityDispatcherCx" action="{!redirect}"/>

As you can see, the code of the page is really simple, only the standard controller to catch you Opportunity Id, a custom extension to make the redirection, and an action to redirect the user.

Here's an example for your controller:

public class OpportunityDispatcherCx {
    // Opportunity Variable
    Opportunity oOpportunity {get;set;} 

    public OpportunityDispatcherCx(ApexPages.standardController stdController) {
        oOpportunity = [Select Id, Amount
                        From Opportunity
                        Where Id = :stdController.getId()];

    public PageReference redirect() {
        // Construct a redirection url
        String redirectUrl = '';

        // Make your checks
        if(oOpportunity.Amount < 100) {
            redirectUrl = '/apex/errorPage?id=' + oOpportunity.Id;
        }else {
            redirectUrl = /apex/oppOverride?id=' + oOpportunity.Id + '&sfdc.override=1';

        // Set a pageReference for redirection
        PageReference pageRef = new PageReference(redirectUrl);

        // Return it for the redirection
        return pageRef;

Then on your "View" button, just set it up to the visualforce page.

The oppOverride visualforce page would just require simple code as follow:

<apex:page standardController="Opportunity">
    <apex:detail title="true" relatedList="true"/>

As mentioned above, this would only work for Classic.

  • 1
    This code will cause infinite recursion if will override View (In cases that the amount is > 100). The code will be called again and again for every successful redirection, leading to another redirection. Jul 15 '18 at 10:25
  • @LewisBushman you're right, forgot about that, you would need to setup a simple vfp with the override attribute. Updated the code.
    – Péqueño
    Jul 17 '18 at 7:23
  • 1
    Yeah thanks, just to note here for others - this code will still cause an infinite loop in Salesforce1 (mobile version). The reason is that nooverride is not supported on the mobile version. As for now, there is no solution for disabling override on Salesforce1. Jul 17 '18 at 7:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.