I want to remove the comma "," at the end of the list.Can anyone help me on this!!Thanks in advance.

Kumar60
Kilo Expert

I have a requirement to get the "Affected CI's"(task_ci) list attached to the incident in a client template.By using the below script,I am getting the list of Affceted CI's.But to the last value the comma is getting concatenated.I tried to remove in many ways as shown in the script below(commented),but the last operator(highlighted in Red) is not getting remove.

Below is the mail script I used and Result I am getting:

Result:

Service Impacted:  CDN_ITS_AlfrescoRetrievePropertiesService_PROD,AWD - FORMS, DocGenDashboard (Member statements)- SL - UAT, GIS Messaging - Production - CA, IGP Captive Reporting - INST - SIT,

Email Script:

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */ event) {

// Add your code here
var gr1=new GlideRecord('task_ci');
var a="";
gr1.addQuery('task',current.incident_alert.source_incident.sys_id);
gr1.query();
while(gr1.next()){
//gs.print(gr1.name.getDisplayValue());
//template.print(gr1.ci_item.getDisplayValue() + "<br/>");
a=gr1.ci_item.getDisplayValue() + ",";
// a=a.substr(0, a.length-1);
// var lch = a.slice(-1);
// if(lch == ','){
// a=a.slice(0,-1);
// }
//var newa = a.length-1;
//a=newa.replace(",","");
// template.print(newa.replace(/.$/,""));
template.print(a);
}
/*function removeLastComma(strng){
var n=strng.lastIndexOf(",");
var a=strng.substring(0,n);
return a;
}*/


})(current, template, email, email_action, event);

 

Thanks in Advance.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Kumar,

can you try using an array instead?

 

script below

var arr = [];

var gr1=new GlideRecord('task_ci');
gr1.addQuery('task',current.incident_alert.source_incident.sys_id);
gr1.query();
while(gr1.next()){

var value = gr1.ci_item.getDisplayValue();

arr.push(value.toString())

}

template.print(arr);

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

View solution in original post

4 REPLIES 4

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Have you considered using a array? This way you don't have to deal with a trailing seperator. Dig in to things like .push / .join:

var list = [];

list.push(gr1.ci_item.getDisplayValue());

list.join();

(the push would be done in your while)

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

---

LinkedIn

 

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Kumar,

can you try using an array instead?

 

script below

var arr = [];

var gr1=new GlideRecord('task_ci');
gr1.addQuery('task',current.incident_alert.source_incident.sys_id);
gr1.query();
while(gr1.next()){

var value = gr1.ci_item.getDisplayValue();

arr.push(value.toString())

}

template.print(arr);

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

siva_
Giga Guru

Using an array and join would definetely help you to reduce the redundant script 

 

Thanks,

Siva

Mark this response as correct if that really helps

Kumar60
Kilo Expert

Thanks All for the help.By using the Array it worked.