I just wanted to share a best practice that we implemented for monitoring the web requests in our workflows.
What was my problem?
We wanted to get an email if some API calls to external applications are failing. The monitoring of the API is really not that great in Freshservice. Even in Freshdesk you get an email when an API call fails for whatever reason. It gets even more difficult if you have more than a few API calls in a single workflow path. This solution should work in nearly every module in freshservice.
Why not use the condition node?
You can use the condition node to check if an API call fails or not. Just like this:
But then you have to branch after every API call to send an email to your admins. If multiple API calls do not depend on each other you want the rest of the workflow to execute. That's why you would have to duplicate the other nodes as well.
What is my solution?
I could solve this by using expression builders and scheduled workflows. Instead of branching every workflow path after using a web request I implemented an expression after every workflow that stores the response code from the web request without having to branch them or add any duplicate nodes. At the very end of the workflow path you simply write the result into a custom logging field that is hidden from the users.
Workflow path with a web request, the expression builder and an action node at the end:
I'm not showing the web request as this can be any web request.
Expression builder:
if({{W2.status_code}} >=200 && {{W2.status_code}}<=299, concat("WF101 W2 / WF-Success / Date ", "{{current_date_and_time_iso | date: '%Y-%m-%d %H:%M'}}"),concat("WF101 W2 / WF-Error / Date ", "{{current_date_and_time_iso | date: '%Y-%m-%d %H:%M'}}"))
In short using words:
If the web request is succesful with status code 200-299 the expression contains the following:
"Name of the workflow + executed web request node / Workflow success / Date"
Else (every other response code) means the workflow had issues running:
"Name of the workflow + executed web request node / Workflow failed / Date"
The condition node then stores this information in a custom field that automatically extends by using the current value of the logging field and add the new result of the expression. You don't have to branch the workflow path or add any email notification in here.
How do we get notified about the error?
We implemented a scheduled workflow to check the logging field. Sorry the following screenshots are partly in german. Hope you can follow.
1: Check if the logging field contains the word "error"
2: Check if an email already has been sent to the admins (this string “Benachrichtigung” is set in step 3 down below)
3: Send an email to the admin and add another string at the end of the logging field (that you check in step 2). This is to prevent duplicate emails for the same error.
Step 2+3 prevents the initial email from being triggerd twice. But what happens if another web requests runs at a later point of time and fails? You’d not get a notification for it as of now.
Example:
At workflow execution:
WF-Error 1
Notification of admin via scheduled workflow
2 days later:
Another WF-Error that is written to the log
Only using step 1-3 the email for the second error is not being sent to the admin. In theory we have to check if after notification to the admin is being sent there is another error.
We need two expressions to check this:
4. Length of the entire string in the logging field
5. The position where "WF-Error" last occurs within the string
6. The position where "Benachrichtigung" last occurs within the string
If the position of WF-Error in the string is greater than “Benachrichtigung” then there had been a second error.
In detail:
4: Getting the length of the string as number (so we can start checking the string from the last character)
length("{{change.admin_logs}}")
5: Check the boolean if "WF-Error" or "Benachrichtigung" is the last string found. If “WF-Error” then continue with “true”.
if(lastIndexOf("{{change.admin_logs}}","Error",{{E1.result}})>lastIndexOf("{{change.admin_logs}}","Benachrichtigung",{{E1.result}}),true,false)
6: If this expression returns true, just send an email and add "Benachrichtigung" again to the string. Just like in step 3 of the workflow.
We duplicated this scheduled workflow to get an alert twice a day if an error occurs within a change or ticket.
If you have any questions, feedback or suggestions for improvement, happy to hear about it. Could you solve this need of alerting in any other way?