GlideQuery .select(Fields) is not working as expected

Surya_Prakash
Tera Contributor

Hi All,

 

I have a requirement where I'm passing set of sys_id's in a order to get some fields using the GlideQuery.select(Fields) method.

My expected output was to get the result in same order of the sys_id I'm passing in the query, but the code is returning data's based on some other order.

 

Sample code:

var sysID = {1,2.3,....100};

var selectedFields = [a,b,c,d];

var gq = GlideQuery(table);

gq = gq.where('sys_idIN' + sysID);//Till here the code was going on the same order as the sysid I passed.

 

gq = gq.select(selectedFields);

gs.info("ANS: " + gq); //Here the sysid is getting mixed up and showing data on some random order

 

Is it possible to get the output also in the same order of sys_id which was passed in the script?

 

Thanks,

Surya. P

1 ACCEPTED SOLUTION

Rajesh Chopade1
Mega Sage

hi @Surya_Prakash 

Try bellow script and check once:

var sysIDs = ['sys_id_1', 'sys_id_2', 'sys_id_3', /* ... up to 100 */];
var selectedFields = ['a', 'b', 'c', 'd'];

var gq = new GlideQuery('your_table_name');
gq = gq.where('sys_id', 'IN', sysIDs); 

var results = gq.select(selectedFields).toArray();

// Create a temporary map to hold the results keyed by sys_id
var resultMap = {};
results.forEach(function(record) {
    resultMap[record.sys_id] = record;
});

// Output results in the same order as the original sys_id array
var orderedResults = sysIDs.map(function(id) {
    return resultMap[id]; // Retrieve from the map
}).filter(function(record) {
    return record !== undefined; // Filter out any undefined values if sys_id was not found
});

// Log the ordered results
orderedResults.forEach(function(result) {
    gs.info("Result for sys_id " + result.sys_id + ": " + JSON.stringify(result));
});

 

i hope my answer helps you to resolve your issue, if yes please mark my answer helpful and correct.

thank you

rajesh

 

View solution in original post

6 REPLIES 6

No luck, it is showing "undefined" as the output when I'm fetching Value from the code.

Note: This is not a payload, it was the data fetched using another glide query line.

 

ex: glidequery.where(Parameter[field], Parameter[operator], Parameter[value]);

Bert_c1
Kilo Patron

Please review: GlideQueryCondition API

If you want your code to work.