<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>question GlideQuery .select(Fields) is not working as expected in Developer forum</title>
    <link>https://www.servicenow.com/community/developer-forum/glidequery-select-fields-is-not-working-as-expected/m-p/3058057#M1148820</link>
    <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a requirement where I'm passing set of sys_id's in a order to get some fields using the&amp;nbsp;GlideQuery.select(Fields) method.&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sample code:&lt;/P&gt;&lt;P&gt;var sysID = {1,2.3,....100};&lt;/P&gt;&lt;P&gt;var selectedFields = [a,b,c,d];&lt;/P&gt;&lt;P&gt;var gq = GlideQuery(table);&lt;/P&gt;&lt;P&gt;gq = gq.where('sys_idIN' + sysID);//Till here the code was going on the same order as the sysid I passed.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;gq = gq.select(selectedFields);&lt;/P&gt;&lt;P&gt;gs.info("ANS: " + gq); //Here the sysid is getting mixed up and showing data on some random order&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is it possible to get the output also in the same order of sys_id which was passed in the script?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Surya. P&lt;/P&gt;</description>
    <pubDate>Fri, 27 Sep 2024 12:53:44 GMT</pubDate>
    <dc:creator>Surya_Prakash</dc:creator>
    <dc:date>2024-09-27T12:53:44Z</dc:date>
    <item>
      <title>GlideQuery .select(Fields) is not working as expected</title>
      <link>https://www.servicenow.com/community/developer-forum/glidequery-select-fields-is-not-working-as-expected/m-p/3058057#M1148820</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a requirement where I'm passing set of sys_id's in a order to get some fields using the&amp;nbsp;GlideQuery.select(Fields) method.&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sample code:&lt;/P&gt;&lt;P&gt;var sysID = {1,2.3,....100};&lt;/P&gt;&lt;P&gt;var selectedFields = [a,b,c,d];&lt;/P&gt;&lt;P&gt;var gq = GlideQuery(table);&lt;/P&gt;&lt;P&gt;gq = gq.where('sys_idIN' + sysID);//Till here the code was going on the same order as the sysid I passed.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;gq = gq.select(selectedFields);&lt;/P&gt;&lt;P&gt;gs.info("ANS: " + gq); //Here the sysid is getting mixed up and showing data on some random order&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is it possible to get the output also in the same order of sys_id which was passed in the script?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Surya. P&lt;/P&gt;</description>
      <pubDate>Fri, 27 Sep 2024 12:53:44 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/glidequery-select-fields-is-not-working-as-expected/m-p/3058057#M1148820</guid>
      <dc:creator>Surya_Prakash</dc:creator>
      <dc:date>2024-09-27T12:53:44Z</dc:date>
    </item>
    <item>
      <title>Re: GlideQuery .select(Fields) is not working as expected</title>
      <link>https://www.servicenow.com/community/developer-forum/glidequery-select-fields-is-not-working-as-expected/m-p/3058070#M1148825</link>
      <description>&lt;P&gt;hi&amp;nbsp;&lt;a href="https://www.servicenow.com/community/user/viewprofilepage/user-id/496228"&gt;@Surya_Prakash&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Try bellow script and check once:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;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));
});&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;i hope my answer helps you to resolve your issue, if yes please mark my answer helpful and correct.&lt;/P&gt;&lt;P&gt;thank you&lt;/P&gt;&lt;P&gt;rajesh&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 27 Sep 2024 13:04:15 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/glidequery-select-fields-is-not-working-as-expected/m-p/3058070#M1148825</guid>
      <dc:creator>Rajesh Chopade1</dc:creator>
      <dc:date>2024-09-27T13:04:15Z</dc:date>
    </item>
    <item>
      <title>Re: GlideQuery .select(Fields) is not working as expected</title>
      <link>https://www.servicenow.com/community/developer-forum/glidequery-select-fields-is-not-working-as-expected/m-p/3058117#M1148843</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://www.servicenow.com/community/user/viewprofilepage/user-id/74479"&gt;@Rajesh Chopade1&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for the reply. Currently I'm getting the code in below format. Can you please help to just fetch the sysid's (Value) from the code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;GlideQuery&amp;lt;tablename&amp;gt; [&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;{&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;"type": "where",&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;"field": "sys_id",&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;"operator": "IN",&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;"value": [&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;"sysid 1",&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;"sysid 2",&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;"sysid 3"&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;],&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;},&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;]&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Thanks,&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Surya. P&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 27 Sep 2024 14:01:37 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/glidequery-select-fields-is-not-working-as-expected/m-p/3058117#M1148843</guid>
      <dc:creator>Surya_Prakash</dc:creator>
      <dc:date>2024-09-27T14:01:37Z</dc:date>
    </item>
    <item>
      <title>Re: GlideQuery .select(Fields) is not working as expected</title>
      <link>https://www.servicenow.com/community/developer-forum/glidequery-select-fields-is-not-working-as-expected/m-p/3058125#M1148846</link>
      <description>&lt;P&gt;hi&amp;nbsp;&lt;a href="https://www.servicenow.com/community/user/viewprofilepage/user-id/496228"&gt;@Surya_Prakash&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here you just simply focus on fetching the values from the "value" array.&lt;/P&gt;&lt;P&gt;Assuming you have input in a variable, you can use following code.&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;// Assuming this is your input data structure
