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

Hi @Yougander patel ,

 

The code you have provided looks good to me and should fulfill your requirement of listing all open tickets that have breached SLA at the end of the day, including the assignment group and assigned to on that day's report.

However, if a ticket is still out of SLA the next day but changed assignment, the code you provided will not record this.

 

Here's an updated version of the checkSLABreaches() function that should include tickets that were out of SLA the previous day but have changed assignment:

 

 

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());
  }

  // Check for tickets that were out of SLA the previous day but have changed assignment
  var previousDay = new GlideDateTime();
  previousDay.addDays(-1);
  gr = new GlideRecord('incident');
  gr.addActiveQuery();
  gr.addNotNullQuery('assignment_group');
  gr.addNotNullQuery('assigned_to');
  gr.addQuery('sla_due', '<=', previousDay);
  gr.addQuery('sys_updated_on', '>', previousDay);
  gr.addQuery('close_code', '','!='); // Exclude closed incidents
  gr.query();

  while (gr.next()) {
    gs.print("Incident " + gr.number + " was out of SLA the previous day but has changed assignment.");
    gs.print("Assigned Group: " + gr.assignment_group.getDisplayValue());
    gs.print("Assigned To: " + gr.assigned_to.getDisplayValue());
  }
}

 

 

 

Thanks,

Ratnakar

Hi Ratanakar ,

 

Is there any way to test these tasks because iam unable to test above two tasks as you mentioned please give other way to test the both codes weather working or not as expected