Non-Major problem record

sureshp89882164
Tera Contributor

need to add work notes and close the problem tasks automatically when problem record is moved to closed.

7 REPLIES 7

below one script is existing one to close the problem record once reached 29 days.

fun();
function fun()
{
var reminder = 28;
 
var NOW = new GlideDateTime();
var grProblem = new GlideRecord('problem');
grProblem.addEncodedQuery('state=101^major_problem=false');
grProblem.query();
 
while (grProblem.next()) {
var dur1 = GlideDateTime.subtract(new GlideDateTime(grProblem.sys_created_on),NOW); // returns a GlideDuration object
var days = dur1.getDayPart(); // Days apart
 else if (days > reminder)
        {
 
   grProblem.resolution_code = 'canceled';
   grProblem.state ='107';
   grProblem.active = 'false';
   grProblem.setWorkflow(false);
   grProblem.autoSysFields(false);
   grProblem.setUseEngines(false);
   grProblem.closed_at = new GlideDateTime();
   grProblem.update();
   
    }
}
*For this code need to add new requirement is to close the all problem tasks automatically and update work notes in problem record.

Rishi_11
Kilo Sage

Hi @sureshp89882164 

Please try the below script in a scheduled job. Please mark my response as helpful or accept as solution, if it helps you.

(function() {
    // Set up the 29 day threshold
    var days = 29;
    var thresholdDate = new GlideDateTime();
    thresholdDate.addDaysLocalTime(-days); //get date 29days ago

    var probGR = new GlideRecord('problem');
    probGR.addQuery('major_problem', false);  //non major problem
    probGR.addQuery('state', '101');   //state new
    probGR.addQuery('sys_created_on', '<=', thresholdDate);
    probGR.query();

    while (probGR.next()) {
        // Close the problem record
        probGR.setValue('state', '107');
        probGR.update();
        
		//Close problem taks
        var taskGR = new GlideRecord('problem_task');
        taskGR.addQuery('problem', probGR.getUniqueValue());
		taskGR.query();

        while (taskGR.next()) {
            taskGR.setValue('state', '157'); //set task closed
            // Add a work note
            var note = "Auto-closed as associated problem record was closed after 29 days.";
            taskGR.work_notes.setJournalEntry(note);
            taskGR.update();
        }
    }
})();

Thanks, 

Rishi.

Minor correction in my script. There are some Data policies running on problem and problem task table which won't  allow us to directly change the state from "new" to "closed". To bypass this we can use gr.setUseEngines(false); Sharing my updated script below.

(function() {
    // Set up the 29 day threshold
    var days = 29;
    var thresholdDate = new GlideDateTime();
    thresholdDate.addDaysLocalTime(-days); //get date 29days ago

    var probGR = new GlideRecord('problem');
    probGR.addQuery('major_problem', false); 
    probGR.addQuery('state', '101');
    probGR.addQuery('sys_created_on', '<=', thresholdDate);
	probGR.setUseEngines(false);
    probGR.query();

    while (probGR.next()) {
        // Close the problem record
        probGR.setValue('state', '107');
        var probUpdated = probGR.update();
        
		//Close problem taks
        var taskGR = new GlideRecord('problem_task');
        taskGR.addQuery('problem', probGR.getUniqueValue());
		taskGR.setUseEngines(false);
		taskGR.query();

        while (taskGR.next()) {
            taskGR.setValue('state', '157'); //set task closed
            // Add a work note
            var note = "Auto-closed as associated problem record was closed after 29 days.";
            taskGR.work_notes.setJournalEntry(note);
            taskGR.update();
        }
    }
})();

If you found my response helpful, please mark it as helpful.

Thanks,

Rishi.