- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2025 05:25 AM
Problem ticket cannot be closed if there are open Incident Tasks anyone please help with coding.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2025 08:47 AM - edited 02-15-2025 01:24 PM
Hello, I am not sure if the requirement makes sense. There is a relation between Problem record and Incident record (via problem_id field on Incident). However here is the solution for your question:
- Create Business Rule on Problem table
- Advanced: True
- When: before
- Run on: Update
- Filter Conditions: State is Closed
- In the Advanced tab, paste this code:
(function executeRule(current, previous /*null when async*/ ) {
var problemSysID = current.getUniqueValue();
var incidentArr = [];
var incTaskArr = [];
// Get active Incident records in relation with current Problem record
var incidentGR = new GlideRecord('incident');
incidentGR.addQuery('problem_id', problemSysID);
incidentGR.addActiveQuery();
incidentGR.query();
while (incidentGR.next()) {
incidentArr.push(incidentGR.getUniqueValue());
}
if (incidentArr.length === 0) {
// No related active Incident record found, no need to check for Incident Tasks
return;
}
// Get active Incident tasks of found Incident records
var incTaskGR = new GlideRecord('incident_task');
incTaskGR.addQuery('incident', 'IN', incidentArr);
incTaskGR.addActiveQuery();
incTaskGR.query();
while (incTaskGR.next()) {
incTaskArr.push(incTaskGR.getValue('number'));
}
// If there is an active Incident Task, abort action
if (incTaskArr.length > 0) {
current.setAbortAction(true);
gs.addErrorMessage('Problem task still has an active incident task: ' + incTaskArr.toString());
}
})(current, previous);
Modify the code as you need. For example you can change the error message to include clickable link to which redirects user to the Incident Task record.
If my answer helped you, please mark it as correct and helpful, thank you 👍
Martin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2025 08:29 AM
Hello, I am not sure if the requirement makes a sense. There is a relation between Problem record and Incident record (via field 'problem_id' on Incident). However here is the solution:
- Create a Business Rule on Problem table [problem]
- Advanced: True
- When: before
- Run on Update
- Filter Conditions: State is Closed
- Add below code to Advanced field
(function executeRule(current, previous /*null when async*/ ) {
var problemSysID = current.getUniqueValue();
var incidentArr = [];
var incTaskArr = [];
// Get active Incident records in relation with current Problem record
var incidentGR = new GlideRecord('incident');
incidentGR.addQuery('problem_id', problemSysID);
incidentGR.addActiveQuery();
incidentGR.query();
while (incidentGR.next()) {
incidentArr.push(incidentGR.getUniqueValue());
}
if (incidentArr.length === 0) {
// No related active Incident record found, no need to check for Incident Tasks
return;
}
// Get active Incident tasks of found Incident records
var incTaskGR = new GlideRecord('incident_task');
incTaskGR.addQuery('incident', 'IN', incidentArr);
incTaskGR.addActiveQuery();
incTaskGR.query();
while (incTaskGR.next()) {
incTaskArr.push(incTaskGR.getValue('number'));
}
// If there is an active Incident Task, abort action
if (incTaskArr.length > 0) {
current.setAbortAction(true);
gs.addErrorMessage('Problem task still has an active incident task: ' + incTaskArr.toString());
}
})(current, previous);
Modify code as you need, especially error message (for example you can enhance it by providing clickable link that redirects user to the record).
Hope it helps 👍
Martin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2025 08:47 AM - edited 02-15-2025 01:24 PM
Hello, I am not sure if the requirement makes sense. There is a relation between Problem record and Incident record (via problem_id field on Incident). However here is the solution for your question:
- Create Business Rule on Problem table
- Advanced: True
- When: before
- Run on: Update
- Filter Conditions: State is Closed
- In the Advanced tab, paste this code:
(function executeRule(current, previous /*null when async*/ ) {
var problemSysID = current.getUniqueValue();
var incidentArr = [];
var incTaskArr = [];
// Get active Incident records in relation with current Problem record
var incidentGR = new GlideRecord('incident');
incidentGR.addQuery('problem_id', problemSysID);
incidentGR.addActiveQuery();
incidentGR.query();
while (incidentGR.next()) {
incidentArr.push(incidentGR.getUniqueValue());
}
if (incidentArr.length === 0) {
// No related active Incident record found, no need to check for Incident Tasks
return;
}
// Get active Incident tasks of found Incident records
var incTaskGR = new GlideRecord('incident_task');
incTaskGR.addQuery('incident', 'IN', incidentArr);
incTaskGR.addActiveQuery();
incTaskGR.query();
while (incTaskGR.next()) {
incTaskArr.push(incTaskGR.getValue('number'));
}
// If there is an active Incident Task, abort action
if (incTaskArr.length > 0) {
current.setAbortAction(true);
gs.addErrorMessage('Problem task still has an active incident task: ' + incTaskArr.toString());
}
})(current, previous);
Modify the code as you need. For example you can change the error message to include clickable link to which redirects user to the Incident Task record.
If my answer helped you, please mark it as correct and helpful, thank you 👍
Martin