- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-27-2024 08:02 AM
Hello Folks,
I have a requirement on incident form, i want to close incident automatically when all associated incident tasks are closed. How can we do this with scripting. i tried with After Business rule but my script not working as expected. any help would be appreciated.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-27-2024 08:31 AM
HI @Jaya Chandra Re ,
Write a After Update BR on incident task table
when to run: state changes to closed complete
Script:
(function executeRule(current, previous /*null when async*/ ) {
var inc_task = new GlideRecord('incident_task');
inc_task.addQuery('incident', current.incident);//field where incident is captured
inc_task.addQuery('active', true);//check if any tasks are active
inc_task.query();
if (inc_task.next()) {
//do nothing
//you can also print any info message if you want
} else {
var inc = new GlideRecord('incident');
inc.addQuery('sys_id', current.incident);
inc.query();
if (inc.next()) {
inc.state = 6; //close state
inc.close_code = 'successful';
inc.close_notes = "Auto-Closed based on Incident Tasks being Closed"; //while closing incident check if there are any mandatory fields and pass value to those so that the script works
inc.update();
}
}
})(current, previous);
Please mark my answer helpful/correct if it resolved your query
Thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-27-2024 08:41 AM
Have you tried debugging your code?
- 2020-10-21 - Blog - Q: Help!!! My code is not working! A: Did you debug your code?
I'm just not sharing any copy/paste script since that won't learn you anything.
Have you also considered using Flow Designer? For your use case zero code would be involved.
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-27-2024 08:31 AM
HI @Jaya Chandra Re ,
Write a After Update BR on incident task table
when to run: state changes to closed complete
Script:
(function executeRule(current, previous /*null when async*/ ) {
var inc_task = new GlideRecord('incident_task');
inc_task.addQuery('incident', current.incident);//field where incident is captured
inc_task.addQuery('active', true);//check if any tasks are active
inc_task.query();
if (inc_task.next()) {
//do nothing
//you can also print any info message if you want
} else {
var inc = new GlideRecord('incident');
inc.addQuery('sys_id', current.incident);
inc.query();
if (inc.next()) {
inc.state = 6; //close state
inc.close_code = 'successful';
inc.close_notes = "Auto-Closed based on Incident Tasks being Closed"; //while closing incident check if there are any mandatory fields and pass value to those so that the script works
inc.update();
}
}
})(current, previous);
Please mark my answer helpful/correct if it resolved your query
Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-27-2024 08:58 AM
Hello Ramz,
Thanks a lot for helping, it's working as expected.
one more issue i am facing - i have written After - Update BR to create incident task when new incident created or inserted. if i will update anything in short description, instead of updating short description it created new incident task again. how to overcome this issue.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-27-2024 09:38 AM
If you want to create task only when incident is created/inserted then just give After Insert BR.
After Update triggers the business rule for each update you make in incident and hence when you update short description a new task will be created.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-27-2024 09:44 AM
Yaa got it.
i tried with your script its working as expected and inc got closed after tasks closed. but i make some changes on my own to do in different way. but its not working below i am sharing my script. where i did mistake..?