var input = {
    "type": "where",
    "field": "sys_id",
    "operator": "IN",
    "value": [
        "sysid 1",
        "sysid 2",
        "sysid 3"
    ]
};

// Extracting sys_ids
var sysIds = input.value;

gs.info("Extracted sys_ids: " + sysIds.join(", ")); 
// output : Extracted sys_ids: sysid 1, sysid 2, sysid 3

//If you want to use these sys_id values in a GlideQuery for further operations, you can use the IN operator
var gq = new GlideQuery('tablename');
gq = gq.where('sys_id', 'IN', sysIds);
gq.select('sys_id'); // or any other fields you want
&lt;/LI-CODE&gt;</description>
      <pubDate>Fri, 27 Sep 2024 14:11:18 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/glidequery-select-fields-is-not-working-as-expected/m-p/3058125#M1148846</guid>
      <dc:creator>Rajesh Chopade1</dc:creator>
      <dc:date>2024-09-27T14:11:18Z</dc:date>
    </item>
    <item>
      <title>Re: GlideQuery .select(Fields) is not working as expected</title>
      <link>https://www.servicenow.com/community/developer-forum/glidequery-select-fields-is-not-working-as-expected/m-p/3058127#M1148848</link>
      <description>&lt;P&gt;Please review:&amp;nbsp;&lt;A href="https://developer.servicenow.com/dev.do#!/reference/api/washingtondc/server_legacy/c_GlideQueryConditionAPI" target="_blank" rel="noopener"&gt;GlideQueryCondition API&lt;/A&gt;&lt;/P&gt;&lt;P&gt;If you want your code to work.&lt;/P&gt;</description>
      <pubDate>Fri, 27 Sep 2024 14:16:23 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/glidequery-select-fields-is-not-working-as-expected/m-p/3058127#M1148848</guid>
      <dc:creator>Bert_c1</dc:creator>
      <dc:date>2024-09-27T14:16:23Z</dc:date>
    </item>
    <item>
      <title>Re: GlideQuery .select(Fields) is not working as expected</title>
      <link>https://www.servicenow.com/community/developer-forum/glidequery-select-fields-is-not-working-as-expected/m-p/3058140#M1148851</link>
      <description>&lt;P&gt;No luck, it is showing "undefined" as the output when I'm fetching Value from the code.&lt;/P&gt;&lt;P&gt;Note: This is not a payload, it was the data fetched using another glide query line.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ex: glidequery.where(Parameter[field], Parameter[operator], Parameter[value]);&lt;/P&gt;</description>
      <pubDate>Fri, 27 Sep 2024 14:24:27 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/glidequery-select-fields-is-not-working-as-expected/m-p/3058140#M1148851</guid>
      <dc:creator>Surya_Prakash</dc:creator>
      <dc:date>2024-09-27T14:24:27Z</dc:date>
    </item>
    <item>
      <title>Re: GlideQuery .select(Fields) is not working as expected</title>
      <link>https://www.servicenow.com/community/developer-forum/glidequery-select-fields-is-not-working-as-expected/m-p/3059282#M1149174</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://www.servicenow.com/community/user/viewprofilepage/user-id/74479"&gt;@Rajesh Chopade1&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for your reply, this was the solution I expected and with some minor changes to my code I was able to achieve the output.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Surya. P&lt;/P&gt;</description>
      <pubDate>Mon, 30 Sep 2024 05:50:13 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/glidequery-select-fields-is-not-working-as-expected/m-p/3059282#M1149174</guid>
      <dc:creator>Surya_Prakash</dc:creator>
      <dc:date>2024-09-30T05:50:13Z</dc:date>
    </item>
  </channel>
</rss>

