+ Start a Discussion
Marco PinderMarco Pinder 

Custom Button to start Flow with JavaScript



I originally created a custom button on the Contact page layout that allowed me to start a flow. The code that worked fine is as follows:



 This worked great. However I now need to add some JavaScript to this button to analyse the current users profile to allow or deny running the flow.


The new code now looks like this:


//Required by Salesforce.com to run OnClick JavaScript

if ( '{!User.Profile}' != 'System Administrator' )
alert('You are not authorised to edit SLink Settings for this Contact');

 However now it takes me to an invalid URL where the message on screen says 'URL No Longer Exists'.


The URL has become: https://cs2.salesforce.com/flow/Enable_SLink_Access&varContactId=003R000000Z7dEv&retURL=/003R000000Z7dEv


Can anyone help me with the code I should dress up the flow bit of the code with please?


Thanks very much,



Anup JadhavAnup Jadhav

Hi Marco,


I'd recommend capturing the base url somewhere in the controller like this:

String baseUrl = URL.getSalesforceBaseUrl();

 Then use this baseUrl along with the builtIn javascript functions: encodeURIComponent(str) and encodeURI(str).


In your case:


var uriToOpen = {!baseUrl}+encodeURI('/flow/Enable_SLink_Access&varContactId={!Contact.Id}&retURL=/{!Contact.Id}');

 Hope this helps!


- Anup


I need this same functionality.  Where do you put the baseUrl?  Which controller?  I'm just using a button, do buttons have controllers attached to them? I know very little about JavaScript

Derek Wiers (W)Derek Wiers (W)
I know this is, as they say, 'thread necromancy' since it's been 2 years, but just for the sake of those google-searching this forum, the issue would have been one with the url:
should have been:
(note the first & was changed to a ?)
The first argument must open with a ?.