Is it possible, via the API or otherwise, to override the View button of an object with a URL (instead of a VF page)?


You can't directly override the View action with a non-VF page, but the VF page could directly redirect to any arbitrary URL:

<apex:page standardController="myObject">
        window.top.location.href = 'someurl?id={!myObject.Id}';

Edit: You can hijack the page's loading process by way of JavaScript. First, create a static resource that contains the JavaScript you want to run. Here's an example:

(function() {
    window.top.location.href = 'https://www.google.com/';

Next, use RequireScript in a custom button or link. The name and label doesn't really matter, but it does need to be on the page layout. Here's an example:

Label: Override
Name: Override
Description: Sends the user elsewhere when viewing this record.
Display Type: Detail Page Button
Behavior: Execute JavaScript
Content Source: OnClick JavaScript

Finally, add this button (as written above) or link (you can choose) to the appropriate section on a page layout. What happens is that as soon as the DOM finishes loading, your script will be injected into the page, at which point it will execute, causing the user to be redirected to the new page (in this example, Google, but you could use any target you desire). If you make this a list view button instead, you can apply to any parent object that shows the related list by updating the parent's related list for that object.

There used to be other means of accomplishing this, notably the sidebar hack, and it should still work (in theory) using Visualforce sidebar components, but if you're adamant about not creating new pages at all, then that route is closed to you as well.

  • 1
    I would think it might be preferable to simply use action="someurl?id={!myObject.Id}" in your page tag and it can even be self closing. Might also want to turn off stylesheets, header, sidebar to speed things up.
    – Adrian Larson
    May 11 '15 at 2:58
  • Under the covers, I think action also does a JS redirect, but I agree that style sheets and such should probably be left out.
    – sfdcfox
    May 11 '15 at 3:18
  • Thanks sfdcfox and Adrian. But I was explicitly looking for a JS override. It's basically about using a single VF page to override many objects.
    – Mossi
    May 11 '15 at 18:32
  • You can create a master page, then use apex:include to build one page for each object. Each object's page will be just three lines of code.
    – sfdcfox
    May 11 '15 at 18:51
  • This particular use-case of mine doesn't allow creation of any new VF page besides the one I am trying to work with. I literally need to redirect to this page upon View.
    – Mossi
    May 12 '15 at 0:00

Which view button? if you are referring to the Name field of an object that redirect you to view/edit state of pages - basically you can add a new formula field that takes any parameter and redirect you to a URL :

HYPERLINK('/'& Id&'/e','Edit ' + {!Account.Name},'_blank')

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.