Display values as List on a Multiple Line text variable

Insider
Giga Guru

I have a variable called approvals.

On which i show all the list of approvals who approved that current case.

as of now i am seeing data as 

approver1. approver2. approver3. approver4

But i want the data to be shown as 
approver1
approver2

approver3

approver4

 

for info i am using an array to store this data, so could it be possible to achieve this functionality with in array? should i take a new approach?

 

Please suggest

1 ACCEPTED SOLUTION

Is that a dot or a comma, I can't tell.  Try this instead:

rec.variables.first_approver = approversList.join("");

To convert the array to a string by specifying the character that comes between the members.  If it doesn't like this (no character), try taking the '\n' out of the push then join the array on "\n".

View solution in original post

8 REPLIES 8

Sankar N
Kilo Guru

One approach is to use the join() method of the array to convert the array into a string with each element separated by a delimiter (e.g., a comma). Then, you can use the replace() method to replace the delimiter with a line break character (i.e., '\n').

 

// Assuming 'approvals' is an array containing the list of approvers
var approverList = approvals.join(', '); // Convert the array to a string with comma-separated values
approverList = approverList.replace(/,\s/g, '\n'); // Replace the comma with a line break character
gs.info(approverList); // Display the formatted approver list in the ServiceNow system log

 

This code will replace all occurrences of the comma followed by a space with a line break character. The resulting string will be displayed in the ServiceNow system log.

Alternatively, you can use a loop to iterate through the array and concatenate each element with a line break character.

 

// Assuming 'approvals' is an array containing the list of approvers
var approverList = '';
for (var i = 0; i < approvals.length; i++) {
  approverList += approvals[i] + '\n'; // Concatenate the current approver with a line break character
}
gs.info(approverList); // Display the formatted approver list in the ServiceNow system log

This code will loop through the array and concatenate each element with a line break character.

I am currently using,

 var approversList = [];

    while (gr.next()) {

//gs.addInfoMessage(gr.state);

if(gr.state == 'approved'){

 

        approversList.push(gr.approver.getDisplayValue() + ' - ' + gr.short_description) + ' ';

 

    }

}

 

    var rec = new GlideRecord('x_gskgs_gsf_case_1_finance_procurement_case');

    rec.get(current.document_id);

    rec.variables.first_approver = approversList.toString();

    rec.update();

Where in the end i need the list of approvers on first_approver field but i am getting them side by side.

Brad Bowman
Kilo Patron
Kilo Patron

You can pass '\n' into the variable for a new line, so when you are populating the variable with the array, if it's a string and not an actual array type script variable I would try to first split the string into an array on the . or , that it currently contains, then join it specifying \n instead.  If that doesn't work split the string into an actual array then use a for loop to write each member to a new string script variable, followed by \n in each iteration, then populate the Mutli Line Text variable with the string. 

I am using,

 var approversList = [];

    while (gr.next()) {

//gs.addInfoMessage(gr.state);

if(gr.state == 'approved'){

 

        approversList.push(gr.approver.getDisplayValue() + ' - ' + gr.short_description) + ' ';

 

    }

}

 

    var rec = new GlideRecord('x_gskgs_gsf_case_1_finance_procurement_case');

    rec.get(current.document_id);

    rec.variables.first_approver = approversList.toString();

    rec.update();

Where in the end i need the list of approvers on first_approver field but i am getting them side by side.