Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

how to create a metric defination which creates instance only when priority is changed

Gaurav Vaze
Kilo Sage

hello all,
i have a requirement where I have to create a report based on priority changes on incident
for this I want to create a metric defination
how ever I want to restrict the definition in a way that it should create a metric instance only when priority is actually changed but not set during initial creation
I want script for the same
help will be appriciated
any other alternative that can capture the priority change such as scrip ton sys_audit history  will also be considered
thank you

 

1 ACCEPTED SOLUTION

Gaurav Vaze
Kilo Sage

just an update,
I found a workaround for the same requirement

create a script include with the following code

var threeMonthsAgo = gs.daysAgoStart(90); // Calculate the date 90 days ago
    var changedIncidents = [];

    // Query the sys_audit table for changes in the priority field of incidents
    var audit = new GlideRecord('sys_audit');
    audit.addQuery('tablename', 'incident'); // Focus on the incident table
    audit.addQuery('fieldname', 'priority'); // Focus on changes to the priority field
    audit.addQuery('sys_created_on', '>=', threeMonthsAgo); // Limit to changes within the last three months
    audit.query();

    while (audit.next()) {
        // Collect unique Sys IDs of incidents with changed priorities
        var docKey = audit.documentkey.toString();
        if (changedIncidents.indexOf(docKey) === -1) {
            changedIncidents.push(docKey); // Store the Sys ID
        }
    }

   return changedIncidents;

create a report on incident table
use filter condition as follows
sys_id          is one of the            javascript&colon; new <scriptinludename>().<functionname>()



this will give you the incidents from last 3 months for whom priority is changed
Thank you

Mark as helpful if this helped you a little

View solution in original post

4 REPLIES 4

Gaurav Vaze
Kilo Sage

just an update,
I found a workaround for the same requirement

create a script include with the following code

var threeMonthsAgo = gs.daysAgoStart(90); // Calculate the date 90 days ago
    var changedIncidents = [];

    // Query the sys_audit table for changes in the priority field of incidents
    var audit = new GlideRecord('sys_audit');
    audit.addQuery('tablename', 'incident'); // Focus on the incident table
    audit.addQuery('fieldname', 'priority'); // Focus on changes to the priority field
    audit.addQuery('sys_created_on', '>=', threeMonthsAgo); // Limit to changes within the last three months
    audit.query();

    while (audit.next()) {
        // Collect unique Sys IDs of incidents with changed priorities
        var docKey = audit.documentkey.toString();
        if (changedIncidents.indexOf(docKey) === -1) {
            changedIncidents.push(docKey); // Store the Sys ID
        }
    }

   return changedIncidents;

create a report on incident table
use filter condition as follows
sys_id          is one of the            javascript&colon; new <scriptinludename>().<functionname>()



this will give you the incidents from last 3 months for whom priority is changed
Thank you

Mark as helpful if this helped you a little

Sohail Khilji
Kilo Patron

Hi @Gaurav Vaze ,

 

By default it will capture both on insert and onupdate.

 

To avoid insert values you need to create a business rule which runs on prority update which creates a record on metric defination table to track all the updates on priority.


☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect

but looking at the script, we are only concerned about priority, right?
i tried this script in Si and updated the incidents but nothing was concernful
also creating metric instance a good idea however it is difficult for historical data to capture and the definition will also capture other incidents when created isn't it?

vermaamit16
Kilo Patron

Hi @Gaurav Vaze 

 

Below link could be helpful :

https://www.servicenow.com/community/developer-forum/priority-duration-metric-calculation/m-p/187828...

 

Thanks and Regards

Amit Verma

Thanks and Regards
Amit Verma