Merge Tickets - Copy work notes

gaidem
ServiceNow Employee
ServiceNow Employee

I'm being lazy here... Curious if anyone has previously written a script to transfer all the work notes from multiple tickets into one. This is for a "merge" functionality.

If not, I'll write something and post it here for future reference.

Thanks!

5 REPLIES 5

Joe_Employee
Tera Contributor

From the searching I have done it would appear you'd be the first to do this and post it. I look forward to your results. If you get stuck let the community know and maybe we can lend some assistance.


gaidem
ServiceNow Employee
ServiceNow Employee

Here's my script. It sort of works, but has some problems. The tickets I query do not get updated. Only the notes from the first ticket are applied, but if I comment out the lines updating the queried ticket all the tickets notes get applied.



mergeIncidents();
function mergeIncidents(){

var inc_sys_id = current.sys_id;
//glide_list of tickets
var incMRG = current.u_merge_tickets;
//var incMRGstr = incMRG.toString();
var incarry = incMRG.split(",");

for (var i=0; i < incarry.length; i++) {
//query for the tickets in that glide_list
var incRec = new GlideRecord('task');
//incRec.initialize();
incRec.addQuery('sys_id',incarry<i>);
//don't query tickets that have been merged in the past
incRec.addQuery('u_new_ticket','!=',inc_sys_id);
incRec.query();
while(incRec.next()){

//get all journal notes and split them
var notes = incRec.work_notes.getJournalEntry(-1);
var na = notes.split("\n\n");
for (var t = 0; t < na.length; t++){
//insert notes on current ticket
current.work_notes =(na[t]);
current.update();
}
//update the queried ticket to set it to closed and to record the new ticket ID
incRec.u_new_ticket = inc_sys_id;
incRec.incident_state ='7';
incRec.update();
}

}
}


gaidem
ServiceNow Employee
ServiceNow Employee

I think I may have gotten it. Need to go back and clean up some stuff, but here's what it's looking like:
Business Rule
Before Insert or Update
Condition: current.u_merge_tickets!=''&&current.u_merge_tickets.changes()
Script:



mergeIncidents();
function mergeIncidents(){
//current.u_merge_tickets!=''& ¤t.u_merge_tickets.changes()
//gs.log("merging tickets");
var inc_sys_id = current.sys_id;
var incMRG = current.u_merge_tickets;
var incMRGstr = incMRG.toString();
var incarry = incMRGstr.split(",");

for (var i=0; i < incarry.length; i++) {
var incRec2 = new GlideRecord('incident');
//incRec.initialize();
incRec2.addQuery('sys_id',incarry<i>);
//gs.log("ticket is: "+incarry<i>);
incRec2.addQuery('u_new_ticket','!=',inc_sys_id);
incRec2.query();
while(incRec2.next()){
//gs.log("found ticket");
closeMergedIncident(incRec2.sys_id,inc_sys_id);


var notes = incRec2.work_notes.getJournalEntry(-1);
var na = notes.split("\n\n");
// for (var t = 0; t < na.length; t++){
//current.work_notes =(na[t]);
current.work_notes = notes;
//current.update();

}

}
}
function closeMergedIncident(incID3,parent){
//gs.log("updating tickets");
var incRec3 = new GlideRecord('incident');
incRec3.addQuery('sys_id',incID3);
incRec3.query();
if(incRec3.next()){
incRec3.u_new_ticket = parent;
incRec3.incident_state ='7';
incRec3.update();
}}


This is great...unfortunately it is hard for me to locate the relevant part. Any ideas how to make it client callable (e.g. as a UI action form button) and operate it on related incidents (that are attached via a related list)?

Thx.