How to setup a recurring scheduled Process Builder and/or Flow

***UPDATE***

My recommendation is to use this solution by Doug instead of following the steps below: https://douglascayers.com/2016/04/18/salesforce-process-builder-scheduler/


 

In Apex you’re able to setup a recurring job that will fire whenever you’d like.  However, now that many of the solutions that we previously needed Apex for can be solved through Process Builder and Flow, it would be great if we could setup a schedule to do batch jobs without Apex.  I’ve recently come across more and more clients wanting something to run from every 15 minutes to every business day.  Unfortunately, I’m not savvy enough with Apex (yet!) to feel comfortable writing a trigger just to get this added functionality.  So, that is when I got creative.  With just three workflow rules we are able to setup our own scheduled Flow!  (So that means, we’re not actually going to be building a Flow in this post, sorry!)

How might you use something like this?  Well, you could query Leads and send out emails to groups of them every morning at 8am, or you could recalculate some roll-up fields that you have.  Really, you can do anything that you want!

Things to keep in mind:

  • Time-based Workflows fire every 15 minutes in bulk from Salesforce
  • A Custom Object for your Scheduling is not Required (but it makes it easier)
  • To customize your ‘time’ for the next scheduled process, get comfortable with math!  If you want to do less than a day, you just need to modify the formula to add in the % of a day that it is.

Now that we have an idea of what our end goal is, lets get started!

Create a Custom ‘Scheduling’ Object and Three Custom Fields.

  1. Fire Flow [Checkbox]
  2. Scheduled Date/Time [Date/Time]
  3. Next Scheduled Date/time [Date/Time]

When you’re done, it should look like this:

Scheduled Flow Object

Notice, in this example we are not adding in record types, picklists, or anything to differentiate between our scheduled jobs.  We are just going over the basics of this process.

Now, lets get to the workflow rules that will make the magic happen!

Workflow Rule #1:

The purpose of this Workflow is to fire our Flow.  We will do a Time-Dependent Workflow Action based off of the Scheduled Date/Time field.  So, whenever that time passes, we want to immediately push this Workflow into the queue to fire off.  In addition to this, we’re going to also want to reset our Scheduled Date/Time field to be null.

So, lets take that concept and put it into a Workflow Rule!

Navigate to create a new Workflow Rule, and select the target Object (in this case Scheduled Flow)

New Workflow #1

Set our Rule Criteria to be whenever the Scheduled Date/Time is not equal to NULL (leave it blank), and when the Fire Flow checkbox is marked as FALSE.

New Workflow #2

Lets now go down to our Time-Dependent Workflow Rules and add a Time Trigger.  We want to set it to be 0 Hours or 0 Days from the Scheduled Date/Time.

New Workflow #2.5Now, we need to add in two Field Updates.  The first will be marking our Fire Flow checkbox as TRUE.

New Workflow #3

The second is to make our Scheduled Date/Time null.

New Workflow #4

Here is what our finalized Workflow Rule should look like.  If yours looks like this, select Done and Activate your Workflow Rule!

New Workflow #5

Workflow Rule #2:

The purpose of this Workflow is to be set our Scheduled Date/Time value to our next value.  This way we have it queued up to fire again once the next scheduled date/time happens.

So, lets take that concept and put it into a Workflow Rule! (again)

Navigate to create a new Workflow Rule, and select the target Object (in this case Scheduled Flow).

New Workflow #1

Set our Rule Criteria to be whenever the Fire Flow checkbox is marked as TRUE.

New Workflow #6

Now we want to create another Field Update under our Immediate Workflow Actions.  This Field Update is going to be on our Scheduled Date/Time field, and we are going to update it with a ‘formula’ which is essentially just the Next Scheduled Date/Time field.  Make sure that you also check the Re-evaluate Workflow Rule after Field Change.

New Workflow #7

When you are finished, click Save.  Your Workflow Rule should now look like this:

New Workflow #8

Great job, now press Done and Activate this Workflow Rule as well.

Workflow Rule #3:

This one is where it can get confusing, so stay with me!  Based on our first Workflow Rule, we are setting the Scheduled Date/Time field to blank — and in our second Workflow Rule we then set the Scheduled Date/Time to be the value in our Next Scheduled Date/Time field.  So, when that series happens we want to then update our Next Scheduled Date/Time with its next value.  This way we are able to stop our two Date/Time fields from looping into an abyss of updates.  You still with me?!

