+ Start a Discussion
MayTheForceBeWithYouMayTheForceBeWithYou 

Standard Button Override Ramifications

I needed to come up with a way to override the standard "New" button for the Accounts object. The button now redirects the user to an invalid date page by default, but redirects back to the usual New Account page if my Apex class determines that the current month is within a particular range.  After succeeding in my goal I noticed that now if the user is attempting to enter a new account during a valid month and he/she is in fact brought to the New Account page, then the "Cancel" button here is no longer functioning properly.  I believe this has to do with the redirect I created but I am unsure-any help you can provide would be greatly appreciated!  Below you will find my VF Page and my Apex Class:

 

<apex:page standardController="Account" extensions="DateCheck" action="{!pageredir}">
<apex:sectionHeader title="Invalid Date"/>
<apex:outputPanel >
Unfortunately, you cannot create new accounts this month-please try again during a valid month.
</apex:outputPanel>
</apex:page>

 

public class DateCheck {

    public DateCheck(ApexPages.StandardController controller) {

    }

    public Pagereference pageredir(){

        Pagereference newPage = new Pagereference ('/001/e?retURL=/001/');

        Date dateToday = date.Today();

        Integer month = dateToday.month();

        newPage.getParameters().put('nooverride', '1');

        if(new Set<Integer>{1,2,3,10,11,12}.contains(month)) {

            return newPage;

         }

        else

            return null;

    }

}

 

Thanks!

 

 

P.S. On a similar note-I'd like to hide the Quick Create Sidebar menu since this circumvents my Apex code and allows the user to save a new account regardless of date-I know I can turn this off in Setup but does anyone know if there is a way to turn this off conditionally as I did with the "New" button's functionality?  Thanks!

Best Answer chosen by Admin (Salesforce Developers) 
Ritesh AswaneyRitesh Aswaney

If you hit the default new Account button, you'll see that the URL should be

Pagereference newPage = new Pagereference ('/001/e?retURL=/001/'o);

 

that is with an 'o' at the end. Should work okay.

All Answers

Ritesh AswaneyRitesh Aswaney

If you hit the default new Account button, you'll see that the URL should be

Pagereference newPage = new Pagereference ('/001/e?retURL=/001/'o);

 

that is with an 'o' at the end. Should work okay.

This was selected as the best answer
MayTheForceBeWithYouMayTheForceBeWithYou

Ritesh,

 

You hit the nail on the head; adding the 'o' solved my problem-thanks!