logo

NJP

Flow Designer Demo: Automate Knowledge Expiration Warning

Import · May 10, 2022 · article

If you have not worked with Flow Designer, you probably wonder, “what is Flow Designer?” Well, ServiceNow has a great introductory video on their YouTube channel. Still, I’ll provide an excerpt directly from the ServiceNow datasheet, “Flow Designer gives you rich capabilities for automating processes to reduce repetitive tasks, allowing you to focus on high-value work. Use natural language tools to automate approvals, tasks, notifications, and record operations without writing a single line of code. Expand Flow Designer with IntegrationHub to integrate third-party services for more comprehensive workflows and automation across your enterprise.”

In laymen's terms, Flow Designer works similarly to Workflows by enabling you to drive a digital transformation in how your organization performs mundane, redundant tasks via automation in a no-code (or low-code if necessary) platform with a user-friendly, natural language environment. Actions are easy to understand, and visually, you can follow the data flow from start to finish without the guesswork. With San Diego, ServiceNow also introduced Flow Diagramming, which will give you an all too familiar flowchart look to make digestion even easier. @Mark Roethof has an excellent write-up on how to enable this feature.

The Problem

When it comes to managing a knowledge base, a challenge that will without a doubt arise is getting knowledge authors to validate their articles. OOTB, the Author field is hidden, and in some cases, this Author is a member of a Knowledge Management team and not directly the Subject Matter Expert (SME) of the content. Along with this, the SME can change as employees come and go from within the organization. Because of these challenges, maintaining visibility can become clouded, causing articles to be removed from the Service Portal when they have expired.

The Solution

We can create a flow that will identify expiring articles and notify the author and an SME we’ve designated for the content to overcome this problem. In this article, I’ll cover the foundational items needed and the components of our flow to make this successful. I’ve attached an update set containing everything created here, along with a PDF version of this article if you wish to download and save it. Now - let's get started!

---

First: Who’s the SME?

We need to identify who the SME of the article content is for the notification. To accomplish this, add a new reference field to the kb_knowledge table that references the sys_user table.

image

You can extend this further with reference qualifiers or other means to limit the selectable list of users, but we’re keeping it simple for this article. Once on the form, you’ll now see your nice new reference field.

image

I fully understand that SMEs can - and will - change as time goes on. Another good opportunity is a business process that will require an update to the SME field when a user is offboarded. (Hint: you can build that in a flow as well!)

---

Lights...camera...ACTION(s)

With this field added to our table, we can move on to the two actions that will play a crucial role in our flow.

1. Get All Authors with Expiring Articles

Our first action will collect an array of authors who have an expiring article within the next month. Navigate to Flow Designer by going to Process Automation > Flow Designer or typing “flow designer” in your navigator to get started.

image

Once here, click “New” and then click “Action”

image

For action name, enter “Get All Authors with Expiring Articles” and for description, enter a friendly description of what the action is for — the other items can be left as-is unless you know you need to make changes.

image

Now, this particular action does not need to take in any Inputs, so you can skip this section and go to add a new step by clicking the + and scrolling down until you see Script.

image

Now that you have added a Script step to your flow, you will see a screen like this:

image

At the top, replace “Script step” with the same name as the Action “Get All Authors with Expiring Articles.” Then we can skip our inputs for this action. For the script section, enter the following code:

(function execute(inputs, outputs) {
    //Create our GlideRecord Variable
    var knowledge = new GlideRecord('kb_knowledge');

    //Initialize an array for authors
    var authors = [];

    //Add an encoded query to articles valid to on next month and run the query
    knowledge.addEncodedQuery('valid_toONNext month@javascript:gs.beginningOfNextMonth()@javascript:gs.endOfNextMonth()');
    knowledge.query();

    //While loop to continue while there is a new record
    while (knowledge.next()) {

        //Push the knowledge author email to the array
        authors.push(knowledge.author.email);
    }

    //Use ArrayUtil.unique() to make authors unique and remove duplicates
    //This is useful in the event John Doe has 5 articles expiring, we 
    //only want John to be returned once.
    var uniqueAuthors = new ArrayUtil().unique(authors);

    //Push our array to action outputs as a comma split string
    outputs.authors = uniqueAuthors.toString().split(',');
})(inputs, outputs);

I’ve added comments to help explain what is going on, but this will be relatively straightforward for anyone familiar with scripting in ServiceNow. We query for articles with a valid to date on next month, push all authors to an array, and then make the array unique before outputting.

In our Output Variables, create an output with a Label and Name of authors as type Array.String

image

Then go to Action Outputs, repeat the same process above and click “Exit Edit Mode”

image

Now, click the Data Pill Picker and click into your script step and choose the authors string output.

image

image

You can now click Publish and move on to our second action with that done.

---

2. Get Authors Expiring Articles

Repeat the same process as in our first action to create a new action and fill in the information similarly

image

This action will utilize an input passed in for all articles from a Look Up Records action for the same author to pull out the short description, sme, and valid to date. So we create an input called expiringArticles using a Records type for the Knowledge table.

image

image

Script:

