[SalesForce] How to execute the standard Onclick Javascript code from Detail Page Button in a Visualforce Page

I've a custom button in the detail view of the object (type is onClick Javascript). I have a visualforce Page where I'm trying to replicate the same functionality done through the Onclick JS button in the page.

{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")}
var result = 'www.google.com';
window.top.location.href = result;

I was wondering if the button code can be brought in to execute the code. Metadata API was the approach I chose. I queried the button details through Metadata and I got the simple JS working. However, when I have buttons that use connection.js & apex.js, BOOM! They don't work.

I replaced the merge fields those used in the JS script to be replaced with the actual value using a Util class. How do I get the {!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")} scripts to work in the page as well?

Thanks in Advance!

Best Answer

If you are calling from a VF page then the code remains same but ensure that you have added scripts to the top of VF page as below:

<apex:includeScript value="/soap/ajax/30.0/connection.js"/>
<apex:includeScript value="/soap/ajax/30.0/apex.js"/>
Related Topic