0

I'm trying to get a button to open a standard record page in Salesforce classic view. I can get it to open in a new tab, but not in the same tab. I want to override the existing tab.

Example of what I'm trying to do:

<apex:page showHeader="false" sidebar="false" docType="html-5.0">
    <head>        
        <script type='text/javascript'>

            function dummyTab(url){
                //window.open(url,'_top'); //This Doesn't work
                //window.location.href = url; //This Doesn't work
                window.open(url,'_blank'); //This works. Opens in a new window
            }

        </script>
    </head>
    <body>
        <apex:form>
            <apex:pageBlock >
                <apex:pageBlockButtons location="top">
                    <apex:commandButton onclick="dummyTab('https://www.google.com');" value="Cancel"/>                
                </apex:pageBlockButtons>
            </apex:pageBlock>
        </apex:form>
    </body>    
</apex:page>

Is there anything I'm missing?

1 Answer 1

1

You're not cancelling the default action of the button, so it ends up submitting the form, cancelling the prior navigation. There's easier ways to do what you're trying to do, but at the most basic level, you can just do this:

<apex:commandButton 
    onclick="dummyTab('https://www.google.com'); return false;" value="Cancel"/>

By returning false, the browser cancels the command button's default action (submitting the form). Your JavaScript should then be able to override the current window's location.


If you're using a Standard Controller, you can just call Cancel:

<apex:commandButton action="{!cancel}" value="Cancel" />

Or, you can set the action to go to a specific location:

<apex:commandButton action="{!URLFOR($ObjectType.Account.View, record.Id)}"
  value="Cancel" />

Also, since you may be faced with required attributes, you can combine either of the above techniques with the immediate attribute to skip validations:

<apex:commandButton action="{!URLFOR($ObjectType.Account.View, record.Id)}"
  value="Cancel" immediate="true" />
1
  • I ended up going 'return false;' route.
    – Avinash
    Commented Feb 16, 2018 at 1:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .