Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

@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