We're reclaiming inactive PDIs to keep them available for active builders. Learn what's changing, who's affected, and how to protect your work. Read More

Find max number from array

dvelloriy
Kilo Sage

Hi All,

 

I have the below code but its not working as expected. I am storing few numbers in RITM description and wanted to obtain the max number.

----------------

var ritmAT = [];
        var gr = new GlideRecord("sc_req_item");
        gr.addEncodedQuery("cat_item=4410e34a1b649a101a29eb98b04bcbfe^sys_created_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()");
        gr.orderByDesc('sys_created_on');
        gr.setLimit(1);
        gr.query(); //101, 102
        gs.info("$$$Records in RITM table: " + gr.getRowCount());
        while (gr.next()) {
            ritmAT.push(gr.description); //101, 102
        }
        gs.info("$$$ RITMAT: " + ritmAT);
        var maxNum = Math.max.apply(null,ritmAT); //102
        gs.info("$$$Max Number: " + maxNum);
 
dvelloriy_0-1730128407109.png

 

1 ACCEPTED SOLUTION

Forcing the retrieved description to a string should remedy that

    var descArr = gr.description.toString().split(',');

View solution in original post

9 REPLIES 9

Ravi Peddineni
Kilo Sage

@dvelloriy 

gr.description might be a string field. Try below code in the while loop:

ritmAT.push(parseInt(gr.description)); //101, 102

 

Still showing NaN

@dvelloriy 

Can you try this:

ritmAT.push(parseInt(gr.getValue('description')));

// Make sure you're getting the description, split desc on comma, then reduce array (allows parsing of string to int).

var desc = '101, 102';

var descNums = desc.split(',');
var max = descNums.reduce(function(a, c) {
return Math.max(parseInt(a), parseInt(c));
}, descNums[0]);

gs.info('max='+max);