Script include response

servicenow14710
Tera Expert

In one catalog item i need to populate Ids(ID field) ids of users (NAME field)sys_user table.

So when i type users in the name filed lets say user1,user2,user3,user4. The corresponding ids are populated as(Id4,Id3,Id1,Id2) without sequence i mentioned in the names field, as it should be (Id1,Id2,Id3,id4).

For this i'm using client script to send the names field,From script include im getting the response.

 

 

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 name_ref_str = g_form.getValue('name_1');//name_1 is backend name of name field 

var name_ref_arr = getArray(name_ref_str);
    g_form.setValue('requested_for', name_ref_arr[0]);

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 " + name_ref_str); 
    emailgr.addParam('sysparm_field_name_needed', 'user_name'); // user_name column is the 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 );// idField where we store the response in catalog ,the ids i get here are not in the same sequence as the names i give
    }

3 REPLIES 3

Ratnakar7
Mega Sage
Mega Sage

Hi @servicenow14710 ,

 

To ensure that the IDs are returned in the same sequence as the names, you can modify your script include as follows:

 

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 of each user
    var fields_arr = fields.split(','); // split the fields by comma to get an array
    var fields_len = fields_arr.length;
    var arr = [];

    // get the records based on the provided names and add their IDs to the array
    var record = new GlideRecord(table);
    record.addEncodedQuery(id);
    record.query();
    while (record.next()) {
        var fieldName = record.getValue('name'); // assuming the field storing the names is 'name'
        var index = fields_arr.indexOf(fieldName);
        if (index > -1) {
            var idValue = record.getValue(fields_arr[index]);
            arr[index] = idValue;
        }
    }

    return arr.join(" , ");
}

 

By modifying the script include, it ensures that the IDs are stored in the array arr at the corresponding index of the names provided in the catalog item. This will ensure that the IDs are returned in the same sequence as the names.

 

If my response was helpful in resolving the issue, please consider accepting it as a solution by clicking on the Accept solution button and giving it a thumbs up 👍. This will benefit others who may have a similar question in the future.

 

Thank you!

Ratnakar

@Ratnakar7 : Thanks ratnakar,

I tried modifying the script include accordingly but it didnt work.

From script include i cannot have access to the name i give in the form right, canyou please explain this line. Appreciate so much for your time. Thanks.

        var fieldName = record.getValue('name'); // assuming the field storing the names is 'name'

 

Hi @servicenow14710 ,

 

The line var fieldName = record.getValue('name'); assumes that there is a field named 'name' in the table specified by sysparm_tablename.

If the names are not stored in a field named 'name', you'll need to update that line with the actual field name where the names are stored in the sys_user table.

For example, if the names are stored in a field named 'user_name', you would modify the line as follows:

 

var fieldName = record.getValue('user_name');

 

Ensure that you replace 'user_name' with the correct field name in your environment. This line is used to compare the retrieved name with the provided names and determine the corresponding index in the fields_arr array.

 

Thanks,

Ratnakar