- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2021 03:08 PM
Hi ,
I have created a business rule to replace a line in the Description field with empty space .
It is working and that line is replaced with blank space . Now I want to remove this white space and move Up the line below it .
Please guide how to achieve it .
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-09-2021 06:42 AM
I just used the same script and it worked fine for me
Original Message
Script:
var gr = new GlideRecord('problem');
gr.get('a463aa81db41e010224a2a9a48961954');
var desc = gr.description;
gs.info('Original String->\n' + desc);
desc = desc.replace(/Legal Entity Code: LE-[A-Z]+[\d]{3}/g, '');
var arr = desc.replace(/(\r\n|\n|\r)/gm, "@").replaceAll('@',',').split(',');
var finalArr = [];
for(var i in arr){
if(arr[i] != ''){
finalArr.push(arr[i].toString());
}
}
var finalString = finalArr.join('\n');
gs.info('Final String->\n' + finalString);
gr.description = finalString;
gr.update();
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-08-2021 06:37 AM
I tried the code you provided to remove the empty line in the description .
It is not working and the empty line still remains.
I added the logs in BR and below are screenshots .
(function executeRule(current, previous /*null when async*/ ) {
var description = current.description.toString();
if (description.includes('Legal Entity Code: LE-')) {
var grdesc = new GlideRecord('sn_hr_core_case');
grdesc.addEncodedQuery('active=true^descriptionLIKELegal Entity Code: LE-');
grdesc.addQuery('sys_id', current.sys_id);
grdesc.orderByDesc('sys_created_on');
grdesc.setLimit(1);
grdesc.query();
if (grdesc.next()) {
var finalArr = [];
var value = description.replace(/Legal Entity Code: LE-[A-Z]+[\d]{3}/g, '');
var arr = value.toString().split('\r\n');
gs.info('the refined arr is :' + arr);
for (var i = 0; i < arr.length; i++) {
if (arr[i].trim() != '') {
finalArr.push(arr[i].toString());
}
}
gs.info('my final array is :' +finalArr);
grdesc.description = finalArr.toString();
grdesc.update();
}
//current.variables.u_legal_entity_code = ''
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-08-2021 06:57 AM
Hi,
update as this
if (grdesc.next()) {
var finalArr = [];
var value = description.replace(/Legal Entity Code: LE-[A-Z]+[\d]{3}/g, '');
var arr = value.toString().split('\r\n');
gs.info('the refined arr is :' + arr);
for (var i = 0; i < arr.length; i++) {
if (arr[i].trim() != '') {
finalArr.push(arr[i].toString());
}
}
gs.info('my final array is :' +finalArr);
var str = finalArr.join(',').replace(/\n/g, "");
grdesc.description = str;
grdesc.update();
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-08-2021 07:14 AM
Hi
This joining all the variables in the description field and not showing in new line.
This creates confusion for HR agents when there are many record producer variables .
please let me know if there is a way to show in new lines and remove that White space .
In below image it removed empty space and joined two variables.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-08-2021 07:59 AM
Update as this and it should work
if (grdesc.next()) {
var finalArr = [];
var value = description.replace(/Legal Entity Code: LE-[A-Z]+[\d]{3}/g, '');
var arr = value.toString().split('\r\n');
gs.info('the refined arr is :' + arr);
for (var i = 0; i < arr.length; i++) {
if (arr[i].trim() != '') {
finalArr.push(arr[i].toString());
}
}
var arr = desc.replace(/(\r\n|\n|\r)/gm, "@").replaceAll('@',',').split(',');
var finalArr1 = [];
for(var i in arr){
if(arr[i] != ''){
finalArr1.push(arr[i].toString());
}
}
grdesc.description = finalArr1.join('\n');
grdesc.update();
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-08-2021 09:16 AM
The text which need to be replaced as per original requirement is not getting removed.
Please suggest .
BR:
(function executeRule(current, previous /*null when async*/ ) {
var description = current.description.toString();
if (description.includes('Legal Entity Code: LE-')) {
var grdesc = new GlideRecord('sn_hr_core_case');
grdesc.addEncodedQuery('active=true^descriptionLIKELegal Entity Code: LE-');
grdesc.addQuery('sys_id', current.sys_id);
grdesc.orderByDesc('sys_created_on');
grdesc.setLimit(1);
grdesc.query();
if (grdesc.next()) {
var finalArr = [];
var value = description.replace(/Legal Entity Code: LE-[A-Z]+[\d]{3}/g, '');
var arr = value.toString().split('\r\n');
gs.info('the refined arr is :' + arr);
for (var i = 0; i < arr.length; i++) {
if (arr[i].trim() != '') {
finalArr.push(arr[i].toString());
}
}
var arr = desc.replace(/(\r\n|\n|\r)/gm, "@").replaceAll('@', ',').split(',');
var finalArr1 = [];
for (var i in arr) {
if (arr[i] != '') {
finalArr1.push(arr[i].toString());
}
}
grdesc.description = finalArr1.join('\n');
grdesc.update();
}
}
})(current, previous);