Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Creating a Metric for AWA Presence

JHufford
Mega Guru

We are using AWA and I need to track the amount of time our agents are Available. In the past I have been doing this by pulling an excel report from the awa_agent_presence_history table and running some calculations. Unfortunately this is not meeting our needs.

I've created a metric definition:

  • Name: AWA Agent Presence Duration

  • Table: awa_agent_presence_history

  • Field: Presence State

  • Type: field value duration

I've also duplicated the "metrics events" metrics business rule for the awa_agent_presence_history table (per Define a metric (servicenow.com) )

  • Name: Metrics Events AWA Presence

  • Table: awa_agent_presence_history

  • Application: global

  • Script: same as oob Metrics Events

I'm having trouble figuring out where this calculation is being recorded so I can report on it. Or if the metric definition even makes sense or is working at all. Does anyone have pointers to get me back on track?

We have another similar report that looks at the incident state duration so I've mirrored that one

  • Table: incident_metric

  • Type: Single Score

  • Aggregation: Average

  • Aggregated Field: mi_duration

  • Filter: Definition | is | AWA Presence Duration

  • Filter: Value | is | Available

This is reporting 0 - but I think that's because it's querying the incident_metric table. I dont think that's where any of this is being stored but I dont know where it is being stored or if it's even doing anything.

4 REPLIES 4

Tsura Andreeva
Mega Sage

I wonder if you need to create a table to store the new callculated records, you can take a look at your metric and see the records in the Metric Definition 

JHufford
Mega Guru

I'm able to see them in the metric_instance table but there is no end time captured and no duration calculated

Zac_R
Tera Contributor

Following this, I'd like this capability as well. 

For example something that shows Yesterday Agent Smith spent 1hr 23min on Available.

At the moment I can only find state changed. For example Agent Smith changed state at 12pm, and again at 1:23pm. No way to count the time in Available.

I just accomplished this exact task.  

Duplicate the out of the box business rule 'metrics events' to the table 'awa_agent_presence'.  

 

Create a metric definition on the 'awa_agent_presence' table.  

Table = awa_agent_presence

Field = Current presence state

Type = Script Calculation

 

For the script, I have some custom fields for another metric I utilized to grab the agent name and put it directly on the metric instance, but it should be available through the 'ID' field of the metric instance as well.  Here's my script, you can modify as needed:

// Get the current Date/Time
var gdt = new GlideDateTime();
gs.info('AWA first gdt: ' + gdt);

var currentState = current.getDisplayValue('current_presence_state');

// Early exit if no state value
if (!currentState) {
    gs.info('AWA Metric Script: No current display state, skipping');
} else {

    createMetric(gdt);
}

function createMetric(gdt) {
	gs.info('AWA Function gdt: ' + gdt);
    // Get raw for reference
    var currentRaw = current.getValue('current_presence_state');
    // Query LAST OPEN instance for this user
    var lastDisplay = '';
    var lastGR = new GlideRecord('metric_instance');
    lastGR.addQuery('table', current.getTableName());
    lastGR.addQuery('u_assigned_to', current.getDisplayValue('agent'));
    lastGR.addQuery('calculation_complete', false);
    lastGR.orderByDesc('sys_created_on');
    lastGR.setLimit(1);
    lastGR.query();

    // If found, get the record's state
    if (lastGR.next()) {
        lastDisplay = lastGR.getValue('value').toString();

        // Check if the current state is different from the found record
        if (lastDisplay != currentState) {
            // Complete the previous instance
            lastGR.setValue('calculation_complete', true);
            lastGR.setValue('end', gdt);
			lastGR.setValue('duration', gs.dateDiff(lastGR.start, lastGR.end));
            lastGR.update();
        }
    }

    // Create new Metric Instance for the new state
    var mi = new MetricInstance(definition, current);
    var gr = mi.getNewRecord();
    //gr.setValue('field', 'Current presence state');
    gr.setValue('u_assigned_to', current.getDisplayValue('agent'));
    gr.setValue('start', gdt);
    gr.setValue('value', currentState);
    //gr.setValue('table', current.getTableName());
    gr.setValue('field_value', current.current_presence_state);
    //gr.setValue('definition', definition);
    gr.setValue('u_assignment_group', current.getDisplayValue('agent.department'));
    gr.insert();
}