Display array values on new line, when copying list collector to text type variable.

Baggies
Kilo Guru

I have a script, and script include that I have found on the forum, and works well (many thanks). The requirement is to take the values from a list collector, and display them in a multi line text variable.

The results are being displayed on one line, no spaces, and comma separated. I would like each value on a new line. I have tried without success to add \n ( or is it /n) after the split. But nothing seems to work?

here is the client script, and script include, any suggestions welcome. Appreciate all replies, thanks, Mark S.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//Array of the current values selected
var newValueArr = newValue.split(',');
//Array of the previous values selected
var oldValueArr = oldValue.split(',');

//Values that have been added
var newSelections = fieldValueCheck(newValueArr, oldValueArr);
//Values that have been removed
var oldSelections = fieldValueCheck(oldValueArr, newValueArr);

var getNames = new GlideAjax('getChangedValueNames');
getNames.addParam('sysparm_name', 'getNames');
getNames.addParam('addedValue', newSelections + '');
getNames.addParam('removedValue', oldSelections + '');
getNames.getXMLAnswer(getAnswer);
}

function getAnswer(answer) {
answer = JSON.parse(answer);
console.log(answer);
g_form.setValue('text_var_1', answer.addedValue);
// g_form.setValue('removed_server', answer.removedValue);
}

function fieldValueCheck(array1, array2) {
var rtnArr = [];
for (var first = 0; first < array1.length; first++) {
if (array2.indexOf(array1[first]) == -1) {
rtnArr.push(array1[first]);
}
}

return rtnArr;
}

 

 

var getChangedValueNames = Class.create();
getChangedValueNames.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getNames: function(){
var newValues = this._getRecords(this.getParameter('addedValue'));
//var oldValues = this._getRecords(this.getParameter('removedValue'));
var rtnObj = {
addedValue:newValues,
//removedValue:oldValues
};
var rtnStng = JSON.stringify(rtnObj);
return rtnStng;
},
_getRecords: function(arrayList){
//Change 'sys_user' to the table you are refencing in your field
var rtnArr = [];
var record = new GlideRecord('sys_user');
record.addEncodedQuery('sys_idIN' + arrayList);
record.query();
while(record.next()){
rtnArr.push(record.name.toString());
}
return rtnArr + '';
},
type: 'getChangedValueNames'
});
1 ACCEPTED SOLUTION

Jon Barnes
Kilo Sage
Try replacing the last line of your _getRecords with: return rtnArr.join(‘\n’);

View solution in original post

4 REPLIES 4

Jon Barnes
Kilo Sage
Try replacing the last line of your _getRecords with: return rtnArr.join(‘\n’);

Thank you John, that worked a treat. I should have looked the script include. Have a great week.

Malanbee
Tera Contributor
Hi Baggies
 
i'm trying to implement the above functionality but i'm getting the sys id instead of value, can you please help me with it

 

Hello. This was a while ago. Maybe post your script here, and a screenshot of what you are seeing. Once I get my dev instance back, I'll take a look what I had. Always offline when you need it

find_real_file.png