Problem in Creating Custom Metric Definition

Yougander patel
Tera Contributor

if (!current.active) {
answer = false;
mi.endDuration();
closeDurations(mi.current);
}

function closeDurations(current) {
var gr = new GlideRecord('metric_instance');
gr.addQuery('id', current.sys_id);
gr.addQuery('calculation_complete', false);
gr.addQuery('definition.type', 'field_value_duration');
gr.query();
while (gr.next()) {
var definition = new GlideRecord('metric_definition');
definition.get(gr.definition);
var mi = new MetricInstance(definition, current);

// Get the start and end times of the current metric instance
var start = new GlideDateTime(gr.start);
var end = new GlideDateTime(gr.end);

// Calculate the duration excluding weekends
var duration = 0;
var tempStart = new GlideDateTime(start);
var tempEnd = new GlideDateTime(end);
while (tempStart.compareTo(tempEnd) < 0) {
// Check if the current day is a weekend
if (tempStart.getDayOfWeek() == 6 || tempStart.getDayOfWeek() == 0) {
// Skip the weekend
tempStart.addDays(1);
continue;
}

// Add a day to the duration
duration++;
tempStart.addDays(1);
}

// Set the duration on the metric instance
mi.setValue(duration);
mi.endDuration();
}
}

 

The Above code not working as my task is that incident record should calculate the duration from Monday to Friday ,exclude Saturday and Sunday should continue. can anyone please reverify above and provide exact code which matches my requirement. i have selected table incident, field is Active; and type is Script Calculation ;while creating a new metric definition.

1 ACCEPTED SOLUTION

Hi @Yougander patel ,

 

To test this code, you can create a test incident record with a start time and end time that span across a weekend. Then, run the script and verify that the duration calculated excludes the weekend.

Here are the steps you can follow:

  1. Create an incident record with a start time on a Friday evening and an end time on a Monday morning.
  2. Open the script debugger and set a break point at the first line of the code.
  3. Open the incident record and click the 'Run script' button.
  4. When the script debugger stops at the break point, step through the code line by line and verify that the variables are being set correctly.
  5. Once the script execution completes, check the value of the duration variable to verify that the weekend has been excluded.
  6. You can also check the value of the answer variable to ensure that it is false when the incident is not active.

If the duration calculation is working as expected, it should exclude the weekend and only calculate the duration for the weekdays.

 

Script Debugger 

Script Debugger KB 

 

Thanks,

Ratnakar

View solution in original post

6 REPLIES 6

Ratnakar7
Mega Sage
Mega Sage

Hi @Yougander patel ,

 

It looks like you're attempting to calculate the duration between the start and end times of a Metric Instance record and exclude weekends. However, it's not clear how this code relates to your specific metric definition for incident records.

To calculate the duration between the start and end times of an incident record and exclude weekends, you can modify the code as follows:

 

if (!current.active) {
    answer = false;
    mi.endDuration();
}

// Calculate the duration between the start and end times, excluding weekends
var start = new GlideDateTime(current.opened_at);
var end = new GlideDateTime(current.resolved_at);
var duration = 0;
var tempStart = new GlideDateTime(start);
var tempEnd = new GlideDateTime(end);
while (tempStart.compareTo(tempEnd) < 0) {
    // Check if the current day is a weekend
    if (tempStart.getDayOfWeek() == 6 || tempStart.getDayOfWeek() == 0) {
        // Skip the weekend
        tempStart.addDays(1);
        continue;
    }

    // Add a day to the duration
    duration++;
    tempStart.addDays(1);
}

// Set the duration on the metric instance
mi.setValue(duration);
mi.endDuration();

 

 

If my response helps you to resolve the issue close the question by Accepting solution and hit 👍thumb icon. From Correct answers others will get benefited in future.

 

Thanks,

Ratnakar

 

Hi Ratnakar ,

 

Thank for the code ,How Do i test this code weather it is working or not exactly as expected ??

 

Hi @Yougander patel ,

 

To test this code, you can create a test incident record with a start time and end time that span across a weekend. Then, run the script and verify that the duration calculated excludes the weekend.

Here are the steps you can follow:

  1. Create an incident record with a start time on a Friday evening and an end time on a Monday morning.
  2. Open the script debugger and set a break point at the first line of the code.
  3. Open the incident record and click the 'Run script' button.
  4. When the script debugger stops at the break point, step through the code line by line and verify that the variables are being set correctly.
  5. Once the script execution completes, check the value of the duration variable to verify that the weekend has been excluded.
  6. You can also check the value of the answer variable to ensure that it is false when the incident is not active.

If the duration calculation is working as expected, it should exclude the weekend and only calculate the duration for the weekdays.

 

Script Debugger 

Script Debugger KB 

 

Thanks,

Ratnakar

i have other Requirement that  i Would like to list all open tickets that have breached SLA at the end of the day. This needs to include the assignment group and assigned to on that days report. If ticket still out of SLA the net day but changed assignment we need to record this 

here while Creating a metric  i need to some inputs 

like;

Table :incident

Field:"       " - which Field to take

Type: script or Field Calculation

 here i have code can you  please review the code which  justify above Requirement. Please get me correct code :

if (!current.active) {
answer = false;
mi.endDuration();
closeDurations(mi.current);
checkSLABreaches();
}

function closeDurations(current) {
var gr = new GlideRecord('metric_instance');
gr.addQuery('id', current.sys_id);
gr.addQuery('calculation_complete', false);
gr.addQuery('definition.type', 'field_value_duration');
gr.query();
while (gr.next()) {
var definition = new GlideRecord('metric_definition');
definition.get(gr.definition);
var mi = new MetricInstance(definition, current);
mi.endDuration();
}
}

function checkSLABreaches() {
var gr = new GlideRecord('incident');
gr.addActiveQuery();
gr.addNotNullQuery('assignment_group');
gr.addNotNullQuery('assigned_to');
gr.addQuery('sla_due', '<=', gs.beginningOfToday());
gr.addQuery('close_code', '','!='); // Exclude closed incidents
gr.query();

while (gr.next()) {
gs.print("Incident " + gr.number + " has breached its SLA and is still open.");
gs.print("Assigned Group: " + gr.assignment_group.getDisplayValue());
gs.print("Assigned To: " + gr.assigned_to.getDisplayValue());
}
}