Andy in the Cloud

From BBC Basic to Force.com and beyond…

Clicks not Code with Visual Flow Custom Buttons

31 Comments

officehoursI attended an MVP Office Hours event last Friday which has lead to this blog. If your not aware this a weekly event run by fellow MVP’s Jarrod Kingston and Joshua Hoskins, which is a kind of open hour for anybody to pose admin or developer related questions to the MVP community. Last weeks was a great learning event for all involved, i would recommend dropping by, even if its just to ease drop, though there is no reason to be shy!

One question that was raised, was as if a Workflow Rule defined on an Opportunity Product Line could be started via a Custom Button at the Opportunity level. When clicked would effectively update the lines to run the Workflow Rule, in this case an Email Workflow. Ideally without JavaScript or having to resort to Apex. So can record, create, read, update and delete be done in a code free way? Read on...

FlowUpdateBeing very excited about the possibility of invoking the hugely under stated Visual Flow rules from a Workflow Rule in Spring’14 (Pilot only). I pretty much have Flow on my mind at the moment! So started pondering if any of the Flow “elements” that are capable of reading and writing to custom object records would do the trick? Since i knew we already run a Flow from a Custom Button. I continued to ponder after the call and today managed to confirm that it is indeed possible accomplish the requirement with a astonishingly simple Flow, in fact so simple its one element!

The Record Update flow element allows you to query a Standard or Custom object with some criteria and then update the results rows based on fixed or variable values you define (again via the point and click Flow Designer). My test was simple, could i set the Quantity to 1 for all the lines, though much more complex filtering and updates are possible. The configuration of the Update Lines element looks like this.

recordupdate

The {!OpportunityId} expression refers to a Flow variable, which i would later populate from my Custom Button. As you can see both the Filters and Assignments support addition rows, so this example is really a very basic one. Note that it has none of the Screen or Choice elements Flow supports, its just performing an update action, that’s all. Making sure i set it as the Start element by hovering over it and click the green icon. I then saved and started to figure out the best way to go about running it from a Custom Button.

The first option i looked at was the URL option, via a URL Custom Button, this was easy enough and i could pass the OpportunityId parameter via a a URL parameter. The issue with this, is when the Flow completes returning the user to the Opportunity page. The solution eventually was to wrap the Flow in a small Visualforce page (so ok i tiny bit of code i confess, but you really don’t have to touch this at all after you create it) then point the Custom Button to that instead.

<apex:page standardController="Opportunity">
    <flow:interview name="UpdateLines" finishLocation="/{!Opportunity.Id}">
        <apex:param name="OpportunityId" value="{!Opportunity.Id}"/>
    </flow:interview>
</apex:page>

updateflowcustomb

This allowed me to use the finishLocation attribute to define a URL that the Flow would navigate to after it completed. Since I have no “visual” aspects to my Flow, it simply executed the Update Records element and performed the redirect back to Opportunity detail page immediately. So once i defined my button and put it on the layout, i was ready to give it a go! 

It works great, really well in fact!

flowbutton

opplines

NOTE: If your concerned about security regarding CSRF, technically the recommendation above would be to present the user with a Flow Screen element asking them to confirm before proceeding. I’ve left this out here as i know its not always everyones preference visually to have what might be viewed as a annoying confirmation prompt following the button press.

Flow supports other data manipulation elements (in addition to many UI elements), such as Lookup, Create and Delete you can configure using its drag and drop editor. So if you fancy an alternative to hacking around with JavaScript Custom Buttons or want to avoid needing developers to write you Apex code for simple updates, i recommend you try this out (there is a great two part blog here and here and also workbook).

Certainly once Salesforce unleash (the both exciting and slightly scary) power of using these from Workflow Rules, you’ll be missing out on a significant new “Clicks Not Code” trick if you don’t! 

Detailed Steps to Recreate above Demo

  1. Under Setup menu, under Workflow and Approvals, click Flows.
  2. Create a new Flow and drag the Update Records element from the palette and give it a name (anything will do) and description.
  3. Complete the Update Record element configuration as shown above, when prompted for the Value to select by the OpportunityId, click the drop down menu and select CREATE NEW Variable.
    createvar
  4. Complete the Variable element configuration as shown below, take note to enter the variable name correctly, this will be referenced on the Visualforce page used to run the flow.
    varconfig
  5. Click to highlight the Update Records element on the Flow design canvas, then click the green icon in its top right corner to make this element the Start element.
    startelm
  6. Save the Flow and make it Active (clicking the Activate link next to it on the Flow detail page), take note of the Flow name used (defaults to the description with underscores for spaces).
  7. Create a Visualforce page via Setup, then Develop, then Pages, paste in the sample Visualforce page shown above, taking note to use correct Flow name and Flow parameter names.
  8. Create a Custom Button as shown in the screenshot above and add it to the Layout
  9. Press the button and enjoy your code free creation!

