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.

array of object to get lateste updated date (lastEscalatedTS) and that caseNumber

siva mani kanta
Tera Expert

Hi All,

In below script i need to get latest date of 'lastEscalatedTS' case number 

 [{"caseNumber" : "3PR0217912", "firstEscalatedTS" : "2024-02-28T12:33:52Z", "lastEscalatedTS" : "2024-02-28T12:34:44Z"}, {"caseNumber" : "3PR0218796", "firstEscalatedTS" : "2024-03-04T16:27:18Z", "lastEscalatedTS" : "2024-03-04T16:26:45Z"}]

1 ACCEPTED SOLUTION

var jsonString= ' [{"caseNumber" : "3PR0217912", "firstEscalatedTS" : "2024-02-28T12:33:52Z", "lastEscalatedTS" : "2024-02-28T12:34:44Z"}, {"caseNumber" : "3PR0218796", "firstEscalatedTS" : "2024-03-04T16:27:18Z", "lastEscalatedTS" : "2024-03-04T16:26:45Z"}]';
var cases = JSON.parse(jsonString);

// Initialize a variable to store the latest date
var latestDate;

// Iterate through the array of objects
for (var i = 0; i < cases.length; i++) {
// Get the 'lastEscalatedTS' value for the current case
var currentLastEscalatedTS = cases[i].lastEscalatedTS;

// If this is the first iteration or the current date is later than the latest date found so far
if (!latestDate || currentLastEscalatedTS > latestDate) {
// Update the latest date to the current date
latestDate = currentLastEscalatedTS;
var casenumber = cases[i].caseNumber;
}
}

// Output the latest date
gs.info('Latest date of lastEscalatedTS: ' + latestDate);

gs.info('Latest date of lastEscalatedTS: ' + casenumber);

View solution in original post

3 REPLIES 3

Astik Thombare
Tera Sage
Tera Sage

Hi @siva mani kanta ,

 

To find the latest date of 'lastEscalatedTS' case number from given JSON  Formatted String . Please use below script 

 

 

var jsonString= ' [{"caseNumber" : "3PR0217912", "firstEscalatedTS" : "2024-02-28T12:33:52Z", "lastEscalatedTS" : "2024-02-28T12:34:44Z"}, {"caseNumber" : "3PR0218796", "firstEscalatedTS" : "2024-03-04T16:27:18Z", "lastEscalatedTS" : "2024-03-04T16:26:45Z"}]';
var cases = JSON.parse(jsonString);

// Initialize a variable to store the latest date
var latestDate;

// Iterate through the array of objects
for (var i = 0; i < cases.length; i++) {
    // Get the 'lastEscalatedTS' value for the current case
    var currentLastEscalatedTS = cases[i].lastEscalatedTS;

    // If this is the first iteration or the current date is later than the latest date found so far
    if (!latestDate || currentLastEscalatedTS > latestDate) {
        // Update the latest date to the current date
        latestDate = currentLastEscalatedTS;
    }
}

// Output the latest date
gs.info('Latest date of lastEscalatedTS: ' + latestDate);

 

 

 

Please mark Correct if this resolves your issue, and also mark Helpful if you find my response valuable based on its impact.  

 

Regards,

Astik Thombare

HI @Astik Thombare 
the above script is fine but i need to get that latest date object i need that  casenumber

var jsonString= ' [{"caseNumber" : "3PR0217912", "firstEscalatedTS" : "2024-02-28T12:33:52Z", "lastEscalatedTS" : "2024-02-28T12:34:44Z"}, {"caseNumber" : "3PR0218796", "firstEscalatedTS" : "2024-03-04T16:27:18Z", "lastEscalatedTS" : "2024-03-04T16:26:45Z"}]';
var cases = JSON.parse(jsonString);

// Initialize a variable to store the latest date
var latestDate;

// Iterate through the array of objects
for (var i = 0; i < cases.length; i++) {
// Get the 'lastEscalatedTS' value for the current case
var currentLastEscalatedTS = cases[i].lastEscalatedTS;

// If this is the first iteration or the current date is later than the latest date found so far
if (!latestDate || currentLastEscalatedTS > latestDate) {
// Update the latest date to the current date
latestDate = currentLastEscalatedTS;
var casenumber = cases[i].caseNumber;
}
}

// Output the latest date
gs.info('Latest date of lastEscalatedTS: ' + latestDate);

gs.info('Latest date of lastEscalatedTS: ' + casenumber);