How To: Set future dates excluding weekends

Updated 08/09/2022

An everyday use case for automation in Salesforce, whether through Workflow, Process Builder, or Flow Builder, is setting a future date. A lot of office-based businesses don’t work weekends. You may want to send out a reminder or set a deadline for paperwork to be signed. If you just put a rule saying two days in the future or forty-eight hours, this could very well land on the weekend.

Scenario

When booking a job for a customer, we need the customer to sign an agreement for the work within forty-eight hours of the Booking being made to reserve the slot. Your company only works Monday to Friday, so you need the expiration date to fall on a weekday and to consider which day of the weekend it would land on. If the expiration date lands on Saturday, we need to push this to Monday. If it lands on Sunday, we must push this to Tuesday. When it isn’t agreed upon in time, the Booking isn’t held and will be updated canceled status.

Solution

To accommodate this we will create a custom object called Booking, a before-save Flow that sets the expiry date, and a second flow that updates the status to canceled on this record.

Steps To Achieve This

Custom Object

Create a new object called Booking. On this, we will create three fields; the first is Expiry Date, a date type field. The second is a checkbox called confirmed. The third is a Status that will be a picklist with two values of Cancelled and Reserved.

Before-Save Flow

Create a flow that uses the Booking object as the base object. Please set it to run when it is created and leave it as fast field updates. Create a formula in the flow by selecting resource and formula. Put the following in as a formula and set the type to DateTime. Call the Formula ExpiryDate.

IF(WEEKDAY({!$Flow.CurrentDate} + 2) = 5,{!$Flow.CurrentDateTime} + 4,
IF(WEEKDAY({!$Flow.CurrentDate} + 2) = 6,{!$Flow.CurrentDateTime} + 3,
{!$Flow.CurrentDateTime} + 2))

What we are doing in this formula is querying what day of the week it is using WEEKDAY expression. This returns a number from 0 to 6 with 0 being Monday and 6 being Sunday. We are using the built-in variables of the flow indicated by $Flow. If the current day plus two equals Saturday then add four days on from the CurrentDateTime and add three days on from the CurrentDateTime if Sunday.

Create an update record element and set the Expiry Date to be populated by the formula ExpiryDate. See the screenshot below for reference. Save the flow as Expiry Date Setter and activate.

screenshot for reference on setting on the expiry date setter.

Time-Based Flow

Create a flow that uses the Booking object as the base object. Set the criteria to updated or created and set the criteria to Expiry Date to Is Null false using {!$GlobalConstant.False}. Add a scheduled path by clicking on the start element and clicking the scheduled path link. Name the label Expiry Date Trigger and set the Time Source to Expiry Date. Now set the offset to 0 and set the offset option to minutes after. What we are doing with this is telling the time to be triggered when the current time matches the Expiry Date.

Screenshot of Configure Scheduled Paths to match example.

Create an update record element and set the value of Status to Canceled. Connect the path Expiry Date Trigger and save the flow as Booking Cancelled. Activate this.

Example of update records on time based flow path.

Testing

As you will be testing something from the future you will want to change the formula. Use {!$Flow.CurrentDateTime} as the value in the before save instead of the ExpiryDate formula. Create a booking record. Once that is successful, change the version back to the original and create three records one on Thursday another on Friday which will get future dates populating, and one on another day of the week. These should populate the Expiry Dates as a Monday, Tuesday, and then two days after the third one.

Expanding On This

You could the same method on the Service Appointments object. You would need to add the fields to Service Appointments and build new flows for this. In the time-based flow, you could add an email alert sent to the customer. This will inform them that their slot has now been canceled which may help prompt them. You could also create a task for the booking team to chase the customer on this.

Further Learning

If you are just starting out with flows I would recommend checking out more of my tutorials. Salesforce also has a fantastic learning tool called Trailhead which covers everything from business philosophy to Apex Code and REST API usage.

Published 08/09/2022
Category: Beginner Friendly, One Page Project, Tutorials
Tags: ,

Other Posts