Copying List Collector Values to Multiline Text Field.

snow_p
Tera Contributor

Hi All,

In a requirement, need to copy List Collector Values into Multiline text field. For this, I created client script and script include. This is on a table when creating a new record on selection of values in list collector, need them to copy into multiline text.

Client Script:

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

var grpList = g_form.getValue('group_id');
alert(grpList);
var ga = new GlideAjax('grpValues');
ga.addParam('sysparm_name','grpDetails');
ga.addParam('sysparm_record',grpList);
ga.getXML(CallBack);

function CallBack(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('grp_names', answer); 
alert(answer);
}
}

Script Include:

var grpValues = Class.create();
grpValues.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
grpDetails: function(){
var grpVal = [];
var grpList = this.getParameter('sysparm_record');
var Value = grpList.split(',');


for(var i=0; i< Value.length; i++){
var names = new GlideRecord('u_groups'); 
names.addQuery('sys_id',Value[i]);
names.query();
while(names.next())
{

grpVal.push(names.grp_name+''); 

}

}
return grpVal.join();
},
    type: 'grpValues'
});

I'm getting selected list values in alert but they are not populating into multiline text and getting null alert 

1 ACCEPTED SOLUTION

@Nani.P 

when the last value is removed the newValue is empty

so the onchange client script was not executing as the newValue check was present along with the isLoading

please update as below and it should work fine

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

// when newvalue is empty/cleared

if(newValue == ''){

g_form.clearValue('grp_names'); // clear the values

}


g_form.clearValue('grp_names'); // clear the values

var grpList = g_form.getValue('group_id');
alert(grpList);
var ga = new GlideAjax('grpValues');
ga.addParam('sysparm_name','grpDetails');
ga.addParam('sysparm_record',grpList);
ga.getXML(CallBack);

function CallBack(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('grp_names', answer.toString().split(',').join('\n'));
alert(answer);
}
}

Regards
Ankur

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

View solution in original post

22 REPLIES 22

Hi Ankur, it is working now. Thanks for helping me in fixing the issue.

Got some issue, When I select more than 1 group in list collector and remove them, the same is reflecting in multiline text field but for the last group is still holding in multiline text even it is removed in list collector.

what part of the script need to change ? and also can I put each selected values in multiline text in new line ?

@Nani.P 

Glad to know that my comment helped.

Please remember to mark my response as correct and helpful if I was able to help you.

the onChange will work even after you remove the values

whenever value is changed first clear the multi line text and then set

yes you can add that in new line as below

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

g_form.clearValue('grp_names'); // clear the values

var grpList = g_form.getValue('group_id');
alert(grpList);
var ga = new GlideAjax('grpValues');
ga.addParam('sysparm_name','grpDetails');
ga.addParam('sysparm_record',grpList);
ga.getXML(CallBack);

function CallBack(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('grp_names', answer.toString().split(',').join('\n'));
alert(answer);
}
}

Regards
Ankur

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

Example: I removed all the selected values but still the last removed is in multiline text.

 

@Nani.P 

I believe your question is answered as per my script suggestion.

Let me know if I have answered your question.

If so, please mark my response as correct & helpful so that this thread can be closed and others can be benefited by this.

Regards
Ankur

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

Hi Ankur,

The new line is working and still the values are not clearing off from the multiline text. If I add 3 values in list collector and started removing, the same is happening in multiline but when removed last value, the last value is still in multiline text.

 

check below screenshot where list collector is empty but one value left in multiline text