Assessment Metric Query
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-25-2024 01:22 AM
Hi All,
I have the below requirement:
If there were any failed changes(we can take the Resolution code/close code as unsuccessful) logged for the Primary CI(Configuration Item) in the past 6months in change requests then default the metric should be 'Yes' otherwise 'No'.
How to achieve this through Script in the Metric.
Please assist.
Thanks in advance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-25-2024 04:39 AM
Hi @Joshuu
I am not a coder, so tough for me to debug this.
@Jaspal Singh @Anurag Tripathi @SanjivMeher
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.
Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]
****************************************************************************************************************
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-25-2024 04:42 AM
Quick observation
Line 22 should be ci.addEncodedQuery and it should not have any spaces in the query you pass
Make this change and lets see what you get then.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-25-2024 06:39 AM
Hi @Anurag Tripathi ,
Thank you for the response.
I have updated the code as below and observed below points.
var sixMonthsAgo = gs.daysAgoStart(180); // Get the date 6 months ago
var ci = new GlideRecord('change_request');
ci.addQuery('cmdb_ci', ci.cmdb_ci); // Filter for the current CI
ci.addQuery('close_code', 'unsuccessful'); // Filter for unsuccessful changes
ci.addEncodedQuery('sys_updated_on', '>=', sixMonthsAgo); // Filter for changes in the past 6 months
ci.query();
if (ci.next()) {
actual_result = 300;
string_result = 'Yes';
scaled_result = 300;
gs.info('review Found! string_result=' + string_result);
gs.info('review Found! scaled_result=' + scaled_result);
} else {
actual_result = 150;
string_result = 'No';
scaled_result = 150;
}
1) The else part is not working. It is always showing as 300 in the risk assessment form as below.
2) When I select 'Number' as data type the risk is getting calculated but the string value is not getting populated as yes or no.
3) When I select 'String' as data type the risk is not at all getting calculated through script but the yes or no is getting populated on the risk assessment form.
So, my requirement is If there were any failed/unsuccessful changes logged for the Primary CI(Configuration Item) in the past 6months in change requests then default the metric should be 'Yes' otherwise 'No'. If yes was populated on the risk assessment form then the value should be 300 for no the value should be 150.
Please correct me if I am doing anything wrong here/the script is wrong.
Thanks & Regards.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-25-2024 01:32 AM
Here are the steps to achieve this through a script in the Metric:
1. Create a new Metric Definition:
- Navigate to System Definition > Metrics > Definitions.
- Click on New to create a new Metric Definition.
- Fill in the necessary fields such as Name, Table (change_request), Collection type (Script), and Script.
2. In the Script field, write a script to check for failed changes for the Primary CI in the past 6 months. Here is a sample script:
javascript
(function executeRule(current, previous /*null when async*/) {
var metricValue = 'No';
var sixMonthsAgo = gs.daysAgoStart(180); // Get the date 6 months ago
// Create a GlideRecord to query the change_request table
var gr = new GlideRecord('change_request');
gr.addQuery('cmdb_ci', current.cmdb_ci); // Filter for the current CI
gr.addQuery('close_code', 'unsuccessful'); // Filter for unsuccessful changes
gr.addQuery('sys_updated_on', '>=', sixMonthsAgo); // Filter for changes in the past 6 months
gr.query();
// If there are any records, set the metric value to 'Yes'
if (gr.next()) {
metricValue = 'Yes';
}
// Return the metric value
return metricValue;
})(current, previous);
3. Save the Metric Definition.
4. Now, whenever a change request is updated, this metric will be calculated and stored. You can view the metric data by navigating to System Definition > Metrics > Metric Instances.
Please note that this script is a basic example and may need to be adjusted to fit your exact requirements and ServiceNow setup.
For ServiceNow Live Classes, Books, Sample Resumes, Interview Questions, CSA Quizzes.
And getting better services's on ServiceNow you can visits our website.
Please visit : https://nowkb.com/home
Our Website :https://nowkb.com/home
nowKB.com