0

I had a custom button that used OnClick JavaScript behavior and when it was clicked would execute a bunch of JavaScript which would ultimately build a url and open it in a new window for the user.

There was quite a bit of JavaScript logic to determine the exact url which was difficult to read due to the lack of formatting with Salesforce for the custom button window. Also there are quite a few buttons doing similar logic. Not to mention that Salesforce recommends against using the JavaScript buttons.

Therefore I'd like to pull the logic out of a Custom Button and have created a new Visualforce page that is just a command button that executes the JavaScript which has been moved out as well. I did this because it will now be easier to read/format in IntelliJ and add to it when needed. Also if there is overlap in logic it can be put in one JS file and reused rather than spread through custom buttons.

I created the Visualforce button similar to this:

<apex:page standardController="Custom_Object__c">    
    <apex:form>
        <apex:pageMessages />
        <apex:commandButton value="Open Window"
                            onclick="return openWindow();"/>
    </apex:form>
    <script>
        // Bunch of JavaScript logic to create url
        window.open(url);
    </script>
</apex:page>

I now have the functionality working exactly like it did in the Custom Button logic.

However, since it is a Visualforce page, I am unable to add it to the Custom Button section of the page layouts. Is there a way for me to add it to that section of the page layouts so it is consistently located with the other buttons?

Or is there an approach that better suits my needs for what I was trying to accomplish?

We are on Salesforce Classic if it makes a difference in options.

  • You can create a custom button with the Content Source as VF page to be able to use it on the page layout, are you having trouble doing so? – Jayant Das Apr 15 at 19:07
  • @JayantDas Yeah I did try that. It sort of 'works' but is not optimal either. When I did that, after clicking the button on the page it opens up a new window which is the Visualforce page of just the button I made, and then after clicking that it opens the new window. So essentially when I did that, I had to click the original custom button to get to my Visualforce button I actually wanted to click. – Developer Guy Apr 15 at 19:14
  • There's no other way unfortunately to achieve what you are looking for unless you customize your screen. – Jayant Das Apr 15 at 19:15
1

The following 2 updates to your visualforce page may do the trick for you:

  1. Instead of using a commandButton, use the window's onLoad even to call your openWindow method.

  2. Instead of using window.open to open a popup, you can simply redirect the user to the new url (since you are already in a popup window now).

<apex:page standardController="Custom_Object__c">
    <apex:form>`
        <apex:pageMessages />
    </apex:form>
    <script>
        window.onload = function(){
            openWindow();
        }

        function openWindow(){
            // Bunch of JavaScript logic to create url
            window.location.href = my_new_url;
        }
    </script>
</apex:page>
  • The one thing I dislike about this is the split second that a user winds up on the blank Visualforce page prior to the window location updating them to the new url; there is time in there for them to wonder what is going on or click around. Other than that, this seems to be pretty close to what I am looking for. – Developer Guy Apr 15 at 20:45
  • You can add a spinner animation with a Please Wait... text to make it look natural. – Aayush K Apr 16 at 15:11

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.