- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-01-2024 09:15 AM
I have a Catalog Item (Firewall Rule Change) that generates a Change Request after the RITM is approved. The RITM is set to Closed Complete after the Approval activity completes.
There is a new requirement to capture the Duration of when the Request is opened (Waiting for Approval) to when it gets approved (Complete).
I created a new Metric Definition that runs on the Requested Item (sc_req_item) table. When I set the Type as "Field value duration", it sets the duration correctly. It only calculates the difference between created on and closed, which is fine because that essentially is what I need.
However, I only want the Metric to capture records if the Catalog Item = Firewall Rule Change, so I have to set the Type to “Script calculation” and use the Script. This is where I’m struggling.
The stage transitions through waiting for approval to fulfillment, to complete. I would like to get the Duration of when the RITM is created (Stage = waiting_for_approval) to when it is completed (Stage = complete). My script is not working. Any help appreciated.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2024 07:52 AM - edited 03-02-2024 07:53 AM
Hi Cory,
I would do something like this
// definiton: GlideRecord - this row
// current: current record
if (current.sc_cat_item == 'firewall') {
if (current.stage== 'requested')
createMd();
else if(current.stage== 'approved')
closeMd();
}
//calls the script include "MetricInstance" and create a metric instance
function createMd() {
var md = new MetricInstance(definition, current);
// check if a metric instance exists already
if (md.metricExists()) {
return;
}
var gr = md.startDuration();
}
//calls the script include "MetricInstance" and close the metric instance
function closeMd() {
var md = new MetricInstance(definition, current);
if (md.metricExists()) {
md.endDuration();
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-01-2024 10:30 AM
Hi @Mike Cory ,
Assuming you have problems in date difference calculation.
The sys_created_on field is of data type datetime and gs.now returns only date i.e. 2024-03-01 , so before doing the calculation you must convert either start date to GlideDate format or end to GlideDateTime.
Instead of gs.now you can use gs.nowDateTime() , after making both dates to same data type, you can try the datediff function if that does not works you need to make use of getnumericvalue and setnumericvalue functions as below:
nDate1 = date1.getNumericValue();
nDate2 = date2.getNumericValue();
var ndiff = nDate2-nDate1;
var gdt = new GlideDateTime();
gdt.setNumericValue(ndiff);
Please mark correct/helpful if my response helped you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-01-2024 11:00 AM
Made the changes to the script (below). It is still not working. Just like before it doesn't even create the Metric record after the RITM is created.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2024 06:59 AM
Hi @Mike Cory ,
Are the debug messages getting logged in your script , is the control coming to your script or not , if yes then can you try and print the values of ndate1 , ndate2 and ndiff after setnumericvalue statement and then please verify the data type of "duration" column as well.
Please mark correct/helpful if my response helped you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2024 07:52 AM - edited 03-02-2024 07:53 AM
Hi Cory,
I would do something like this
// definiton: GlideRecord - this row
// current: current record
if (current.sc_cat_item == 'firewall') {
if (current.stage== 'requested')
createMd();
else if(current.stage== 'approved')
closeMd();
}
//calls the script include "MetricInstance" and create a metric instance
function createMd() {
var md = new MetricInstance(definition, current);
// check if a metric instance exists already
if (md.metricExists()) {
return;
}
var gr = md.startDuration();
}
//calls the script include "MetricInstance" and close the metric instance
function closeMd() {
var md = new MetricInstance(definition, current);
if (md.metricExists()) {
md.endDuration();
}
}