Roll Up Summary Field Using Headless Flow

In my last post, I provided an example of marrying Flows with Apex . This post we’re looking at a different marriage, between Workflow Rules and Flow, or “Headless Flows.”

Headless Flows lets you start a Flow from a Workflow Rule. This basically turns Flows into something similar to an APEX Trigger – except there’s no code. Not only is there no code, but there’s no Unit Tests to write (I’m of mixed minds on this one).

Headless flows was released as a pilot in Spring’14. I was lucky enough to get in the pilot. While I waited for the Spring’14 release, I was racking my brain with how I could use this potentially powerful process.

One of the many things I end up writing triggers for are Roll up Summary Field behavior on objects that will not support Roll Up Summary Fields.

When To Use It

  • You have two related objects that cannot be in Master/Detail relationship
  • You have Standard Objects that do not support Roll Up Summary Fields
  • You’ve run out of Roll Up Summary Fields, you can’t reduce them, and salesforce.com won’t increase your limit

There are three things you need to complete to use Headless Flows:

  1. A Flow
  2. A Flow Trigger
  3. A Workflow Rule

The Flow

Simple Flow doing a roll up summary function

Simple Flow doing a roll up summary function

The Flow is fairly simple. It’s designed to work from an Opportunity Team Member or from an Opportunity. The starting element is a decision. All the decision element does is check to see if the TeamMemberID variable is blank.

The Left Path:

If it’s not blank, I assume the flow is being called from an Opportunity Team Member. The next element does a record lookup of the Opportunity Team and gets the Opportunity ID.

The Other Path:

Once an Opportunity ID is available, we go to the Fast Lookup. This looks up all the Opportunity Team Members associated to that Opportunity ID. The Flow saves this list in a new variable type “SObject Collection Variables” which is similar to a List in Apex Code.

The next element is also a new type. This is a loop. The Loop will go through a Sobject Collection and pass each record in that collection to the Next Element in the flow. Once the Loop is completed it goes to the element followed in the “End of Loop”

During our loop, I’m passing it to an assignment Element. All this is doing is adding 1 to a variable, “TeamMemberCount,” that starts at 0. Once the Loop is complete it goes to the next element which updates the Opportunity field with our TeamMemberCount.

That’s it. A 6 element Flow. I created 1 flow that would support if it was triggered from an Opportunity or Opportunity Team Member. We could make this a 4 element flow, but that would require to create 2 different flows – one for the Opportunity and one for the Opportunity Team.

The Flow Trigger

A Flow Trigger on an Opportunity Team Member

A Flow Trigger on an Opportunity Team Member

Before we can run the flow with Workflow, we first need to assign it to a Flow Trigger. Think of a Flow Trigger just like a Field Update or an Email Alert. The Flow Trigger is the action that the Workflow Rule will take. In this situation, the action just happens to call a Flow.

A Flow Trigger is pretty simple. You give it a name, the Object it’ll “trigger” against and then you can optionally assign Flow Parameters. In my example of the Flow Trigger against Opportunity Team, you can see I’m passing the ID of the Opportunity Team Member into “TeamMemberID” a Flow variable.

What’s nice is there’s an option to set Admins to run the latest flow. This lets you create new versions of the flow that aren’t activated yet and test them through the Workflow rule.

Also note, that I failed to do a best practice and add a description to this Flow Trigger. I obviously did this on purpose and will use this opportunity to remind you that “The Description Field Is My Friend.”

The Workflow Rule

At last! We have arrived at the Workflow Rule. You’re probably familiar with Workflow rules. Using them with Flow works just like any other Workflow Rule. We need to specify a Rule Criteria, and Evaluation Criteria, and then set Actions.

Flow Triggers are the actions that we’ll reference in our Workflow Rule. Currently, you can only use Flow Triggers as immediate actions. Timebased Workflow actions do not support Flows.  In my example I created two Workflow Rules, one for each Flow Trigger. One Workflow Rule is for the Opportunity and the second on Opportunity Team Member. I set up my Workflow to run anytime an Opportunity or Opportunity Team Member is saved.

Workflow Rule for Opportunity Team

Workflow Rule for Opportunity Team

We activate the Workflow rule and there we go! Now when I add or edit an Opportunity Team Member, my Opportunity is updated with the number of Team Members associated with it. When I edit my Opportunity, the number gets rechecked.

The Shortfalls

