Delete record in Multi Row variable set when unchecking the checkbox

Sarah Austria
Giga Guru

Hi Guys,

I have a Requested For variable (itsm_requested_by), a checkbox (include_me_in_the_access) and a mrvs (users_who_need_access). When I checked the checkbox it will add the Requested For to the mrvs, then when deselecting the checkbox it should be remove from the mvrs.

find_real_file.png

 

I came across from one of the question  and was able to add the details in my mrvs by creating script include + client script (onChange)

Script Include:

var populateEmailfromList = Class.create();
populateEmailfromList .prototype = Object.extendsObject(AbstractAjaxProcessor, {

listcollector: function() {

var listValuename = [];
var userInfo = this.getParameter('sysparm_user_info');
var query = 'sys_idIN' + userInfo;
if(userInfo)
{
var gr = new GlideRecord('sys_user');
gr.addEncodedQuery(query);
gr.query();
if(gr.next()){

listValuename.push({
"name": gr.getUniqueValue('name'),
"ntid": gr.getValue('u_network_id')
});
}
}
gs.info('ARB JSON'+JSON.stringify(listValuename));
return JSON.stringify(listValuename);
},

type: 'populateEmailfromList '
});

 

 

OnChange Client Script:

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

if(newValue == ''){
g_form.clearValue('users_who_need_access'); // give name of MRVS variable set here
}

if(oldValue != newValue){
var ga = new GlideAjax('populateEmailfromList');
ga.addParam('sysparm_name', 'listcollector');
ga.addParam('sysparm_user_info', g_form.getValue('itsm_requested_by')); // give here the requestor variable name
ga.getXML(listcolleValues);
}
function listcolleValues(response) {
var val = response.responseXML.documentElement.getAttribute("answer");

if (g_form.getValue('include_me_in_the_access') == 'true') {
g_form.setValue('users_who_need_access', val); // give name of MRVS variable set here
}
else if (g_form.getValue('include_me_in_the_access') == 'false'){

var toRemove = g_form.getDisplayValue('itsm_requested_by').value;
var finalArr = [];
var parser = JSON.parse(val);
for(var i=0;i<parser.length;i++){
var obj1 = parser[i];
var newParser = JSON.parse(JSON.stringify(obj1));
if(newParser.name != toRemove){
finalArr.push(obj1);
}
}
alert('new JSON' + JSON.stringify(finalArr));
g_form.setValue('users_who_need_access',JSON.stringify(finalArr));
}


}

}

 

Unfortunately, the "else if (g_form.getValue('include_me_in_the_access') == 'false'){" in my client script is not working for me. Please help me with my script. 

 

Appreciate for any assistance. 

 

thanks,

Sarah

1 ACCEPTED SOLUTION

Hi,

you need to again perform GlideAjax when it is checked so that you get the user details and add it to existing json string

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

16 REPLIES 16

Hi,

did you check toRemove also has same sys_id

try this

if (parser[i].name.toString() != toRemove.toString()) {
alert('Pushing when value is not matched');
finalArr.push(parser[i]);
}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Ankur,

I have tried to removed the .name, it is now looks like below, and i can now received the alert.

 if (parser[i] != toRemove) {
alert('Pushing when value is not matched');
finalArr.push(parser[i]);
}

However, when I go to the last part:

alert('new JSON' + JSON.stringify(finalArr));
g_form.setValue('users_who_need_access', JSON.stringify(finalArr))

it retained my name and the rest of the records removed aside from my name. 

yes. toRemove has the same sys_id with parser[i].

Hi,

can you share what's coming in this

alert(val);

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Ankur,

I am now able to make it work. I just created one catalog client script (onChange). I removed the script include and just populate the details in the hidden variable. So the client script will looks like below.

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

var selfDetails = [];
var details = '';

var obj = {};

obj["name"] = g_form.getValue('itsm_requested_by');
obj["ntid"] = g_form.getValue('ntid1');
selfDetails.push(obj);
details = JSON.stringify(selfDetails);


if (newValue != '' && g_form.getValue('include_me_in_the_access').toString() == 'true') {
g_form.setValue('users_who_need_access', details);
}
else if (g_form.getValue('include_me_in_the_access').toString() == 'false'){

var details1 = g_form.getValue('users_who_need_access');
var toRemove = g_form.getValue('itsm_requested_by');
//alert(toRemove);
var finalArr = [];
var parser = JSON.parse(details1);
for(var i=0;i<parser.length;i++){
//alert('Name is ' + parser[i].name.toString());
var obj1 = parser[i];
var newParser = JSON.parse(JSON.stringify(obj1));
if(newParser.name.toString() != toRemove.toString()){
//alert('Pushing when value is not matched' + parser[i].name.toString());
finalArr.push(obj1);
}

}
//alert('new JSON' + JSON.stringify(finalArr));
g_form.setValue('users_who_need_access',JSON.stringify(finalArr));
}

//g_form.clearValue('ntid1');

}

 

 

However, i have more issue. When I tried to select the checkbox it populates my details in the mvrs, but the rest of the initial records cleared out. Do you know how I fix this part?

if (newValue != '' && g_form.getValue('include_me_in_the_access').toString() == 'true') {
g_form.setValue('users_who_need_access', details);
}