Checking for down MID servers, and current open incidents

Ben54
Tera Contributor

Hey guys, wondered if anyone could help me with this code I'm working on. I have a flow which fires when a MID state is down and will execute a workflow, the workflow goes through a few checks and ultimately raises a ticket. I know the mid name is in the scratchpad and working,

 

There is an open incident with this MID name currently and matches this criteria. If I run this code in background scripts and replace the return with gs.info and replace the MID name, I can get a yes/no returned. So I know this works, just seems to fall over inside a workflow and its baffling me! Any help would be really appreciated.

 

function existingticket() {
    // Retrieve the value of 'midname' from the workflow scratchpad
    var mids = workflow.scratchpad.midname;
    
    // Initialize the incident count
    var incCount = 0;
 
    // Create a new GlideRecord for the 'incident' table
    var gr = new GlideRecord('incident');
    
    // Add queries to filter records based on specified criteria
    gr.addQuery('short_description', 'CONTAINS', mids);
    gr.addQuery('active', true);
    gr.addQuery('state', 'NOT IN', '6,7');
    
    // Execute the query
    gr.query();
 
    // Loop through the records and count them
    while (gr.next()) {
        incCount += 1;
    }
 
    // Check the count and return the result
    if (incCount >= 1) {
        return 'yes';
    } else {
        return 'no';
}
}();

 

2 REPLIES 2

Ahana 01
Tera Expert

To check for down MID servers and current open incidents in ServiceNow, you can follow these steps:

1. **Checking for Down MID Servers:**
- Navigate to "MID Server" > "Servers".
- The list of MID servers will be displayed.
- Check the "Status" column for each MID server. If the status is "Down", then the MID server is not running.

2. **Checking for Current Open Incidents:**
- Navigate to "Incident" > "Open".
- The list of open incidents will be displayed.
- You can further filter this list based on assignment group, priority, etc.

Please note that you need the appropriate roles to view these modules and lists.

For automating these checks, you can use ServiceNow scripting. Here is a basic example of how you can do it:

javascript
// Checking for down MID servers
var midServer = new GlideRecord('ecc_agent');
midServer.addQuery('status', 'Down');
midServer.query();
while(midServer.next()) {
gs.print('MID Server ' + midServer.name + ' is down.');
}

// Checking for open incidents
var incident = new GlideRecord('incident');
incident.addQuery('state', '!=', 6); // 6 is the state value for "Closed"
incident.query();
while(incident.next()) {
gs.print('Incident ' + incident.number + ' is open.');
}


This script will print the names of down MID servers and open incidents to the system log. You can modify it to send notifications, create incidents, etc., based on your needs.


nowKB.com

For a good and optimistic result, and solving ServiceNow-related issues please visit this website.https://nowkb.com/home
Kindly mark correct and helpful if applicable

Ben54
Tera Contributor

Hi Ahana, thank you for the reply.

I dont need to print the incident number, just a simple count to see if any open records meet my criteria. The plan is to terminate the workflow if an incident is open to avoid duplication. However the If statement in the workflow always ends up at the 'no' condition, despite there being open records that match this criteria.

 

Running the same script in background scripts (with a modified MID name) returns yes.