Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

To add line break in Script include

trisha1
Kilo Contributor

I have one notification for RITM's In that i need to add line break after every comma for description. Below is the script include part. Please suggest.

 

default_sc_req_item_rows: function() {
this.row('Number', '${number}');
this.row('Short description', '${short_description}');
this.row('Description', '${Description}');
},

4 REPLIES 4

Gunjan Kiratkar
Kilo Patron
Kilo Patron

Hi ,

You can use below code for that. 

 

var formattedString = yourString.split(",").join("\n");

 

Regards,

Gunjan


Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

please share complete script and expected output?

Regards
Ankur

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

script include:

var xx_email_notification_content_blocks = Class.create();
xx_email_notification_content_blocks.prototype = {

initialize: function() {
this.str = new xx_StringUtil();
},

/* Style definitions */

global_style: 'font-family: Arial,sans-serif; font-size: 10pt;',
header_style: 'font-size: 150%; color: rgb(244, 119, 53); background-color: white; border-color: white white rgb(244,119,53); border-style: solid; border-bottom-width: 1px;',
table_style: 'border-collapse: collapse; width: 100%; border: 0;',
td_style: 'border-bottom-width: 1px; border-bottom-color: #ccc; border-bottom-style: solid; padding: 0.5em 1em; vertical-align: top; white-space: pre;',

/* General functions */

header: function(text) {
var html = '<h1 style="' + this.global_style + this.header_style + '">${sys_class_name} ' + text + '</h1>';
template.print(html);
},

header_no_table: function(text) {
var html = '<h1 style="' + this.global_style + this.header_style + '">' + text + '</h1>';
template.print(html);
},

table_start: function() {
var html = '<table border="0" style="' + this.global_style + this.table_style + '"><tbody>';
template.print(html);
},

table_end: function() {
var html = '</tbody></table>';
template.print(html);
},

row: function(label, value, style) {
if (typeof style === 'undefined') {
style = '';
}
var html = '<tr><td style="' + this.td_style + style + '">' + label + ':</td><td style="' + this.td_style + style + '"><div>' + value + '</div></td></tr>';
template.print(html);
},

row_wrap: function(label, value, style) {
if (typeof style === 'undefined') {
style = '';
}
var html = '<tr><td style="' + this.td_style + style + '">' + label + ':</td><td style="' + this.td_style + style + '"><div>' + value + '</div></td></tr>';
template.print(html.replace(/\n/, '<br />'));
},

row_blank: function() {
var html = '<tr><td style="' + this.td_style + '"> </td><td style="' + this.td_style + '"><div> </div></td></tr>';
template.print(html);
},

link: function() {
var html = '<p style="' + this.global_style + '">Click here to view: ${URI}</p>';
template.print(html);
},

linkCMS: function() {
html = gs.getMessage('<p style="{0}">Click here to view: {1}</p>', [
this.global_style,
this.linkPortalPlain()
]);
template.print(html);
},

linkPortalPlain: function() {
var spLinkUtil = new xx_SpLinkGenerator();
return spLinkUtil.isEnabled() ? spLinkUtil.getLink(current) : '${CMS_URI+selfservice/default_content}';
},

notificationSettings: function() {
var html = '<p style="' + this.global_style + 'text-align: center; margin-top: 4em; color: #888;' + '">Adjust your ServiceNow email notification settings <a href="/nav_to.do?uri=/notification_preferences.do">here</a>.</p>';
template.print(html);
},

paragraph: function(text) {
var html = gs.getMessage('<p style="{1}">{0}</p>',
[text, this.global_style]);
template.print(html);
},

generic: function(text) {
var html = '<div style="' + this.global_style + '">' + text + '</div>';
template.print(html);
},

/* Default rows */

default_task_rows: function() {
this.row('Number', '${number}');
this.row('Short description', '${short_description}');
this.row('Description', '${description}');
},

default_sc_req_item_rows: function() {
this.row('Number', '${number}');
this.row('Short description', '${short_description}');
var formattedString = '${description}';
formattedString = formattedString.split(",").join("\n");
this.row('Description', 'formattedString');
// this.row('Description', '${u_description_html}'); //deactivated by INC0488587
},

default_sc_request_rows: function() {
this.row('Number', '${number}');
this.row('Short description', '${short_description}');
this.row('Description', '${description}');
},

default_sc_task_rows: function() {
this.row('Number', '${number}');
this.row('Short description', '${short_description}');
if (current.sys_class_name.toString() === 'u_hr_service_task') {
this.row('Requested for', '${u_requested_for}');
} else {
this.row('Request Item', '${request_item}');
this.row('Requested for', '${request_item.u_requested_for}');
}
this.row('Description', '${description}');
},

default_sysapproval_approver_rows: function() {
var requester = current.sysapproval.u_requested_for ||
current.sysapproval.requested_by ||
current.sysapproval.opened_by;
var opened_by = current.sysapproval.opened_by;

var requesterStr = requester.getDisplayValue();
if (requester.toString() !== opened_by.toString()) {
requesterStr = this.str.format('%2 on behalf of %1',
requesterStr, opened_by.getDisplayValue());
}
this.row('Requester', requesterStr);
this.row('Short description', '${sysapproval.short_description}');
this.row('Description', '${sysapproval.description}');
this.row('Severity', '${sysapproval.priority}');
},

 

 

default_security_task_rows: function() {
this.row('Number', '${number}');
this.row('Short description', '${short_description}');
this.row('Severity', '${priority}');
this.row('Category', '${u_category}');
this.row('Subcategory', '${u_subcategory}');
},

 

/* Special functions */

sysapproval_approver_footer: function() {
this.generic('<h2> </h2>');
this.table_start();
template.print('<tr><td style="background-color: #aea; border: 1px solid #8c8; padding: 0.5em 1em;">${mailto:mailto.approval}</td><td style="border: 0px;"> </td><td style="background-color: #fcc; border: 1px solid #daa; padding: 0.5em 1em;">${mailto:mailto.rejection}</td></tr>');
this.table_end();
this.generic('<p style="font-size: 90%;">To approve the Request please send the email which will appear after you have clicked the link above!<br />If you want to reject this request, please provide a rejection reason in the e-mail body (Note: please do not delete the reference line below: Ref:MSGxxxx).<br /><a href="https://globalfoundries.service-now.com/$h.do?sysparm_doc=UGDOC0010009">How to approve multiple requests at once</a></p>');
this.generic('<p style="border-top: 1px solid #ccc;">Click here to view Approval Request: ${URI}<br />Click here to view ${sysapproval.sys_class_name}: ${sysapproval.URI}</p>');
},
//Footer for Non-Task Table
sysapproval_documentid_approver_footer: function() {
this.generic('<h2> </h2>');
this.table_start();
template.print('<tr><td style="background-color: #aea; border: 1px solid #8c8; padding: 0.5em 1em;">${mailto:mailto.approval}</td><td style="border: 0px;"> </td><td style="background-color: #fcc; border: 1px solid #daa; padding: 0.5em 1em;">${mailto:mailto.rejection}</td></tr>');
this.table_end();
this.generic('<p style="font-size: 90%;">To approve the Request please send the email which will appear after you have clicked the link above!<br />If you want to reject this request, please provide a rejection reason in the e-mail body (Note: please do not delete the reference line below: Ref:MSGxxxx).<br /><a href="https://globalfoundries.service-now.com/$h.do?sysparm_doc=UGDOC0010009">How to approve multiple requests at once</a></p>');
this.generic('<p style="border-top: 1px solid #ccc;">Click here to view Approval Request ${URI}<br />Click here to view ${document_id} ${document_id.URI}</p>');
},

major_incident_report_rows: function() {
var webConf = '6cf3d1cf0f48530098374bfce1050e07'; // Communication Device CI
var opentime = GlideDateTime.subtract(new GlideDateTime(current.opened_at), new GlideDateTime());

this.row('Current state', '${incident_state}');
this.row('Issue description', '${short_description}');
this.row('Affected Location', '${location} > ${u_fab_env_inc}'); //added by CHG0034121
this.row_wrap('Impacted Service', '${u_service_area} > ${u_service_category} > ${u_service_subcategroy} > ${u_business_service}'); //added by CHG0034121
this.row_wrap('Business impact', '${u_business_impact}');
this.row_wrap('Activity log', '${u_activity_log}');
if (current.u_bridge_call_informarion.toString() === webConf) {
this.row('Web conference', this.str.format('<a href="%1" target="_blank">%1</a>', current.u_status_conference_url));
} else {
this.row('Bridge call information', '${u_bridge_call_informarion.comments}');
}
this.row('Time since INC was opened', opentime.getDisplayValue());
this.row('Incident owner', '${assignment_group} (${assigned_to})');
this.row_wrap('Root cause analysis', '${problem_id.close_notes}');
},

type: 'xx_email_notification_content_blocks'
};

