- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-23-2022 01:57 AM
Hi.
Regarding problem management, there is a requirement to automatically close after 2 days if the following conditions are met.
- Problem status resolved
- All problem tasks are closed or no problem tasks exist
I created the following Business Rule and Scheduled Job, but it doesn't work.
I am a beginner in scripting, so please let me know if there are any mistakes.
Help me!
Business Rule (After):problem autoclose
checkForPRTasks();
gs.info("Problem Auto Close");
function checkForPRTasks() {
var prTasks = new GlideRecord('problem_task');
prTasks.addQuery('problem', current.sys_id);
prTasks.query();
while (prTasks.next()) {
if (prTasks.state != ProblemTaskStateSNC.CLOSED) {
break;
} else {
autoCloseProblems();
gs.info("Problem Task is Closed");
}
}
}
function autoCloseProblems() {
var prb = new GlideRecord("problem");
prb.addQuery("problem_state", ProblemState.STATES.RESOLVED);
prb.addQuery("sys_updated_on", "<=", gs.daysAgo(-2));
prb.query();
while (prb.next()){
prb.problem_state = ProblemState.STATES.CLOSED;
prb.state = ProblemState.STATES.CLOSED;
prb.active = false;
prb.closed_by = prb.sys_updated_by;
prb.update();
}
}
Scheduled Job
- Name:Auto Close Problem
- Job context:fsScriptName= problem autoclose
- Trigger type: interval
- Repeat :1h
Solved! Go to Solution.
- Labels:
-
Script Debugger
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-23-2022 08:27 PM
update as this
autoCloseProblems();
function autoCloseProblems() {
var ps = gs.getProperty('glide.ui.autoclose.time.problem');
var pn = parseInt(ps);
var queryTime = new GlideDateTime();
queryTime.addDaysUTC(-pn);
if (pn > 0) {
var gr = new GlideRecord('problem');
gr.addQuery('problem_state', ProblemState.STATES.RESOLVED);
if (gs.getProperty('com.snc.problem.autoclose.basedon.resolved_at', 'false') === 'true') {
gr.addQuery('resolved_at', '<', queryTime);
}
else {
gr.addQuery('sys_updated_on', '<', queryTime);
}
gr.query();
while(gr.next()) {
var pTask = new GlideRecord("problem_task");
pTask.addEncodedQuery("stateIN151,152,154"); // state is New or Access or In Progress
pTask.addQuery("problem", gr.getUniqueValue());
pTask.setLimit(1); // to check 1 record
pTask.query();
if(!pTask.hasNext()){
gr.problem_state = ProblemState.STATES.CLOSED;
gr.state = ProblemState.STATES.CLOSED;
// gr.comments = 'Problem automatically closed after ' + pn + ' days in the Resolved state.';
gr.active = false;
gr.closed_by = gr.resolved_by;
gr.update();
}
}
}
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-23-2022 02:11 AM
Hi,
Please check OOB incident auto closure BR script and enhance for your problem table
You should only focus on auto closure
- All problem tasks are closed or no problem tasks exist -> this can be taken care by Before update BR on problem table which would restrict closure if any open task is present
Name: incident autoclose
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-23-2022 02:19 AM
Thank you for your reply.
this can be taken care by Before update BR on problem table which would restrict closure if any open task is present ->If the problem task is closed, does auto-close mean that it is not implementable?
I somehow understand that the "current.sys_id" part is strange.
How can you meet your requirements?
Thank you!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-23-2022 03:31 AM
Hi,
you can add that logic in the auto closure BR as well then
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-23-2022 06:17 PM
Thank you for your reply.
By the way, I confirmed that it works with the following Business Rule.
Could you please tell me how to code to make sure that all problem tasks are closed in this Business Rule?
Thank you!
Business Rule(Extended from Incident)
autoCloseProblems();
function autoCloseProblems() {
var ps = gs.getProperty('glide.ui.autoclose.time.problem');
var pn = parseInt(ps);
var queryTime = new GlideDateTime();
queryTime.addDaysUTC(-pn);
if (pn > 0) {
var gr = new GlideRecord('problem');
gr.addQuery('problem_state', ProblemState.STATES.RESOLVED);
if (gs.getProperty('com.snc.problem.autoclose.basedon.resolved_at', 'false') === 'true') {
gr.addQuery('resolved_at', '<', queryTime);
}
else {
gr.addQuery('sys_updated_on', '<', queryTime);
}
gr.query();
while(gr.next()) {
gr.problem_state = ProblemState.STATES.CLOSED;
gr.state = ProblemState.STATES.CLOSED;
// gr.comments = 'Problem automatically closed after ' + pn + ' days in the Resolved state.';
gr.active = false;
gr.closed_by = gr.resolved_by;
gr.update();
}
}
}