Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Populating an array from a glide_list field - Works when there is ONE entry but not if there is more

andrewbettc
Kilo Sage

As the title - I have a UI action that calls a script include. It gets the values from a glide_list field and populates an array. I then do a query using the sys_ids from the array and put the results in another array. The idea is that one glide_list will populate another glide_list of related records.


If the original glide_list has one value then I get results out in the log but if I add more than one (say, two, for example!!), I get nothing at all.

My code is on VDI. I'll get it out and post it.

 

Update: As promised, code from Script Include:

var refreshIBS = Class.create();
refreshIBS.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

    RefreshImportantBusinessServices: function() {

        var kb = this.getParameter('sysparm_kb');
        var mappedApplicationServices = [];
        var kbMAS = new GlideRecord('kb_knowledge');

        //query knowledge article
        kbMAS.addQuery('sys_id', kb);
        kbMAS.query();

        /*Get values from Mapped Application Service field
         and add to mappedApplicationServices array*/
        var relatedIbs = [];

        while (kbMAS.next()) {

            mappedApplicationServices.push(kbMAS.getValue('u_mapped_application_service'));

            /*query service offerings using values from
            mappedApplicationServices array*/

            for (var i in mappedApplicationServices) {

                var ibs = new GlideRecord('service_offering');
                ibs.addEncodedQuery('parent=' + mappedApplicationServices[i]);
                ibs.query();

                //instantiate array to store results

                // var relatedIbs = [];

                //loop through results and add to relatedIbs array

                while (ibs.next()) {
                    relatedIbs.push(ibs.sys_id.toString());
                    // relatedIbs.push(ibs.getUniqueValue());
                }
                gs.info('AJB Related IBS 1:' + relatedIbs.join(', \n'));
            }
            gs.info('AJB Related IBS 2:' + relatedIbs);
        }
        gs.info('AJB Related IBS 3:' + relatedIbs);
    },

    type: 'refreshIBS'
});



I thought it was to do with where I instantiate my array but nothing seems to make any difference. 

Any ideas?

3 REPLIES 3

Kris Moncada
Kilo Sage

Hi @andrewbettc ,

 

I believe the issue is in this line of code. 

kbMAS.addQuery('sys_id', kb);

Reason: If kb equals one sys_id, then this query will return results.  But if kb = <sys_id>,<sys_id>,... then this will not return any results.

 

Try changing that line to the following:

 

kbMAS.addQuery('sys_id', 'IN', kb);

 

Thank you Kris. I tried that out. Same result.

Single entry in glide_list = All OK. All three of my log statement output the related sys_ids. but, as soon as I add another entry to the glide_list, the whole thing fails.

This is the log showing the last two runs:

AndrewBettcher_0-1719589565241.png

 

The first 3 are with one entry in the glide_list and the latest 3 are with two.

Mind trying this version of your script? I'm curious about what kb contains and the various row counts.

var kb = this.getParameter('sysparm_kb');

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

var mappedApplicationServices = [];
var kbMAS = new GlideRecord('kb_knowledge');

//query knowledge article
kbMAS.addQuery('sys_id', kb);
kbMAS.query();

gs.info(kbMAS.getRowCount() + ' records returned from kb_knowledge');


/*Get values from Mapped Application Service field
 and add to mappedApplicationServices array*/
var relatedIbs = [];
var mappedApplicationServices = [];

while (kbMAS.next()) {
  mappedApplicationServices.push(kbMAS.getValue('u_mapped_application_service'));
}

gs.info('mappedApplicationServices = ' + mappedApplicationServices.join('\n'));

var ibs = new GlideRecord('service_offering');
ibs.addQuery('parent', 'IN', mappedApplicationServices);
ibs.query();

gs.info(ibs.getRowCount() + ' records returned from service_offering');

while (ibs.next()) {
  relatedIbs.push(ibs.getUniqueValue());
}
gs.info('AJB Related IBS 1:' + relatedIbs.join('\n'));