The Roll Up Summary Flow works, but there’s one glaring gap in the process. Workflow Rules do not run when a record is deleted. This means that if a child record is deleted, our flow isn’t called and our Quantity field isn’t updated!

That’s the reason for the second Flow Trigger and Workflow rule on the Opportunity. It’s an extra catch to “true up” Opportunity Team count in case a member is deleted. I’m counting and hoping that the Opportunity gets modified shortly after. This isn’t a perfect. APEX would let us manage Opportunity Team Member deletion, but that requires code. This covers the majority of our needs, no code required!

General Considerations

  • Flow Trigger can set the values of up to 25 variables and sObject variables
  • Flow Triggers can’t use Multi-select picklist field to set variables
  • Flow Trigger only sets the amount of currency when assigning a flow variable. It doesn’t include the ISo Code. This is an issue if you’re using multi-currency
  • You cannot have a screen in a flow you’re going to use in a Flow Trigger
  • Flow Triggers aren’t available as time-dependent workflow actions
  • When multiple flows are triggered, the flows aren’t run in any particular order
  • Flow Triggers execute after all workflow field updates – including apex triggers and standard validations
  • Flow Triggers aren’t available in Change Sets
  • Flow Triggers aren’t packageable
  • Flow Triggers require the use of Workflow Rules
  • Flow Triggers run in “system context.” Normally flows run by User Context which includes user permissions, FLS, and Sharing Rules. These are ignored with Flow Triggers

A Note From The Editor

A little secret about WizardNews posts. I hate writing Post Titles. I struggle with trying to write titles that are informative, accurate, and grab your attention. One of the more difficult parts is not to interject my sense of humor into the title – too much. For example, alternative titles for today’s post included:

  •  Watch Your Pumpkins – Headless Flows Are Here!
  • SUM ALL THE THINGS!! – With Flow!
  • Flows Without a Head Run Deep
  • You Won’t Believe What This Admin Did With a Flow
  • 6 Reasons Why Buzz Feed Titles are Inaccurate
  • “NYNA NYNA NYNA NYNA” I got in the Flow Pilot and you Didn’t
  • Look at This Cool Thing You’ll Do As Soon As It Is Generally Available

They progressively got worse from here. I want to hear from you my loyal readers!

First, what other titles can you come up with for this week’s blog post?

Second, would you like to see at the end of each post alternative titles I considered for that week’s posts?

Let me know in the comments!

Recommended Posts
Showing 12 comments
  • Reply

    This is really powerful stuff! Can’t wait to start building one of these myself. Love the alternate titles, i think it would be fun to see all the different ideas you come up with while agonizing over the perfect fit.

    my submission for this article would be Custom Roll Up Summary Fields without Code – Because Flows!

  • SutoCom
    Reply

    Reblogged this on SutoCom Solutions.

  • Deepa
    Reply

    Nice blog, but to update quantity field when deletion of child object record you have to use apex trigger without this you can’t make it perfect.

    • Salesforce Wizard
      Reply

      That’s absolutely right Deepa. I mentioned the delete gap in the Short Fall section. I’m somewhat covering myself by also having the count update when the Parent Record is edited, but it’s no way perfect.

      But it’s a close approximation when you can’t do APEX, run our of summary fields or want that functionality on non-Master/Detail relationships. Of course, you could also go out and buy Roll up Helper, but not everyone has the budget.

      A nice note in the Summer’14 release notes is you’ll be able to call a Flow from APEX. This means I could do everything in Workflow Rul and cover the delete gap with a Delete Trigger that calls the same flow trigger as my Workflow Rule.

      • Andres Perez
        Reply

        Not just deletes, but also need to consider undeletes. Nice to hear that Headless Flows will be callable form triggers. But you again need to have code coverage for your one line triggers 😉

  • meighanrockssf
    Reply

    Hey, are you checking the OppID on the OppTeamMember in step one, or are you checking the OpportunityID on the Opp itself? Wouldn’t you want to have the flow based on the OppTeamMember when it is created so that it fires the flow to count, or another question, when creating the team meber since it is an MD relationship, don’t you need the Opp ID to save, so it wouldn’t be null?

  • meighanrockssf
    Reply

    Totally figured that one out wow, sorry.

    • Salesforce Wizard
      Reply

      Kudos for figuring out! Sometimes you have to ask the question before you have enlightenment… Or something profound like that

  • Roger
    Reply

    Override standard delete w/vf page, initiating a flow to delete record and re-run rollup

pingbacks / trackbacks

Leave a Reply