Close an Incident from a UI Action
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-19-2024 01:35 AM
Hi Community,
I'm looking to implement a project from the Share site for converting incidents to requests, looking at the script it looks like the script deletes the incident after the request is created.
Script URL: Incident Converter - Share | ServiceNow Developers
I'd like to change this so that it closes the incident with some resolution notes but am unsure on how to do this from within the UI action.
Please see script below (I've made the part I want to change bold)
var increq = new GlideRecord('sc_request');
increq.short_description = "Request converted from " + current.number;
increq.requested_for = current.caller_id; // caller copied
var sysID1 = increq.insert();
current.sc_request = sysID;
//////////////////////////////////////////
var incritem = new GlideRecord("sc_req_item");
incritem.request = increq.sys_id;
incritem.short_description = current.short_description + " converted from " + current.number;
incritem.cmdb_ci = current.cmdb_ci;
incritem.stage = "Delivery";
incritem.state = "Work in Progress";
incritem.priority = current.priority;
var sysID2 = incritem.insert();
current.sc_req_item = sysID;
////////////////////////////////////////////////
var inctask = new GlideRecord("sc_task");
inctask.short_description = current.short_description + ". Ticket converted from " + current.number;
inctask.description = current.description;
inctask.cmdb_ci = current.cmdb_ci;
inctask.sys_domain = current.sys_domain;
inctask.assignment_group = current.assignment_group;
inctask.assigned_to = current.assigned_to;
inctask.work_notes = current.work_notes.getJournalEntry(-1); // work notes copied
inctask.comments = current.comments.getJournalEntry(-1); //comments copied
inctask.created_by = current.caller_id.user_name;
inctask.u_converted = true;
inctask.work_notes = "Ticket was originally raised on: " + current.opened_at;
inctask.request_item = incritem.sys_id;
inctask.u_original_incident_number = current.number;
var sysID = inctask.insert();
GlideSysAttachment.copy('incident', current.sys_id, 'sc_task', sysID); // Attachment copied form INC to inctask
current.deleteRecord(); // Curent INC record is being deleted
current.sc_task = sysID;
gs.addInfoMessage("Catalog task " + inctask.number + " created");
action.setRedirectURL(inctask);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-19-2024 01:48 AM
Hi @Alex Saager1,
To modify the script so that it closes the incident with resolution notes instead of deleting it, you'll need to make a few adjustments. Below is the modified script with the changes highlighted:
var increq = new GlideRecord('sc_request');
increq.short_description = "Request converted from " + current.number;
increq.requested_for = current.caller_id; // caller copied
var sysID1 = increq.insert();
current.sc_request = sysID;
//////////////////////////////////////////
var incritem = new GlideRecord("sc_req_item");
incritem.request = increq.sys_id;
incritem.short_description = current.short_description + " converted from " + current.number;
incritem.cmdb_ci = current.cmdb_ci;
incritem.stage = "Delivery";
incritem.state = "Work in Progress";
incritem.priority = current.priority;
var sysID2 = incritem.insert();
current.sc_req_item = sysID;
////////////////////////////////////////////////
var inctask = new GlideRecord("sc_task");
inctask.short_description = current.short_description + ". Ticket converted from " + current.number;
inctask.description = current.description;
inctask.cmdb_ci = current.cmdb_ci;
inctask.sys_domain = current.sys_domain;
inctask.assignment_group = current.assignment_group;
inctask.assigned_to = current.assigned_to;
inctask.work_notes = current.work_notes.getJournalEntry(-1); // work notes copied
inctask.comments = current.comments.getJournalEntry(-1); //comments copied
inctask.created_by = current.caller_id.user_name;
inctask.u_converted = true;
inctask.work_notes = "Ticket was originally raised on: " + current.opened_at;
inctask.request_item = incritem.sys_id;
inctask.u_original_incident_number = current.number;
var sysID = inctask.insert();
GlideSysAttachment.copy('incident', current.sys_id, 'sc_task', sysID); // Attachment copied form INC to inctask
//////////////////////////////////////////
// Close Incident with Resolution Notes //
//////////////////////////////////////////
current.work_notes = "Incident converted to request. Resolution: [Your resolution notes here]";
current.state = 6; // Set state to Resolved. You may need to adjust the state value based on your instance's configuration.
current.update();
//////////////////////////////////////////
current.sc_task = sysID;
gs.addInfoMessage("Catalog task " + inctask.number + " created");
action.setRedirectURL(inctask);
```
In this modified script:
1. Instead of deleting the incident using `current.deleteRecord();`, the incident's state is updated to "Resolved" (`current.state = 6;`) with the specified resolution notes (`current.work_notes = "Incident converted to request. Resolution: [Your resolution notes here]";`).
2. The incident is then updated with the new state and resolution notes using `current.update();`.
With these changes, the incident will be closed with resolution notes instead of being deleted when a request is created from it. Adjust the resolution notes and state value as needed to align with your organization's processes and requirements.
Please hit helpful and accept this as a solution if it solved your problem.
Thank you !
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-19-2024 08:43 AM
Hi @M Ismail
Thanks for this looks like i was missing the current.update; from my original script.
Only other thing I need to do is add the resolution notes and resolution code as they are mandatory fields so thought I could do this with something like:
current.close_notes = "Incident has been converted to a request. (can I add the new request number here)";
current.close_code = "Duplicate/Incorrectly Logged";
But those options aren't available when I try: