Salesforce Formula Hack

Javascript in a Formula

Have you ever imagine writing Javascript in a Salesforce Field? No? Well, it's possible.

Formula field in Salesforce allows with the "HYPERLINK" to create link with different options in the target parameter like _blank, _parent... However you are limited with the possibility on what you open. You can however make a kind of "parameter injection" to display other parameter on the rendered link like an onclick parameter which allow you for example to display an alert when the link is clicked or to open a popup. Here is a formula to open a Visualforce Page and to center it in the middle of the screen:

HYPERLINK('javascript:void(0);', 'My Field....' , " \" onclick=\"window.open('/apex/MyPage?id=" + Id + "', 'MyPage', 'width=500,height=550,left=' + (screen.width/2-250) + ',top=' + (screen.height/2-275) );return false;")
comments powered by Disqus