In my last two posts, I covered how to use the SharePoint Get items action in Flow and how to tell if the SharePoint Get items action returned items (by counting them).
I really wanted to provide a real-life sample how one would use the two concepts together in Flow.
Since we just had a national holiday and I completely forgot about it (got ready to go to work and everything), I thought I’d create a sample flow that automatically runs on a schedule and prompts users to do something, except when today’s date is a holiday.
The workflow logic looks a little like this:
st=>start: Start (every n days) e=>end: End op2=>operation: Get today's date op3=>operation: Find statutory holidays with today's date op4=>operation: Today is not a holiday sub1=>subroutine: (Do something) cond=>condition: Is today a holiday? (Did you find any items) io=>operation: Today is a holiday (Do nothing) st->op2->op3->cond cond(yes)->io->e cond(no)->op4->sub1->e
Let’s get started by creating the environment we need for this workflow.
In this example, we’ll create a SharePoint list which will contain an entry for every statutory holiday. We’ll use a list because it allows our HR folks to maintain it without needing a special app. We can also show the list on our SharePoint site so all employees can see what days are statutory holidays.
You can also use your own database, or an API, or even a static Excel spreadsheet if you want, but I wanted to use a SharePoint list to show how to use the SharePoint Get items action in Flow.
To create the list, follow these steps:
If you need to support statutory holidays for multiple states/provinces/countries, feel free to add more columns to your list to support your needs. I wanted to keep this list as simple as possible.
Why didn’t I use a calendar list? I didn’t want to add the extra columns that come with a calendar list. If you really want a calendar view, just add it as a custom view for your list.
Use your list’s Quick edit to enter your statutory holidays. I use this site to get the list of statutory holidays for every year.
When you’re done, you should have a list that looks like this:
Now let’s create a scheduled flow that uses the list!
Your workflow will be created and open in the workflow editor. I renamed the Recurrence action to Every Monday because I always want my workflows to be easy to understand without having to expand every action.
Unfortunately, Flow won’t let you save until you add another action.
Funny, cause that’s exactly what we’ll do next!
Before we can access the Statutory Holidays list in SharePoint, we need to add a connection to SharePoint by following these steps:
From within your flow editor, select +New step at the bottom of the flow.
In the Choose an action prompt, type Get items in the Search connectors and actions. Search is case insensitive.
Select the Get items action with a SharePoint logo from the list of Actions that appears. If the search query returns too many actions and you can’t find the SharePoint Get items, you can filter out all other connectors by clicking on SharePoint just below the search bar.
As soon as you select Get items, the Choose an action box will transform into the Get items box.
If you haven’t created a connection to SharePoint yet, you’ll be prompted to Sign in to create a connection to SharePoint. Click Sign in to sign in with the account that you wish to use to access SharePoint.
The account you use here specifies who will access SharePoint. Make sure that you use an account that can see the site and the list where you want to get items from. It is a good idea to use a service account that isn’t using your own credentials to connect.
Once connected, enter URL to the site that contains your list under Site address. If you experience problems typing or pasting the URL, try selecting Enter a custom value from the drop-down; it will turn the drop-down box into a text box.
If the site URL you entered is valid and the credentials you supplied are correct, you should be able to pick the Statutory Holidays list from the List Name drop down.
If you ran the flow now, it would retrieve every statutory holiday in the list.
We want SharePoint to return only statutory holidays on the days the flow runs. To do this, we’ll add an ODATA filter by following these steps:
Date eq datetime''
.formatDateTime
function, find the utcNow()
function in the Date and time category.utcNow()
but before the last )
, type ', 'yyyy-MM-ddT00:00:00')
and select OK to insert the expression.If everything goes well, your flow is now able to retrieve statutory holidays from the SharePoint list every time your flow runs.
Now let’s add logic to detect whether something was returned or not…
But before we do, let’s rename the Get items action to Retrieve statutory holidays for today’s date to make it easier to read. Hey, my blog, my naming conventions 🙂
As I explained in my previous post, I like using variables to make my flows easier to debug and easier to understand. We’ll store the number of items returned in a variable called Number of statutory holidays.
Since this is the first time we set the variable, we’ll use Initalize variable using the following steps:
In the flow editor, select +New step
From the Choose an action box, type variable in the search box.
From the list of suggested actions, select Initialize variable.
4.An Initialize variable box will replace the Choose an action box. Give your variable a descriptive Name. For example: Number of statutory holidays.
In the Type field, select Integer — because we’ll be storing the number of items returned.
We’ll write the expression to calculate the number of items returned the Value field. If the dynamic content pane doesn’t show, select Add dynamic content, the select Expression.
Look for the length(collection) function in the Collection category and select it to insert it in the expression box. The length function is specifically designed to calculate how long a collection of items is — and that’s what the Get items action returns: a collection of items.
Make sure your cursor is positioned between the two parantheses () in the length
function. Select the Dynamic content tab and look for the value dynamic content for the Retrieve statutory holidays for today action.
Flow will automatically insert body('Retrieve_statutory_holidays_for_today''s_date')?['value']
inside your length()
function. The final expression should be:
length(body('Retrieve_statutory_holidays_for_today''s_date')?['value'])
Select OK to insert the value.
Save and test your flow. Mine returned 1 item:
Now that you have a variable that contains the number of statutory holidays, you can use it anywhere you want.
Let’s create a conditional branch to do something if today is not a statutory holiday:
Save and test your flow. If everything worked well, the Expression value from your condition should return true
if SharePoint found items, and false
if nothing was found. My test returned true
.
That’s it! Now you can insert actions under If no to do something when today isn’t a statutory holiday.
You could even add something under If yes to delay the flow until next day, but that’s another post.
You can use Scheduled flows to run every n days and easily query a SharePoint list containing statutory holidays to skip running when the current date is a statutory holiday.
Note that in today’s sample, I didn’t deal with timezones by setting the start time of my workflow so that it is later than midnight in UTC time. If you run your workflow across multiple timezones, you should keep this into consideration.
I hope this helps you create workflows that know when to take it easy.
Because everyone deserves a vacation once in a while!
Image by Free-Photos from Pixabay