Setvalue is not updating properly in onchange for multiline text field type

Bala13
Kilo Guru

Hi,

I am writing a script to populate the values from list collector to Multi line text field using catalog client script onchange function.

But, when the iteration is happening it is overriding the first name and updating 2nd time. Please help me to get this fixed.

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var reqNam = [];
    var reqForList = g_form.getValue('requestedfor');
    alert("Req list " + reqForList);

    var reqFor = reqForList.split(',');
    alert("Requested for" + " " + reqFor);

    for (i = 0; i < reqFor.length; i++) {
        var gr = new GlideRecord('sys_user');
        gr.addQuery('sys_id', reqFor[i]);
        gr.query(myCallbackFunction);

        function myCallbackFunction(gr) {
            while(gr.next()) {

                reqNam.push(gr.name);
                alert("Check name" + "--" + reqNam);
            }
 g_form.setValue('u_request_roles_list', reqNam);
        }

    }
   


}
1 ACCEPTED SOLUTION

Harish KM
Kilo Patron
Kilo Patron

Hi Bala,

 

Here is the working code. Just change your field names and tablename in script include


   

getListCollector: function() {
        var array = [];
        var sysID = this.getParameter('sysparm_id'); // get values from client script
        
            gs.info("listArray sysID"+sysID);
    
        var user = new GlideRecord('sys_user');
        user.addQuery('sys_id','IN',sysID); // sysid contains list collector values
        user.query();
        while (user.next()) {
           array.push(user.name.toString());
        }
            gs.info("Array"+array);
            return array.join(','); // return array
   
    },

 

Client SCript:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
  
    var ga = new GlideAjax('listCollector'); // SCript Include name
    ga.addParam('sysparm_name', 'getListCollector'); // function name in SCript includes
    ga.addParam('sysparm_id', newValue);// pass list collector values to script include
    ga.getXML(getResponse);
    
}

    function getResponse(response){
        var answer= response.responseXML.documentElement.getAttribute("answer");
        alert("answer"+answer);
        g_form.setValue('test',answer); // set your variable name 
        
        
        
    }

Regards
Harish

View solution in original post

29 REPLIES 29

Using GlideRecord is not recommended in client scripts + without a callback function is a complete no no. Not the best practice.

you can use the same logic and convert it to script include.

Regards
Harish

suvro
Mega Sage

Try this --> Set Value outside loop 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var reqNam = [];
    var reqForList = g_form.getValue('requestedfor');
    alert("Req list " + reqForList);

    var reqFor = reqForList.split(',');
    alert("Requested for" + " " + reqFor);

    for (i = 0; i < reqFor.length; i++) {
        var gr = new GlideRecord('sys_user');
        gr.addQuery('sys_id', reqFor[i]);
        gr.query(myCallbackFunction);
}

        function myCallbackFunction(gr) {
            while(gr.next()) {

                reqNam.push(gr.name.toString());
                alert("Check name" + "--" + reqNam);
            }

        }

g_form.setValue('u_request_roles_list', reqNam);
 
    
   


}

Hi Survo,

I tested the same. Now no name is getting updated in variable Survo.

Is there any chances to use the same script from server side like glideajax and using client script can we set this?

Please can u help me to update using script include and client script to get this worked?

Follow this article

https://community.servicenow.com/community?id=community_article&sys_id=c77ff0dddbbb8190847d5ac2ca96198a

Send the reqForList as a parameter rest of the logic should be implemented in script include.