0% found this document useful (0 votes)
39 views6 pages

Service Now Scripts

The document contains various scripts for interacting with the 'incident' and 'sys_user' tables in ServiceNow, including fetching records, creating and updating incidents, and managing users and groups. It demonstrates how to retrieve the last and first five incident records, create and update incidents with specific attributes, and manage user roles and group memberships. Additionally, it includes a script for printing unique values from two arrays.

Uploaded by

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

Service Now Scripts

The document contains various scripts for interacting with the 'incident' and 'sys_user' tables in ServiceNow, including fetching records, creating and updating incidents, and managing users and groups. It demonstrates how to retrieve the last and first five incident records, create and update incidents with specific attributes, and manage user roles and group memberships. Additionally, it includes a script for printing unique values from two arrays.

Uploaded by

iamehtesham0901
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

== Script to fetch last 5 records from incident table

var inc = new GlideRecord('incident');

inc.orderByDesc('sys_created_on');

inc.setLimit(5);

inc.query();

while(inc.next()){

gs.info(inc.number);

== Script to fetch first 5 record from incident table

var inc = new GlideRecord('incident');

inc.orderByAsc('sys_created_on');

inc.setLimit(5);

inc.query();

while(inc.next()){

gs.info(inc.number);

== creating a new record in incident

var inc = new GlideRecord('incident');

inc.initialize();

inc.caller_id="5137153cc611227c000bbd1bd8cd2005";

inc.category='hardware';

inc.subcategory='cpu';

inc.contact_type='email';

inc.state='1';

inc.impact='1';

inc.urgency='1';

inc.short_description="CPU is not working ";

var sysID = inc.insert();

gs.info("the record created has sys id -->"+sysID);


== updating a record in incident

var inc = new GlideRecord('incident');

inc.addQuery('number', 'INC0010026');

inc.query();

if(inc.next()){

inc.state='2';

inc.update();

== creating and updating an incident

var inc = new GlideRecord('incident');

inc.initialize();

inc.caller_id="9334868983af92102d6f9defeeaad3c0";

inc.category='hardware';

inc.subcategory='cpu';

inc.contact_type='phone';

inc.state='1';

inc.impact='1';

inc.urgency='2';

inc.short_description="CPU is working but sparking can be heard ";

var sysID = inc.insert();

gs.info("the record created has sys id -->"+sysID);

inc.addQuery('sys_id',sysID);

inc.query();

if(inc.next()){

inc.state='2';

inc.update();

}
== creating a user

var usr = new GlideRecord('sys_user');

usr.initialize();

usr.user_name = 'Ashishhhhhh.Singh';

usr.first_name = 'Ashishhhhhhh';

usr.last_login = 'Singh';

var uSysId = usr.insert();

gs.info("user ki sys id hai "+ uSysId);

== creating a group

var Grp = new GlideRecord('sys_user_group');

Grp.initialize();

Grp.name = 'Software Quality Assurance';

Grp.description = 'this is the group which ensure SQA of the software'

var gSysId = Grp.insert();

gs.info("group ki sys id hai "+ gSysId);

== where caller is abel tutor and description like check count

var gr = new GlideRecord('incident');

gr.addEncodedQuery("caller_id=62826bf03710200044e0bfc8bcbe5df1");

gr.query();

gs.info(gr.getRowCount());

// resetting the selections w/o this this will further filter out from the record gathered

gr.initialize();

gr.addEncodedQuery("descriptionLIKEcheck");

gr.query();

gs.info(gr.getRowCount());
=== current logged in user is member of a particular group

gs.info(gs.getUser().isMemberOf('app_engine_admin'));

== Use GlideAggregate to group incidents by urgency and count how many incidents fall under it.

var gr = new GlideAggregate('incident');

gr.addAggregate("COUNT");

gr.groupBy('urgency');

gr.query();

while(gr.next()){

gs.info(gr.urgency.getDisplayValue()+ " count --" +gr.getAggregate("COUNT"));

== Get the roles & groups of logged In user.

var gr = new GlideRecord("sys_user_has_role");

gr.addQuery('user', gs.getUserID());

gr.query();

gs.info(gr.getRowCount());

while(gr.next()){

gs.info(gr.role.name);

var gr1 = new GlideRecord("sys_user_grmember");

gr1.addQuery('user', gs.getUserID());

gr1.query();

while(gr1.next()){

gs.info("Current logged in used is member of "+gr1.group.name);


}

== Get the users with Particular role.

var gr = new GlideRecord("sys_user_has_role");

gr.addQuery('role.name','itil');

gr.query();

while(gr.next()){

gs.info(gr.user.name);

== Print the Unique Values from the given arrays


arr1 = [ 1 , 2 , 3 , 5 ,6 ], arr2 = [ 1 , 3, 5, 7, 8], ans = [ 2 , 6 ,7 ,8]

var arr1 = [1, 1, 2, 3, 4, 5];


var arr2 = [1, 3, 5];
//ans should be [2,4] since they both appears only once
var ans = [0, 0, 0, 0, 0, 0]; //frequency array should have size of the largest numebr in both arrays;
for (var i = 0; i < arr1.length; i++) {
ans[arr1[i]]++;
}
for (var j = 0; j < arr2.length; j++) {
ans[arr2[j]]++;
}
for (var k = 0; k < ans.length; k++) {
if (ans[k] == 1) {
gs.info(k);
}

You might also like