function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Marco PinderMarco Pinder 

Custom Button to start Flow with JavaScript

Hi,

 

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:

 

/flow/Enable_SLink_Access
&varContactId={!Contact.Id}
&retURL=/{!Contact.Id}

 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
{!RequireScript("/soap/ajax/10.0/connection.js")}

if ( '{!User.Profile}' != 'System Administrator' )
{
alert('You are not authorised to edit SLink Settings for this Contact');
}
else
{
window.open('/flow/Enable_SLink_Access&varContactId={!Contact.Id}&retURL=/{!Contact.Id}');
}

 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,

 

Marco

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}');
window.open(uriToOpen);

 Hope this helps!

 

- Anup

StaciStaci

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:
/flow/EnableSLink_Access&varContactIdd={!Contact.Id}&retURL=/{!Contact.Id}
should have been:
/flow/EnableSLink_Access?varContactIdd={!Contact.Id}&retURL=/{!Contact.Id}
(note the first & was changed to a ?)
The first argument must open with a ?.