So, lets take that concept and put it into a Workflow Rule!

Navigate to create a new Workflow Rule, and select the target Object (in this case Scheduled Flow)

New Workflow #1

For this, we want to evaluate our criteria when it is created, and every time it’s edited.  Now, we’re taking off the training wheels and going with a formula for our Criteria!  In this formula we want to only fire this when the Scheduled Date/Time is NOT BLANK and it WAS CHANGED.

New Workflow #9

The Rule Criteria:

ISCHANGED( Scheduled_Date_Time__c ) && NOT(ISBLANK(Scheduled_Date_Time__c))

Next is to create our Field Update.  This is where it can look more complicated than it really is.  In this field update we are going to add one business day to the value we just put in our Scheduled Date/Time field in our second Workflow Rule.

New Workflow #10

The Formula for our Date/Time update:

CASE( MOD( DATEVALUE( Scheduled_Date_Time__c ) – DATE( 1900, 1, 7 ), 7 ),
0, Scheduled_Date_Time__c + 1 + 1,
1, Scheduled_Date_Time__c + 0 + 1,
2, Scheduled_Date_Time__c + 0 + 1,
3, Scheduled_Date_Time__c + 0 + 1,
4, Scheduled_Date_Time__c + 0 + 1,
5, Scheduled_Date_Time__c + 2 + 1,
6, Scheduled_Date_Time__c + 2 + 1,
Scheduled_Date_Time__c + 1
)

Notice, in this Formula we adding two numbers to the Scheduled Date/Time.  If you look closely you’ll see that the left number changes, while the right one stays at 1.  On the right side, this is us simply always adding 1 Business Day.

New Workflow #14

However, when it falls on a ‘Weekend’ value we need to add more days.  So that is what we are doing on the left side.  For instance, 0 = Sunday, so we need to add just one day to get  us to Monday.  If this fires on Friday (5) or Saturday (6) we need to add 2 full Business Days in addition to our 1 Business Day to get to the correct value.  Granted, in this situation we really shouldn’t ever have the Saturday or Sunday event happen, its still best to build as if it might!

New Workflow #13

And lastly, If you wanted to switch this from being just 1 Business Day to being every 15 minutes you would do some math……. and come up with 0.0104166666667 as your value to replace the 1 with.

If yours looks like this, select Done and Activate your Workflow Rule!

New Workflow #8

IMPORTANT:  We have successfully built out our three Workflow Rules, but you might have noticed there is one thing missing.  We didn’t reset the Fire Flow checkbox to FALSE.  I like to play things safe and reset the value to FALSE in my Flow or Process Builder.  This way I know that my Flow or Process Builder fired correctly.

Lets go create our record and set our first Date/Time values that will get us started!

Final Record

Congratulations, you’re done!  With just three Workflow Rules we were able to build our own workflow scheduler!  Tip, if you start to hit some limits (like needing to deal with thousands of records) you can always create additional records on the Scheduled Flow and split your automation into pieces.

