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

Is your script include and client script in HR scope?

Regards
Harish

Yes Harish.

I tried in PDI with script include and client script record producer all in HR scope works fine for me. Attached images for same

Regards
Harish

Yes Harish, in my PDI its working. But in my instance its not working.

 

Logs also not capturing.

Thanks harish. I tried this with global.Abstarct.

Now its working perfectly. Thanks much.