Return values from script include

servicenow14710
Tera Expert

In a catalog item there is a field lets say ids where the ids of the user i selct form sys_user table will be populated.

 

The field names where i enter users lets suppose entered as a,b, c, d . and the output (ids)i get from these will be like a,c,d,b . The order is messing . Can someone help with this i am using script include.

         

       
        while (record.next()) {
            for (var i = 0; i < fields_len; i++) {
                arr.push(record.getValue([i]));
            }
        }
        return arr.join(" , ");
    },

6 REPLIES 6

Anurag Tripathi
Mega Patron
Mega Patron

Can you elaborate a bit?

What is your complete script include function and what is your expected output? what is fields_len

 

 

-Anurag

@Anurag Tripathi : Sure,

Script include :

functionEgName: function() {
        var table = this.getParameter('sysparm_tablename');//holds table name
        var id = this.getParameter('sysparm_ref_record');// holds the field /user (sys_user)we give in catalog item
        var fields = this.getParameter('sysparm_field_id');//holds the name i need lets say id of each user
        var firelds_arr = fields.split(',');
        var fields_len = firelds_arr.length;
        //get the record and return the value 
        var arr = [];
        var record = new GlideRecord(table);
        record.addEncodedQuery(id);
        record.query();
        while (record.next()) {
            for (var i = 0; i < fields_len; i++) {
                arr.push(record.getValue([i]));
            }
        }
        return arr.join(" , ");
    },

 

 

 

client script:

 

var emailgr = new GlideAjax('scriptIncludeName'); //name of script include
    emailgr.addParam('sysparm_name', 'functionEgName'); //name of the function in script include
    emailgr.addParam('sysparm_tablename', 'sys_user'); //table name 
    emailgr.addParam('sysparm_ref_record', " sys_idIN " + users_name); //variable 
    emailgr.addParam('sysparm_field_id', 'qid'); //  id of each user column in User table
    emailgr.getXML(setIds);

 

    function setIds(response) {
        var responseIds = response.responseXML.documentElement.getAttribute("answer");
        g_form.addInfoMessage(responseIds );
        g_form.setValue('idField', responseIds );// the ids i get here are not in the same sequence as the names i give
    }

My input is to give username and theres no limit here ,Suppose i give users as a,b,c,d

There ids are populted in corresponding field as c,a,d,b .which is not the same sequence 

You wont be able to ensure the sequence, unless you return json with key as the user_name and value as the id, or you append the user name in your return value and then extract it on the client, like at the moment you ar returning id1,1d3,1d4,1d2 but instead you return usr3#id3,usr4#id4,usr2#id2,usr1#id1

-Anurag