8 thoughts on “How to setup a recurring scheduled Process Builder and/or Flow

  1. Andrew Mondy April 7, 2016 / 1:24 am

    This was very helpful–I’m trying to do this exact thing without diving into Apex, and your post got me started in the right direction.

    But I am only using one Workflow Rule, and using a formula field for the Next Scheduled Date/Time and doing the calculations there in the formula.

    My Workflow Rule sets the checkbox to True, and Sets the Scheduled Date/Time to the value of the Next Scheduled Date/Time formula field…using one Rule with two Time-based Actions.

    Then the Process Builder or Flow is where I uncheck the Fire Flow box, which causes the Workflow Rule to be triggered again to run the next time.

    This may be simpler to understand and setup. Thanks!

    Like

    • David Litton April 7, 2016 / 3:46 pm

      Andrew, thanks for the comment! Yes, that does make sense and is a great idea to consolidate it to an easier format. I’ll keep that in mind if I have to use this scenario again, and hopefully that will help anyone who sees this post! If I get some time, I might update the post to go with the one workflow option you mentioned, if you don’t mind 🙂

      Like

  2. Jason Orbison June 20, 2016 / 7:05 pm

    There is another way that is much more simple. Create a visual workflow that has a wait element for whatever time you want. For Example, I have an email alert that is triggered by a change to a roll up summary field. After the email alert, there is a wait element for 1 week and then set up the email alert again.

    This is essentially a loop (Not a visual workflow loop). It is a good idea to put some decision elements as this will run forever and could cause errors if the email is no longer good or the parameters for the email alert that originally triggered the even are no longer valid.

    It does work and does not take nearly as much time as an apex class that calls the flow interview or email alert.

    Like

    • David Litton June 22, 2016 / 2:52 pm

      Jason, that is a great solution to use the Wait Element. Thanks for sharing the idea! Hopefully your comment will help someone with a problem they’re facing. It is always cool to hear how many different ways you can solve an issue in Salesforce! I’ll have to give your solution a try soon

      Like

    • John November 22, 2016 / 3:46 pm

      Hi Jason,
      I was hoping I could get more information on what you have proposed here:
      I’m hoping to get guidance on the best way to implement a process builder/visual workflow combo somehow that will be able to send out recurring emails until criteria is met to shut the recurrence off.

      We are basing the recurring emails off of a case record status field and the email recurrence frequency will be based on what case status the record currently sits at.

      If status = open, then send an email reminder every 14 days
      If status = monitoring, then send an email reminder every 30 days
      If status = long term monitoring, then send an email reminder every 60 days

      When the case sits at any of the above status values, the emails should continue to reoccur/recycle UNTIL a new case comment is logged against the case record (we are updating a checkbox & date on the case object for when a new case comment is added and we have formulas on the case object evaluating if the new case comment is within the last 14 days, last 30 days and last 60 days). When a new case comment is logged against the case record, it should start the “clock” over again, based on the frequency determined by the case status (listed above).

      The recurring emails based on status will only stop all together when the case reaches a status of closed.

      I have so far created a process builder that evaluates if the case record is in any of the 3 status values (open, monitoring, long term monitoring) and if it is, trigger a visual workflow.

      The visual workflow kicks off to a wait element with each of the 3 frequencies (14 days, 30 days, 60 days) – each wait element event branching off to an individual send email alert element. Within each wait element event, I have a “waiting condition” that checks to see if a new comment has been added within the number of days that pertain that that wait element event (ex. new comment within 14 days, true or false). I then have the email alert loop back to the wait element at the beginning of the flow.

      I don’t think I’m doing it correctly because I don’t believe I’m triggering the “resetting” of the wait & subsequent email to happen in a recurring cycle. I’m also not sure if it’s working as how I’m intending; when the criteria is no longer met for that wait condition within that wait element event, it should come out of it and potentially be queued up for another wait element event (ex. If the status currently sits at “Open” and an email is queued up to go out every 14 days, but the status changes to “Monitoring”, it should then switch to the email queued up to go ever 30 days instead).

      Hopefully this doesn’t come across as too complicated (wall of text) and I hope to gain insight on your suggestion! Thank you for any help!!

      Like

  3. JD February 9, 2017 / 2:35 pm

    First, thank you for this. My question, what do I change in your formula so the update happens every day? Would I just change the 2 to 0?
    CASE( MOD( DATEVALUE( Scheduled_Date_Time__c ) – DATE( 1900, 1, 7 ), 7 ),
    0, Scheduled_Date_Time__c + 1 + 1,
    1, Scheduled_Date_Time__c + 0 + 1,
    2, Scheduled_Date_Time__c + 0 + 1,
    3, Scheduled_Date_Time__c + 0 + 1,
    4, Scheduled_Date_Time__c + 0 + 1,
    5, Scheduled_Date_Time__c + 2 + 1,
    6, Scheduled_Date_Time__c + 2 + 1,
    Scheduled_Date_Time__c + 1
    )

    Like

    • David Litton February 9, 2017 / 2:37 pm

      Yes, it would look like this:

      CASE( MOD( DATEVALUE( Scheduled_Date_Time__c ) – DATE( 1900, 1, 7 ), 7 ),
      0, Scheduled_Date_Time__c + 1,
      1, Scheduled_Date_Time__c + 1,
      2, Scheduled_Date_Time__c + 1,
      3, Scheduled_Date_Time__c + 1,
      4, Scheduled_Date_Time__c + 1,
      5, Scheduled_Date_Time__c + 1,
      6, Scheduled_Date_Time__c + 1,
      Scheduled_Date_Time__c + 1
      )

      Technically, you could just do:
      Scheduled_Date_Time__c + 1
      Since you’re not making it dynamic by day of week.

      Like

  4. jatin bagri September 4, 2017 / 1:19 pm

    Thanks alot!!

    Like

Leave a comment