PA Indicator to show Pending Approvals by Timeframe

alhicks
Tera Guru

We're trying to show the number of pending approvals weekly by age ranges, a snapshot of what was pending approval on that given date.  We have quite a few months that are missing data, and the greater than 2 months range has the same number for several weeks.  I'm not sure what I'm missing here.  😞

 

Widget

alhicks_0-1710267996624.png

 

alhicks_1-1710268026789.png

 

Indicator

alhicks_2-1710268119525.png

 

Indicator Source

alhicks_3-1710268171332.png

Bucket Group

alhicks_4-1710268212282.png

 

Breakdown

alhicks_5-1710268266856.png

Breakdown Mapping

alhicks_6-1710268311469.png

 

 

 

1 ACCEPTED SOLUTION

Hi @alhicks ,

 

Sorry for the late reply, had some work to be done prior to my respond. If you would like to see if the approval time has improved, you need to look into how long it has taken from requested to approved. 

 

This can be completed by creating a metric definition on the sysapproval_approver table. As you see below I created below in my PDI:

AndersBGS_0-1710922029523.png

With below script to complete the population:

if (current.state.toLowerCase() === 'approved') {
    createMetric();
}

function createMetric() {
    var mi = new MetricInstance(definition, current);
    var gr = mi.getNewRecord();
    gr.field_value = current.state;
    var start = new GlideDateTime(current.sys_created_on);
    var end = new GlideDateTime(current.sys_updated_on);
    gr.start = start;
    gr.end = end;
    // Calculate duration in milliseconds
    var durationMillis = Math.abs(start.getNumericValue() - end.getNumericValue());
    // Convert milliseconds to duration string
    var durationString = durationToTimeString(durationMillis);
    gr.duration = durationString;
    gr.calculation_complete = true;
    gr.insert();
}

// Function to convert milliseconds to duration string
function durationToTimeString(durationInMillis) {
    var seconds = Math.floor(durationInMillis / 1000);
    var minutes = Math.floor(seconds / 60);
    var hours = Math.floor(minutes / 60);

    seconds = seconds % 60;
    minutes = minutes % 60;

    return pad(hours) + ':' + pad(minutes) + ':' + pad(seconds);
}

// Function to pad single digits with leading zero
function pad(number) {
    if (number < 10) {
        return '0' + number;
    }
    return number;
}

This will provide the result:

AndersBGS_1-1710922090929.png

Please note, for the metric to be populated, you need to go to metrics --> business rule:

AndersBGS_2-1710922144416.png

Make a copy of the metrics events (see top row):

AndersBGS_3-1710922180391.png

 

and just correct the task table to sysapproval_approver for the new business rule:

AndersBGS_4-1710922237223.png

The reason that you need to do this is, that the metrics event runs on the task table, but the sysapprover_approval is not extended from task table. 

 

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.

 

best regards

Anders

 

 

 

If my answer has helped with your question, please mark my answer as the accepted solution and give a thumbs up.

Best regards
Anders

Rising star 2024
MVP 2025
linkedIn: https://www.linkedin.com/in/andersskovbjerg/

View solution in original post

12 REPLIES 12

I'm sorry about the delay.  I looked at the numbers with my manager and something just doesn't look right.   I didn't see a way to filter on requested on this month.  I'm not sure if I have something off in this filter or in my breakdown script, anything is possible here I guess. 🤔   And maybe were taking the wrong approach here.  Our management is trying to see if the approval times are improving since they've added approval reminders.  

 

alhicks_0-1710531463573.png

 

Hi @alhicks ,

 

Sorry for the late reply, had some work to be done prior to my respond. If you would like to see if the approval time has improved, you need to look into how long it has taken from requested to approved. 

 

This can be completed by creating a metric definition on the sysapproval_approver table. As you see below I created below in my PDI:

AndersBGS_0-1710922029523.png

With below script to complete the population:

if (current.state.toLowerCase() === 'approved') {
    createMetric();
}

function createMetric() {
    var mi = new MetricInstance(definition, current);
    var gr = mi.getNewRecord();
    gr.field_value = current.state;
    var start = new GlideDateTime(current.sys_created_on);
    var end = new GlideDateTime(current.sys_updated_on);
    gr.start = start;
    gr.end = end;
    // Calculate duration in milliseconds
    var durationMillis = Math.abs(start.getNumericValue() - end.getNumericValue());
    // Convert milliseconds to duration string
    var durationString = durationToTimeString(durationMillis);
    gr.duration = durationString;
    gr.calculation_complete = true;
    gr.insert();
}

// Function to convert milliseconds to duration string
function durationToTimeString(durationInMillis) {
    var seconds = Math.floor(durationInMillis / 1000);
    var minutes = Math.floor(seconds / 60);
    var hours = Math.floor(minutes / 60);

    seconds = seconds % 60;
    minutes = minutes % 60;

    return pad(hours) + ':' + pad(minutes) + ':' + pad(seconds);
}

// Function to pad single digits with leading zero
function pad(number) {
    if (number < 10) {
        return '0' + number;
    }
    return number;
}

This will provide the result:

AndersBGS_1-1710922090929.png

Please note, for the metric to be populated, you need to go to metrics --> business rule:

AndersBGS_2-1710922144416.png

Make a copy of the metrics events (see top row):

AndersBGS_3-1710922180391.png

 

and just correct the task table to sysapproval_approver for the new business rule:

AndersBGS_4-1710922237223.png

The reason that you need to do this is, that the metrics event runs on the task table, but the sysapprover_approval is not extended from task table. 

 

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.

 

best regards

Anders

 

 

 

If my answer has helped with your question, please mark my answer as the accepted solution and give a thumbs up.

Best regards
Anders

Rising star 2024
MVP 2025
linkedIn: https://www.linkedin.com/in/andersskovbjerg/

Thank you so much.  I'll pass this on to our Admins, I don't have access to the metrics.  But yesterday afternoon I was able to get the filter on my indicator to get the numbers we were looking for, or at least they look correct now.  😉