31 thoughts on “Clicks not Code with Visual Flow Custom Buttons

  1. Great job on the post!
    Glad I’m not the only one who feels like the Visual Flow from Workflow Rules update is hugely understated. Personally, I think that should be the headliner!
    Another trick we’ve used flows for is to accomplish a ‘cross-object workflow field update’. Very similar to what you describe above, we point the retURL parameter of the custom button url to the VF page that launches the flow after something like a task is saved. The flow is able to lookup and update fields on other objects (child objects or unrelated objects) which you can’t do with a typical workflow field update.

    • Thanks Scott! Using Flow via Workflow to fill gaps in the Workflow Field Update is high on my list of things to try in the new pilot! 😉

    • Scott, Could I pick your brain on this solution? I’m a single senior administrator for a manufacturing org and I’m attempting to update contact data on our quote object using a loop. On the quote object I have Quote Contact Email. This is manually plugged so going forward I’ll be building more functionality to remove these manual procedures. For now I’d like to look up to contact object by email and pull in first/last names, the contact record id and address.

  2. Pingback: More power to your Flows in Summer’14 | Andy in the Cloud

  3. Hi,
    I want to display flows in lead object direct button no need to use visualforce pages,i think it is possible in javascript can you please suggest me…

    Thanks,
    Bhaskar

  4. Hi,
    No need for the VisualForce page… just add a retURL parameter:
    /flow/UpdateLines/?OpportunityId={!Opportunity.Id}&retURL=/{!Opportunity.Id}
    More details here:
    http://sfdcwizards.com/archives/76/autolaunched-flows-part-1/
    Thanks,
    Keith

    • That’s true however, however this reference is not know to the platform and thus the flow can be deleted without addressing such references. Also if you are packaging the custom button this approach does not resolve the namespace.

  5. Is it possible to adapt your VisualForce page so that it can be used with a list custom button? Specifically – to pass the Id of the parent object to a Flow variable?

    I have this so far but the variable’s not being populated.

  6. Hi, just to let you know, I also posted my question here and have the solution now. So I wasn’t sure that you’d still want to take the time to answer my question. But I’m sure it would benefit other users too..

  7. Hi Andy,
    I am creating a new record from flow. At finish, i need to go to the newly created record using flow not the existing record.

  8. Hi there, just a suggestion, instead of using a VF page for the return component, you can also add the return parameter in your URL –>&retURL={!Opportunity.Id}

  9. Pingback: Visual Flow with List View and Related List Buttons | Andy in the Cloud

  10. This was a really useful post, and helped me figure out how to build a Detail Page Button and VF to launch a Flow on a Custom Object. Thanks very much for taking the time to write up your thoughts and share your knowledge, Andy.

    • Your very welcome, there is only one really robust way to wire up Flows to Custom Buttons and this is it! 🙂

  11. Thanks Andy. Do you know if it’s possible to also invoke a Process Builder process from similar VF code? I’m thinking of a solution for something that would use a button triggering a process.
    James

  12. Andy, great post! How would you handle (or ideally prevent) an error from being thrown when someone clicks the Update Lines button and there aren’t any existing Opportunity Lines Items associated with the Opp?

  13. Andy,- I cannot activate my Flow! When I click on the ‘Set as Start Element’ button on the upper right corner of the item, nothing happens. Very frustrating for the learning process …

    • That just sets the start element, it does not activate. You need to save and come out of the editor to the list of flows in your org. There is an Activate link next to the flow.

  14. Hey Andrew,

    Thanks an awesome post. Only one thing missing here is that “alert”/POPUP. Can you please guide me in doing that. I tried multiple options i.e. by adding the alert in script tag as well as onload function but no luck.

    Thanks

    • I am not sure what you mean. Can you elaborate on what your trying to achieve please?

      • I have created a custom button based on vf page and this same VF page is invoking flow. Once flow finishs the job then coming back to the current record location i.e. the place user clicked on button. Now when the user clicks on the button i want popup with text.

  15. In your case, When user clicks on “Update lines” button how to show the alert saying that records updated.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s