File tree Expand file tree Collapse file tree 2 files changed +60
-0
lines changed
Server-Side Components/Business Rules/Auto close incident if all related changes are closed Expand file tree Collapse file tree 2 files changed +60
-0
lines changed Original file line number Diff line number Diff line change 1+ Business Rule: Auto-Close Incident When All Related Changes Are Closed
2+ Table : change_request
3+ When to Run: After update
4+ Condition: state changes to Closed (or your equivalent "Closed" state number, e.g. state == 3)
5+
6+ Detailed Working
7+ 1 . Trigger Point
8+ This After Business Rule runs after a Change Request record is updated.
9+ Specifically, it checks when the state changes to “Closed”.
10+
11+ 2 . Check for Related Incident
12+ The script retrieves the incident reference field (incident) from the current change request.
13+ If there’s no linked incident, it skips execution.
14+
15+ 3 . Check for Any Remaining Open Change Requests
16+ A new GlideRecord query checks for other Change Requests linked to the same incident where:
17+ If any such records exist, it means not all change requests are closed — so the incident remains open.
18+
19+ 4 . Close the Incident Automatically
20+ If no open Change Requests remain, the script:
21+ Fetches the linked incident.
22+ Sets: state = 7 (Closed)
23+ close_code = Auto Closed
24+ close_notes = Auto closure as all changes are closed.
25+ Updates the record.
Original file line number Diff line number Diff line change 1+ ( function executeRule ( current , previous /*null when async*/ ) {
2+ // Run only when change_request moves to Closed
3+ if ( current . state != previous . state && current . state == 3 ) { // 3 = Closed
4+ var incidentSysId = current . incident ; // assuming there is a reference field to Incident
5+
6+ if ( ! incidentSysId ) {
7+ gs . info ( "No related incident found for this change request: " + current . number ) ;
8+ return ;
9+ }
10+
11+ // Query for other open change requests linked to the same incident
12+ var otherCR = new GlideRecord ( 'change_request' ) ;
13+ otherCR . addQuery ( 'incident' , incidentSysId ) ;
14+ otherCR . addQuery ( 'sys_id' , '!=' , current . sys_id ) ;
15+ otherCR . addQuery ( 'state' , '!=' , 3 ) ; // not closed
16+ otherCR . query ( ) ;
17+
18+ if ( otherCR . hasNext ( ) ) {
19+ gs . info ( "Incident " + incidentSysId + " still has open change requests. Not closing incident." ) ;
20+ return ;
21+ }
22+
23+ // If no open change requests remain, close the incident
24+ var inc = new GlideRecord ( 'incident' ) ;
25+ if ( inc . get ( incidentSysId ) ) {
26+ inc . state = 7 ; // 7 = Closed (modify as per your instance)
27+ inc . close_code = 'Auto Closed' ;
28+ inc . close_notes = 'Incident auto-closed as all associated change requests are closed.' ;
29+ inc . update ( ) ;
30+ gs . info ( "Incident " + inc . number + " auto-closed as all related change requests are closed." ) ;
31+ }
32+
33+ }
34+ } ) ( current , previous ) ;
35+
You can’t perform that action at this time.
0 commit comments