Built something you're proud of? Tell the story. A quick G2 review of App Engine or Build Agent helps other developers see what's possible on ServiceNow. Share your experience.

Backgorund script

SANNAPUREDDYV
Tera Contributor

what is the background script for print the active incidents associated to the closed problem and close those incidents?

1 ACCEPTED SOLUTION

Sid_Takali
Kilo Patron

Hi @SANNAPUREDDYV Try below code and modify accordingly 

var problemGR = new GlideRecord('problem');

problemGR.addQuery('state', '6'); 
problemGR.query();

while (problemGR.next()) {
    var incidentGR = new GlideRecord('incident');
    
    incidentGR.addQuery('problem', problemGR.sys_id);
    incidentGR.addQuery('state', '!=', '7'); 
    incidentGR.query();
    
    while (incidentGR.next()) {
        gs.info('Closing Incident: ' + incidentGR.number + ' associated with Problem: ' + problemGR.number);
        
        // Close the incident
        incidentGR.state = '7'; 
        incidentGR.update();
    }
}

gs.info('Script completed successfully.');

  

View solution in original post

1 REPLY 1

Sid_Takali
Kilo Patron

Hi @SANNAPUREDDYV Try below code and modify accordingly 

var problemGR = new GlideRecord('problem');

problemGR.addQuery('state', '6'); 
problemGR.query();

while (problemGR.next()) {
    var incidentGR = new GlideRecord('incident');
    
    incidentGR.addQuery('problem', problemGR.sys_id);
    incidentGR.addQuery('state', '!=', '7'); 
    incidentGR.query();
    
    while (incidentGR.next()) {
        gs.info('Closing Incident: ' + incidentGR.number + ' associated with Problem: ' + problemGR.number);
        
        // Close the incident
        incidentGR.state = '7'; 
        incidentGR.update();
    }
}

gs.info('Script completed successfully.');