How to close problem ticket or change request using background script.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-18-2025 09:20 PM
Hi Team,
How to close change request or problem ticket using background script as it is process flow , before closing do we need to complete steps? please help on these. I know we can cancel it directly , but cant close it.
- Labels:
-
Incident Management
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-18-2025 11:43 PM
Hi @Vyanktesh08 ,
You can use below code in background script to close the change and problem.
for change
// Get the Change Request record
var changeRequest = new GlideRecord('change_request'); // Table name for Change Request
if (changeRequest.get('<CHANGE_REQUEST_SYS_ID>')) { // Replace with the sys_id of the record
// Ensure all associated tasks are closed
var changeTasks = new GlideRecord('change_task'); // Table name for Change Tasks
changeTasks.addQuery('change_request', changeRequest.sys_id);
changeTasks.query();
while (changeTasks.next()) {
if (changeTasks.state != 3) { // Assuming 3 is 'Closed' state for tasks
changeTasks.state = 3;
changeTasks.update();
}
}
// Check if approvals are completed (if applicable)
if (changeRequest.approval == 'approved') { // Assuming 'approved' is the required state
changeRequest.state = 4; // Assuming 4 is 'Closed' state for Change Request
changeRequest.close_code = 'successful'; // Example close code
changeRequest.close_notes = 'Closed via background script';
changeRequest.update();
gs.print('Change Request closed successfully.');
} else {
gs.print('Change Request cannot be closed. Approvals are not completed.');
}
} else {
gs.print('Change Request not found.');
}
for problem.
// Get the Problem record
var problemTicket = new GlideRecord('problem'); // Table name for Problem
if (problemTicket.get('<PROBLEM_TICKET_SYS_ID>')) { // Replace with the sys_id of the record
// Ensure all associated incidents are resolved/closed
var relatedIncidents = new GlideRecord('incident'); // Table name for Incidents
relatedIncidents.addQuery('problem_id', problemTicket.sys_id);
relatedIncidents.query();
while (relatedIncidents.next()) {
if (relatedIncidents.state != 7) { // Assuming 7 is 'Closed' state for incidents
relatedIncidents.state = 7;
relatedIncidents.close_code = 'Resolved';
relatedIncidents.close_notes = 'Closed as part of Problem resolution';
relatedIncidents.update();
}
}
// Set Problem state to Closed
if (problemTicket.state == 4) { // Assuming 4 is 'Resolved' state for Problem
problemTicket.state = 6; // Assuming 6 is 'Closed' state for Problem
problemTicket.close_notes = 'Problem closed via background script';
problemTicket.update();
gs.print('Problem ticket closed successfully.');
} else {
gs.print('Problem ticket cannot be closed. It must be in Resolved state first.');
}
} else {
gs.print('Problem ticket not found.');
}
-------------------------------------------------------------------------
If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.
Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay
-------------------------------------------------------------------------
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-19-2025 06:56 AM
why you wish to close?
There are few mandatory fields which needs to be populated when closing these records?
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader