0% found this document useful (0 votes)
6 views10 pages

ServiceNow Problem Query Scripts

The document contains a series of scripts written for the GlideRecord API to query and manipulate problem and incident records in a ServiceNow environment. Each script addresses specific conditions such as filtering by state, resolution code, priority, and creation date, and outputs the results in JSON format. The scripts also include logging of total counts and structured data for various problem and incident attributes.

Uploaded by

Sahil Shinde
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views10 pages

ServiceNow Problem Query Scripts

The document contains a series of scripts written for the GlideRecord API to query and manipulate problem and incident records in a ServiceNow environment. Each script addresses specific conditions such as filtering by state, resolution code, priority, and creation date, and outputs the results in JSON format. The scripts also include logging of total counts and structured data for various problem and incident attributes.

Uploaded by

Sahil Shinde
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

//1. Print All Problems Whose State is Closed and Resolution Code is Fix Applied.

//state=107^resolution_code=fix_applied

var closedProblemsArray = [];


var grProblem = new GlideRecord('problem');
[Link]('state', '=', 107).addCondition('resolution_code', '=',
'fix_applied');
[Link]();
while ([Link]()) {
var Obj = {
"Problem names are: ": [Link]('number')
};
[Link](Obj);
}
var Obj2 = [Link](closedProblemsArray, 2, 2);
[Link](Obj2, 'resources');

//2. Print All Problem Whose Related Incident Count is Greater than 3 and State is
not Resolve yet.
//related_incidents>3^state!=106

var grProblem2 = new GlideRecord('problem');


[Link]('related_incidents','>',3).addCondition('state','!=',106);
[Link]();
var closedProblemsArray=[];
var count=0;
while([Link]()){
var Obj={
"Problem number are: ":[Link]('number')
};
[Link](Obj);
count++;
}

var MainObj={
"Problem": closedProblemsArray
}
var InciFinal = [Link](MainObj,2,2);
[Link]('Total count: '+count);
[Link](InciFinal,"resources");

//3. Print All Problem created last 1 week and not yet resolved. Also Print Related
Incident if Priority is critical.
//sys_created_onONLast
week@javascript:[Link]()@javascript:[Link]()^state!
=106^priority=1

// Problems created last 1 week and not yet resolved


var grProblem3 = new GlideRecord('problem');
[Link]('sys_created_on', '>=', [Link]())
.addCondition('sys_created_on', '<=', [Link]());
[Link]('state', '!=', 106); // not resolved
[Link]();

var closedProblemsArray = [];


var count = 0;

while ([Link]()) {
var problemObj = {
"Problem Number": [Link]('number'),
"Short Description": [Link]('short_description'),
"Incidents": [] // to store related incidents
};

var grIncident = new GlideRecord('incident');


[Link]('problem_id', [Link]()); // link to
problem
[Link]('priority', 1); // critical
[Link]();

while ([Link]()) {
var inciObj = {
"Incident Number": [Link]('number'),
"Short Description": [Link]('short_description'),
"State": [Link]('state'),
"Assigned To": [Link]('assigned_to')
};
problemObj["Incidents"].push(inciObj);
}

[Link](problemObj);
count++;
}

var MainObj = {
"Problems": closedProblemsArray
};

var InciFinal = [Link](MainObj, null, 2);


[Link]('Total count: ' + count);
[Link](InciFinal, "resources");

//4. Print all Problems whose state is 'fix in progress' and Assigned to 'Problem
Coordinator B'.
//state=104^assigned_to=38cb3f173b331300ad3cc9bb34efc4d6

var grProblem4 = new GlideRecord('problem');


