Change revert to new metric definition

Community Alums
Not applicable

Hi,

I want to create a metric definition to show the count of number of times, change request has been reverted to new state.  For this i have created a metric definition with type script calculation and created a BR with condition when state changes to new and with below code.  When state changes to new for the first time metric has been adding with value 1.  But next time onwards when change moving back to new state count is not increasing.

1 REPLY 1

Shruti
Mega Sage
Mega Sage
(function executeRule(current, previous /*null when async*/ ) {

 

    var count = 0;

 

    // Add your code here
    var metricSysID = '6fbcce7b1bd996906bdb1f87b04bcb67';
    var mi = new GlideRecord('metric_instance');
    mi.addQuery('id', current.sys_id);
    mi.addQuery('definition', metricSysID);
    mi.query();

 

    if (!mi.next()) {

 

        count= 1;    //count is integer

 

        mi.initialize();
        mi.definition = metricSysID;
        mi.id = current.sys_id;
        mi.value = count;
        mi.calculation_complete = true;
        mi.insert();

 

    } else {
        count = mi.value;
     
        count+=1;
        mi.value = count;
        mi.calculation_complete = true;
        mi.update();

 

    }

 

})(current, previous);