Cannot receive time and date from the "Resolution Time" field

I made a script that should send notifications to specific channels in Slack when the SLA expires. The script's logic assumes it will get the date and time from the "Resolution Time" field. Unfortunately, I did not find exactly how to specify this field in the documentation or examples. Currently, the field is indicated as "undefined" in the console.

 

// Modules
const entities = require('@jetbrains/youtrack-scripting-api/entities');
const http = require('@jetbrains/youtrack-scripting-api/http');
const workflow = require('@jetbrains/youtrack-scripting-api/workflow');
const dateTime = require('@jetbrains/youtrack-scripting-api/date-time');

// Slack bot token
const BOT_TOKEN = "XXXXX";

// Channels by issue type
const CHANNELS_BY_TYPE = {

};

// Priority by type for adding color in notification
const PRIORITY_BY_TYPE = {
 Critical: ":large_red_square:",
 Urgent: ":large_orange_square:",
 High: ":large_yellow_square:",
 Normal: ":large_green_square:",
 Low: ":large_blue_square:"
};

// Function to send message to Slack channel
function sendMessageToSlack(channelID, message) {
 const postMessageConnection = new http.Connection("https://slack.com/api/chat.postMessage", null, 2000);
 const postMessagePayload = {
   "channel": channelID,
   "text": message
 };

 postMessageConnection.addHeader("Authorization", "Bearer " + BOT_TOKEN);
 postMessageConnection.addHeader("Content-Type", "application/json");

 const postMessageResult = postMessageConnection.postSync('', [], postMessagePayload);

 if (!postMessageResult.isSuccess) {
   console.warn("Error sending message:", postMessageResult);
   return null;
 }

 return postMessageResult.response;
}

// Check if ticket SLA is nearing expiration (here is the problem)
function isSLANearingExpiration(issue, warningTime) {
 const ResolutionTime = "Resolution Time";
 const SLAExpiration = issue.fields[`${ResolutionTime}`];
 const currentTime = Date.now();
 return SLAExpiration && SLAExpiration - currentTime <= warningTime;
}

// Function to format message for Slack notification
function formatSlackMessage(issue) {
 const priority = issue.fields.Priority ? issue.fields.Priority.name : null;
 const priorityIcon = PRIORITY_BY_TYPE[priority] || '';
 return `:warning: Attention! SLA for ticket ${issue.id} is nearing expiration. Priority: ${priorityIcon} ${priority}.`;
}

exports.rule = entities.Issue.onSchedule({
title: workflow.i18n('send-notification-to-slack-when-sla-nears-expiration'),
 search: '#Unresolved SLAExpiration: {0d}',
 cron: '0 0 * * * ?', // Run every hour
 action: function(ctx) {
   const issue = ctx.issue;

   // Calculate warning time based on priority
   let warningTime = 24 * 60 * 60 * 1000; // Default warning time is 1 day
   const priority = issue.fields.Priority ? issue.fields.Priority.name : null;
   if (priority === 'Critical') {
     warningTime = 80 * 60 * 1000; // 30 minutes for Critical priority
   } else if (priority === 'Urgent') {
     warningTime = 60 * 60 * 1000; // 1 hour for Urgent priority
   } else if (priority === 'High') {
     warningTime = 3 * 60 * 60 * 1000; // 3 hours for High priority
   } else if (priority === 'Normal') {
     warningTime = 24 * 60 * 60 * 1000; // 1 day for Normal priority
   } else if (priority === 'Low') {
     warningTime = 3 * 24 * 60 * 60 * 1000; // 3 days for Low priority
   }

   // Check if SLA is nearing expiration
   if (isSLANearingExpiration(issue, warningTime)) {
     const issueType = issue.fields.Type ? issue.fields.Type.name : null;
     const channelID = CHANNELS_BY_TYPE[issueType];
     
     // Ensure channel ID is defined for the issue type
     if (channelID) {
       // Check if notification was sent within the last 24 hours
       const lastNotificationSent = issue.fields.LastNotificationSent;
       const currentTime = Date.now();
       if (!lastNotificationSent || currentTime - lastNotificationSent > 24 * 60 * 60 * 1000) {
         const message = formatSlackMessage(issue);
         sendMessageToSlack(channelID, message);
         // Update LastNotificationSent field
         issue.fields.LastNotificationSent = currentTime;
       }
     } else {
       console.warn(`Channel not defined for issue type ${issueType}`);
     }
   }
 }
});

0
3 comments

Hello Ernesto Manuel 

Please use issue.resolved, this property contains the date when the issue was resolved.

0

Hello Alisa Kasyanova 


Thanks for the advice, but this is not exactly what I need. By this script, the system should notify that the ticket’s SLA is about to expire (that is, it has not yet been resolved at this moment)

0

Ernesto Manuel ah, sorry! You can use ctx.issue.fields['Resolution Time'] to get the SLA's resolution time (the same syntax is applicable to Next Reply and First Reply) .

0

Please sign in to leave a comment.