[Link]('state','=',104).addCondition('assigned_to','=','38cb3f173b3313
00ad3cc9bb34efc4d6');
[Link]();
var closedProblemsArray=[];
var count=0;
while([Link]()){
var Obj={
"Problem number are: ":[Link]('number')
};
[Link](Obj);
count++;
}

var MainObj={
"Problem": closedProblemsArray
}
var InciFinal = [Link](MainObj,2,2);
[Link]('Total count: '+count);
[Link](InciFinal,"resources");
//5. Print all Problem whose problem statement contains 'unavailable'.
//short_descriptionLIKEunavailable

var grProblem5 = new GlideRecord('problem');


[Link]('short_description','CONTAINS','unavailable');
[Link]();
var closedProblemsArray=[];
var count=0;
while([Link]()){
var Obj={
"Problem number are: ":[Link]('number')
};
[Link](Obj);
count++;
}

var MainObj={
"Problem": closedProblemsArray
}
var InciFinal = [Link](MainObj,2,2);
[Link]('Total count: '+count);
[Link](InciFinal,"resources");

//6. Print any 10 Incident in following JSON format.

var grProblem6 = new GlideRecord('problem');


[Link]('active', '=', true);
[Link]();
var problemsObj = {};
var count = 0;
while ([Link]()) {
var Obj = {
"Number": [Link]('number'),
"Problem-statement": [Link]('short_description'),
"Model": [Link]('prb_model'),
"State": [Link]('state'),
"Assigned-to": {
"Name": grProblem6.assigned_to.[Link](),
"Email": grProblem6.assigned_to.[Link](),
"Manager": grProblem6.assigned_to.[Link]()
}
};
problemsObj["Problem" + (count + 1)] = Obj;

count++;
}

var Obj2 = [Link](problemsObj, 2, 2);


[Link]('Total count: ' + count);
[Link](Obj2, 'resources');

//7. Print the Problem which are created in last month and assignment group is
empty.
//sys_created_onONLast
month@javascript:[Link]()@javascript:[Link]()^assignmen
t_groupISEMPTY

var grProblem7 = new GlideRecord('problem');


[Link]('sys_created_on', '>=',
[Link]()).addCondition('sys_created_on', '<=',
[Link]());
[Link]();
var count = 0;
var grArray=[];
while ([Link]()) {
var Obj = {
"Problem ": [Link]('number')
};
[Link](Obj);
count++;
}

var MainObj={
"Problem": grArray
}
var InciFinal = [Link](MainObj,2,2);
[Link]('Total count: '+ count);
[Link](InciFinal,"resources");

//8. Print the Opened Problem whose Priority is Critical.


//priority=1
var grProblem8 = new GlideRecord('problem');
[Link]('priority', '=', 1);
[Link]();
var count = 0;
var grArray = [];
while ([Link]()) {
var Obj = {
"Problem ": [Link]('number')
};
[Link](Obj);
count++;
}

var MainObj = {
"Problem": grArray
}
var InciFinal = [Link](MainObj, 2, 2);
[Link]('Total count: ' + count);
[Link](InciFinal, "resources");

//9. Print the All Problems such that the most recently created problems appear
first.

var grProblem9 = new GlideRecord('problem');


[Link]('sys_created_on');
[Link]();
var count = 0;
var grArray = [];
while ([Link]()) {
var Obj = {
"Problem ": [Link]('number')
};
[Link](Obj);
count++;
}

var MainObj = {
"Problem": grArray
};
var InciFinal = [Link](MainObj, 2, 2);
[Link]('Total count: ' + count);
[Link](InciFinal, "resources");

//10. Print the List of Assigned to of all problem

var grProblem10 = new GlideRecord('problem');


[Link]();
var count = 0;
var grArray = [];
while ([Link]()) {
var Obj = {
"Problem ": [Link]('number')
};
[Link](Obj);
count++;
}

var MainObj = {
"Problem": grArray
};
var InciFinal = [Link](MainObj, 2, 2);
[Link]('Total count: ' + count);
[Link](InciFinal, "resources");

-----------------------------------------------------------------------------------
------

//1>.Fetch the latest 5 incidents and build a JSON array with:


[
{"number": "INC0010001", "short_description": "Issue A"},
{"number": "INC0010002", "short_description": "Issue B"}
]
var grProblem1 = new GlideRecord('problem');
[Link]('sys_created_on');
[Link](5);
[Link]();
var count = 0;
var grArray = [];
while ([Link]()) {
var Obj = {
"Problem ": [Link]('number')
};
[Link](Obj);
count++;
}

var MainObj = {
"Problem": grArray
};
var InciFinal = [Link](MainObj, 2, 2);
[Link]('Total count: ' + count);
[Link](InciFinal, "resources");

-----------------------------------------------
//2>.Fetch all Priority = 1 & Active = true incidents. Store them in an array of
JSON objects with:
*>Incident Number
*>Opened By (Display Value)
//active=true^priority=1
var grProblem2 = new GlideRecord('problem');
[Link]('active','=',true).addCondition('priority','=',1);
[Link]();
var count = 0;
var grArray = [];
while ([Link]()) {
var Obj = {
"Problem ": [Link]('number')
};
[Link](Obj);
count++;
}

var MainObj = {
"Problem": grArray
};
var InciFinal = [Link](MainObj, 2, 2);
[Link]('Total count: ' + count);
[Link](InciFinal, "resources");

----------------------------------------------

3>.Fetch 3 incidents and prepare JSON including:


{
"number": "INC0010050",
"caller": {"name": "User A", "email": "userA@[Link]"}
}
var grProblem3 = new GlideRecord('incident');
[Link](3);
[Link]();
var problemsObj = {};
var count = 0;
while ([Link]()) {
var Obj = {
"Number": [Link]('number'),

"caller": {
"Name": grProblem3.caller_id.first_name.getValue(),
"Email": grProblem3.caller_id.[Link](),
}
};
problemsObj["Incident" + (count + 1)] = Obj;

count++;
}

var Obj2 = [Link](problemsObj, 2, 2);


[Link]('Total count: ' + count);
[Link](Obj2, 'resources');
--------------------------------------------
4>.Fetch all incidents where:
active = true
priority = 1
assigned_to IS NOT EMPTY

//active=true^priority=1^assigned_toISNOTEMPTY

var grProblem2 = new GlideRecord('incident');


[Link]('active', '=', true).addCondition('priority', '=',
1).addCondition('assigned_to', '!=', '');
[Link]();
var count = 0;
var grArray = [];
while ([Link]()) {
var Obj = {
"Problem ": [Link]('number')
};
[Link](Obj);
count++;
}

var MainObj = {
"Problem": grArray
};
var InciFinal = [Link](MainObj, 2, 2);
[Link]('Total count: ' + count);
[Link](InciFinal, "resources");

---------------------------------------------
5>.Fetch all incidents where:
state = On Hold
category = network
impact = 2
//state=3^category=network^impact=2

var grProblem2 = new GlideRecord('incident');


[Link]('state', '=',3).addCondition('category',
'=','network').addCondition('impact', '=', 2);
[Link]();
var count = 0;
var grArray = [];
while ([Link]()) {
var Obj = {
"Problem ": [Link]('number')
};
[Link](Obj);
count++;
}

var MainObj = {
"Problem": grArray
};
var InciFinal = [Link](MainObj, 2, 2);
[Link]('Total count: ' + count);
[Link](InciFinal, "resources");

---------------------------------------------
6>.Fetch all incidents where:
active = false
priority = 3
caller_id = Fred Luddy
//active=false^priority=3^caller_id=5137153cc611227c000bbd1bd8cd2005

var grProblem2 = new GlideRecord('incident');


[Link]('active', '=',false).addCondition('priority',
'=',3).addCondition('caller_id', '=', '5137153cc611227c000bbd1bd8cd2005');
[Link]();
var count = 0;
var grArray = [];
while ([Link]()) {
var Obj = {
"Problem ": [Link]('number')
};
[Link](Obj);
count++;
}

var MainObj = {
"Problem": grArray
};
var InciFinal = [Link](MainObj, 2, 2);
[Link]('Total count: ' + count);
[Link](InciFinal, "resources");

----------------------------------------------
7>.Fetch the oldest incident (by sys_created_on) and print its number and created
date.

var grProblem1 = new GlideRecord('incident');


[Link]('sys_created_on');
[Link]();
var count = 0;
var grArray = [];
while ([Link]()) {
var Obj = {
"incident ": [Link]('number'),
"Created date ":[Link]('sys_created_on')
};
[Link](Obj);
count++;
}

var MainObj = {
"Problem": grArray
};
var InciFinal = [Link](MainObj, 2, 2);
[Link]('Total count: ' + count);
[Link](InciFinal, "resources");

----------------------------------------------
8>.Fetch the latest 3 incidents assigned to Database group, ordered by
sys_created_on DESC.

var grProblem1 = new GlideRecord('incident');


[Link]('sys_created_on');
[Link]('assignment_group', '=', '287ee6fea9fe198100ada7950d0b1b73');
[Link](3);
[Link]();
var count = 0;
var grArray = [];
while ([Link]()) {
var Obj = {
"Problem ": [Link]('number')
};
[Link](Obj);
count++;
}

var MainObj = {
"Problem": grArray
};
var InciFinal = [Link](MainObj, 2, 2);
[Link]('Total count: ' + count);
[Link](InciFinal, "resources");

-----------------------------------------------

9>.Fetch all active incidents and print their numbers.

var grProblem1 = new GlideRecord('incident');


[Link]('active','=',true);
[Link]();
var count = 0;
var grArray = [];
while ([Link]()) {
var Obj = {
"Problem ": [Link]('number')
};
[Link](Obj);
count++;
}

var MainObj = {
"Problem": grArray
};
var InciFinal = [Link](MainObj, 2, 2);
[Link]('Total count: ' + count);
[Link](InciFinal, "resources");

---------------------------------------------
10>.Fetch all incidents where priority = 2 and print their number and
short_description.

var grProblem1 = new GlideRecord('incident');


[Link]('priority','=',2);
[Link]();
var count = 0;
var grArray = [];
while ([Link]()) {
var Obj = {
"Problem ": [Link]('number'),
"Short-Description ": [Link]('short_description')
};
[Link](Obj);
count++;
}

var MainObj = {
"Problem": grArray
};
var InciFinal = [Link](MainObj, 2, 2);
[Link]('Total count: ' + count);
[Link](InciFinal, "resources");

==============================================

You might also like