Auto Close Problem(Problem Task is Closed)

Ereshkigal
Tera Contributor

Hi.

Regarding problem management, there is a requirement to automatically close after 2 days if the following conditions are met.

  1. Problem status resolved
  2. 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
1 ACCEPTED SOLUTION

@Ereshkigal 

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

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

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

find_real_file.png

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

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!

 

 

 

Hi,

you can add that logic in the auto closure BR as well then

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Ereshkigal
Tera Contributor

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();
		}
	}
}