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

SK Chand Basha
Giga Sage

Hi @sureshp89882164 

 

Write a simple BR to achieve this requirement. 

 

1. Create a Business Rule:
  • Go to System Definition > Business Rules.
  • Click "New" to create a new business rule.
  • Name: Give it a descriptive name (e.g., "Close Problem Task on Problem Closure").
  • Table: Select "problem".
  • When to Run: Select "After".

 

Code Snippet:-

 

(function executeRule(current, previous /*null when async*/) {

// Check if the problem state has changed to "Closed"
if (current.state == 3 && previous.state != 3) { // 3 is the ID for the "Closed" state in ServiceNow

// Get the related problem task(s)
var problemTask = new GlideRecord('problem_task');
problemTask.addQuery('problem', current.sys_id);
problemTask.query();

while (problemTask.next()) {
// Add a work note to the problem task
var note = "Problem Task " + problemTask.number + " has been closed as part of problem " + current.number + " closure.";
problemTask.work_notes = note;
problemTask.update();

// Close the problem task
problemTask.state = 3; // 3 is the ID for the "Closed" state in ServiceNow
problemTask.close_code = "Resolved"; // Or appropriate close code
problemTask.update();
}
}
})(current, previous);

 

Mark it helpful and Accept Solution, If this helps you to understand

Actually this script is for Scheduled job Need to add work notes automatically in problem record and problem task as well and close the problem tasks automatically

Ankur Bawiskar
Tera Patron
Tera Patron

@sureshp89882164 

if you want to have scheduled job then which problems should be closed? open?

what script did you start with and where are you stuck?

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Below one existing script is for Non-Major problem record in the new state that reaches 29 days is automatically closed.

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();
   
    }
}
*To the above script need to add new requirement is to close all problem tasks automatically. And update the work notes automatically in problem record.