(function execute(inputs, outputs) {
    //Get our inputs in a variable
    var allArticles = inputs.expiringArticles;

    //Initialize an array to hold our article data
    var articles = [];

    //While we have more articles, loop over each
    while (allArticles.next()) {

        //Set an article variable to the format number Valid to | short description 
        var article = '<b>Article:</b> ' + allArticles.number + '<br/><b>Valid to:</b> ' + allArticles.valid_to + '<br/><b>Short Description:</b> ' + allArticles.short_description + '<br/><br/>';

        //Push our article to the array
        articles.push(article);
    }

    //Populate our outputs
    outputs.articles = articles.join('<br/>');
    outputs.author = allArticles.author.email;
    outputs.sme = allArticles.u_subject_matter_expert.email
})(inputs, outputs);

Output Variables:

image

Action outputs:

image

image

Note: The astute will have noticed that I used HTML syntax in our string in our article variable. The neat thing is this will be converted when we use our send email action to create some nice styling.

---

Once again, go back to the main screen of Flow Designer, click New > Flow, fill out the Flow properties similar to below, and click submit to be taken into your Flow.

image

image

Components of a flow:

I feel it is imperative to describe the components that go into a Flow so that those new to Flow Designer do not feel lost or forgotten in the terminology. The below definitions come directly from the datasheet I referenced earlier:

  • Flows: A flow is an automated process consisting of a composite set of actions and subflows triggered by an event, resulting in the automation of business logic for an application or process.
  • Subflows: A subflow is a sequence of reusable actions and data inputs that allow it to be started from a flow, subflow, or script. Ex. Iterate on related records; Add a comment; Notify users of record changes.
  • Triggers: A trigger is an activity that once specified, automatically initiates the flow. Ex. Create a record in a specified table or a scheduled job.
  • Actions: An action is a single reusable operation executed by the system. Ex. Make a REST integration to a third party service; Update field value; Request approval; or Log Value.
  • Conditions: A condition is a statement that determines when or how an action runs. Ex. Run an action only if a field is over a certain value.

Note: For this demonstration, we will not be using a Subflow, but one could be created to submit the catalog requests for the SME to validate article content.

The structure of our flow is as follows:

image

Trigger: Monthly on the 1st at 10 am

1. Get All Authors with Expiring Articles

2. For each author, look up their expiring articles with a Look Up Records action

image3. Pass our Records into Get Authors Expiring Articles

image

4. Send email

image

Now that our Flow is built, we’re ready to test. As a validation of what to expect, I will see how many articles expire next month and the respective author and SMEs of those articles. For my testing, we have three articles expiring next month:

image

When we test our Flow and check the execution stats, we see that we did find three results

image

To see what the email looks like, you can click on the Send Email action and scroll down to where you see email in the Output Data to open the Email record by clicking on the sys_id and clicking “Open Record”

image

image

Then click on “Preview Email” under Related Links to see your email preview.

image

image

What happens when there are two articles for the same author with different SMEs?

image

Uh-oh! We found both articles, but only 1 SME is notified!

image

The reason for this is that we are only using the Author as our unique value, and when passed into our second action, we only return a single SME output based on the last expiringArticle passed input. So how do we correct this? We will populate our SMEs into an array for this demonstration, add an additional value for the knowledge article SME in our email, and update our script output to account for multiple SMEs (e.g., an array).

Note: Update u_full_name with the respective sys_user field you want to use. In my PDI I have this field to show as first last - email

(function execute(inputs, outputs) {
    //Get our inputs in a variable
    var allArticles = inputs.expiringArticles;

    //Initialize an array to hold our article data
    var articles = [];
    var smes = [];

    //While we have more articles, loop over each
    while (allArticles.next()) {

        //Set an article variable to the format number Valid to | short description 
        var article = '<b>Article:</b> ' + allArticles.number + '<br/><b>Valid to:</b> ' + allArticles.valid_to + '<br/><b>Short Description:</b> ' + allArticles.short_description + '<br/><b>SME:</b> ' + allArticles.u_subject_matter_expert.u_full_name + '<br/><br/>';

        //Push our article to the array
        articles.push(article);

        smes.push(allArticles.u_subject_matter_expert.email);
    }

    //Populate our outputs
    outputs.articles = articles.join('<br/>');
    outputs.author = allArticles.author.email;
    outputs.smes = smes.join(',');
})(inputs, outputs);

By joining our array with a comma on output, we can directly pass this into our To: field of the Send Email action, and it will be interpreted as separate recipients.

image

And our final email now looks like this:

image

---

Hopefully, this article has helped outline what Flow Designer can do to improve and automate processes within your organization with a (most likely) familiar use case.

Could you only notify the SME rather than the author? Absolutely, but visibility leads to results. Putting all SMEs on the same email can drive collaboration across teams - especially in the event content is referenced in other articles. You can tailor this to your use case however you deem necessary; again, this was to show what Flow Designer can do.

Also, as previously mentioned, this could submit a catalog item wherein the Author and SME have an approval task to complete. The sky is the limit. Look around at what actions are available for a Flow and think holistically about what processes you frequently do - and automate it!

Thanks for reading, have a great day!

--

Liked this article? Hit b ookmark and mark it as helpful - it would mean a lot to me.

See something wrong with this article? Let me know down below!

LinkedIn

View original source

https://www.servicenow.com/community/now-platform-articles/flow-designer-demo-automate-knowledge-expiration-warning/ta-p/2323289