Setting list field values in a client script

Daniel Gleason
Giga Expert

Hey all,

 

I am trying to set a list field via a client script currently by setting it equal to a comma separated list of sys_ids, however it doesn't seem to be working. 

 

The client script is onSubmit and code is here:

 

function onSubmit() {

    var addedArr = [];
    var removedArr = [];
    var oldlist = g_scratchpad.nodes? g_scratchpad.nodes.split(',') : [] ; // get the existing list of nodes from display business rule
    var newList =g_form.getValue('nodes').split(',');
    for (var k = 0; k < newList.length; k++) {
        if (oldlist.join(',').indexOf(newList[k]) == -1) { //on new but not old
            addedArr.push(newList[k]);
			
        }
    }
    alert('addedArr is ' + addedArr.toString());
    var ga = new GlideAjax('xxxxxxxxxxxxxxxxx');
    ga.addParam('sysparm_name', 'checkUserTasks');
    ga.addParam('sysparm_user', g_form.getValue("user"));
    ga.addParam('sysparm_newNodeArr', JSON.stringify(addedArr));
    ga.getXML(message);
}

function message(response) {

    var answer = response.responseXML.documentElement.getAttribute("answer");
	alert('anwer value is ' + answer);
    if (answer == "true") {
        alert('You have added a node that is already associated with a user task for this user.');
		alert('g_scratchpad,nodes is ' + g_scratchpad.nodes);
		g_form.setValue('nodes', g_scratchpad.nodes); // comma separated list of sys_ids ( g_scratchpad.nodes it is getting the value from the list field, multiple sys_ids via a display br, I am tryin to set). 
    }

 

I'm getting the list field value onLoad from the display business rule and just am trying to set it back to the original field value if a certain condition is met in the Script include I use in via the GlideAjax api. 

I have not yet been able to set the list field, "nodes, to the original value onLoad / or set it at all.

Any help would be must appreciated!

Thanks,

Dan

1 ACCEPTED SOLUTION

Daniel Gleason
Giga Expert

FYI, i changed this to an onchange client script and it now works. 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    };
    var addedArr = [];
    var removedArr = [];
    var oldlist = g_scratchpad.nodes ? g_scratchpad.nodes.split(',') : []; // get the existing list of nodes from display business rule
    var newList = newValue.split(',');
    for (var k = 0; k < newList.length; k++) {
        if (oldlist.join(',').indexOf(newList[k]) == -1) { //on new but not old
            addedArr.push(newList[k]);

        }
    }
    var ga = new GlideAjax('x_ipnll_ipc_app.IPCAppUtils');
    ga.addParam('sysparm_name', 'checkUserTasks');
    ga.addParam('sysparm_user', g_form.getValue("user"));
    ga.addParam('sysparm_newNodeArr', JSON.stringify(addedArr));
    ga.getXML(message);

}


function message(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    if (answer == "true") {
        alert('You have added a node that is already associated with a user task for this user.');
		g_form.setValue('nodes', g_scratchpad.nodes);
		
    }

}

View solution in original post

1 REPLY 1

Daniel Gleason
Giga Expert

FYI, i changed this to an onchange client script and it now works. 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    };
    var addedArr = [];
    var removedArr = [];
    var oldlist = g_scratchpad.nodes ? g_scratchpad.nodes.split(',') : []; // get the existing list of nodes from display business rule
    var newList = newValue.split(',');
    for (var k = 0; k < newList.length; k++) {
        if (oldlist.join(',').indexOf(newList[k]) == -1) { //on new but not old
            addedArr.push(newList[k]);

        }
    }
    var ga = new GlideAjax('x_ipnll_ipc_app.IPCAppUtils');
    ga.addParam('sysparm_name', 'checkUserTasks');
    ga.addParam('sysparm_user', g_form.getValue("user"));
    ga.addParam('sysparm_newNodeArr', JSON.stringify(addedArr));
    ga.getXML(message);

}


function message(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    if (answer == "true") {
        alert('You have added a node that is already associated with a user task for this user.');
		g_form.setValue('nodes', g_scratchpad.nodes);
		
    }

}