trisha1
Kilo Contributor

and this is the script include xx_StringUtil:

var xx_StringUtil = Class.create();
xx_StringUtil.prototype = {
initialize: function() {},


format: function(str) {
var args = arguments;


var regex = /(^|[^%])%(\d+)/g;

return str.replace(regex, function(match, precedingChar, n) {
return typeof args[n] !== 'undefined' ?
(precedingChar + args[n]) :
match;
});
},

makeUserFriendly: function(value) {
switch (value.toString()) {
case '':
return '(empty)';
case 'true':
return 'yes';
case 'false':
return 'no';
default:
return value.toString();
}
},


parseHtmlTable: function(input) {

var htmlTable = input.toString().match(/<TABLE>.+<\/TABLE>/i);
if (!htmlTable || !htmlTable.length) return [];

var rows = new XMLHelper(htmlTable[0]).toObject().tr;
if (!rows || !rows.length) return [];

var header = rows.shift().th;
if (!header || !rows.every(function(row) {
return row.td && row.td.length === header.length;
})) {
return [];
} else {
header = header.map(function(columnName) {
return columnName.toLowerCase().replace(/\s/g, '_');
});
}

return rows.map(function(row) {
return row.td.reduce(function(obj, value, index) {
obj[header[index]] = value;
return obj;
}, {});
});
},


type: 'xx_StringUtil'
};