Counter in Flow Designer

alebessa
Giga Contributor

Hello!

I'm looking for a way to do that:

I am creating a new flow, on flow designer, that will check if there are greater than 2 SCTASKs in a hour. If so, will create an incident. 

What is the best way to do that? Now can I put a counter on my flow?

 

Please, can you help me?

 

Thank you!

5 REPLIES 5

Brian Lancaster
Tera Sage

Can you provide us more details on your requirements. Any sctask as you will most likely be creating 100 of incidents? Is this for a specific Catalog Item? Why do you want to create an incident if you have more the 2 catalog tasks created in one hour?

For ex:

I have an automation created before that creates tasks in [sc_task] table for any reason. Now, I would like to create other new automation that will check every time the task table and if there are more than 2 tasks related for a specific short description, then create a new incident. 

So, if 2 tasks was opened with the short description "This is an example" in a hour, then create a new incident.

 

The reason to create this incident is to do a follow up in these tasks. 

thomaskennedy
Tera Guru

The requirement sounds a little odd. I will echo Brian's question about why you are doing this. You should be very clear about the requirement, why it needs to be done, and exactly which tasks are in scope.

You might start with a business rule that responds to the creation of a task matching your particular pattern (use the On Insert trigger), and checks how many such tasks have been created in the last hour. If n or more are found, and no corresponding incident has already been created in that same time frame (the incident would have to have some identifiable marker on it) the rule sends a mail notification to someone or other. If this goes sideways, it won't mess up your incident queue.

If it works as expected, drop the email notification and rewrite the rule as a flow and have it create an incident instead (or just continue with the business rule). I don't see a big advantage either way, except the flow designer is more intuitive for most users.

Amit Gujarathi
Giga Sage
Giga Sage

Hi @alebessa ,
I trust you are doing fine.

To achieve this, we can use a combination of Flow Designer and ServiceNow APIs. Here's how you can do it:

  1. Create a new Flow in Flow Designer and give it a name (e.g., "Incident Creation Flow").

  2. Add a "Scheduled trigger" to the flow and set it to run every hour.

  3. Next, add a "Script" action to the flow and enter the following script:

 

// Set the query parameters
var queryParams = {
   sysparm_query: 'opened_atONLast hour^active=true',
   sysparm_fields: 'number'
};

// Call the ServiceNow API to get the count of SCTASKs created in the last hour
var response = gs.httpClient.get('https://<your_instance_name>.service-now.com/api/now/table/sc_task', queryParams);
var count = JSON.parse(response.getBody()).result.length;

// Check if the count is greater than 2
if (count > 2) {
   // If the count is greater than 2, create a new incident
   var incident = new GlideRecord('incident');
   incident.short_description = 'Multiple SCTASKs created in last hour';
   incident.insert();
}

 

  • This script does the following:

    • Sets the query parameters to get the count of SCTASKs created in the last hour.
    • Calls the ServiceNow API to get the count of SCTASKs.
    • Checks if the count is greater than 2.
    • If the count is greater than 2, creates a new incident with a short description.
    1. Save the flow and activate it.

    2. Test the flow by manually triggering it or waiting for it to be triggered automatically by the scheduled trigger.


Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi