Practice GlideRecord API ServiceNow
Practice GlideRecord API ServiceNow
-----------------------------------------------------------------------------------
-----------------------------------
//passing mutilple queries using same method
-----------------------------------------------------------------------------------
-----------------------------------
//printing priority 2 incidents includes short description with limit of 10max
-----------------------------------------------------------------------------------
-------------------------------------
//printing priority 2 incidents includes short description with limit of 10max in
ascending order of short description
-----------------------------------------------------------------------------------
-------------------------------------
//printing priority 2 change_request includes short description with limit of 10max
in ascending order of short description
_______________________________________________________________
example-2:
var his = new GlideRecord('incident');
his.addQuery('priority',1);
his.orderByDesc('short_description');
his.setLimit(20);
his.query();
while(his.next()){
gs.print(his.number + ' ' + his.short_description);
}
-----------------------------------------------------------------------------------
-----------------------------------
//using addEncodedQuery method instead of passing multiple queries
-----------------------------------------------------------------------------------
-----------------------------------
//setting an encoded query to a variable and then using that variable in that code
-----------------------------------------------------------------------------------
-------------------------------------
//active is true and priority is less than or equal to 2 and short description
contains email
-----------------------------------------------------------------------------------
--------------------------------------
//prints where category is software and hardware
var cat = ['software','hardware'];
var inc = new GlideRecord('incident');
inc.addQuery('category','IN','cat');
inc.query();
while(inc.next()){
gs.print(inc.getValue('number') + ' ' + inc.getValue('short_description'));
}
-----------------------------------------------------------------------------------
--------------------------------------
//prints all the inc whose category starts with net
-----------------------------------------------------------------------------------
--------------------------------------
//getting encoded query from the code
example-2:
var inc = new GlideRecord('incident');
inc.addEncodedQuery('active=true^category=software^priority=1');
inc.query();
gs.print(inc.getEncodedQuery());
//prints our encoded query
-----------------------------------------------------------------------------------
---------------------------------------
//getting sys_id of a incident
-----------------------------------------------------------------------------------
----------------------------------------
//getting an incident along with its short description using the sys_id
var inc = new GlideRecord('incident');
inc.get('sys_id','46e2fee9a9fe19810049b49dee0daf58');
gs.print(inc.number+ ':'+inc.short_description);
-----------------------------------------------------------------------------------
----------------------------------------
//displaying count of all records of inc table
-----------------------------------------------------------------------------------
-----------------------------------------
//displaying count of all active users from user table
-----------------------------------------------------------------------------------
------------------------------------------
//displays table name of gliderecord
//similarly we can execute to get name of other table just by changing the table
name
-----------------------------------------------------------------------------------
------------------------------------------
//getting the value of particular field with setting limit and orderby of it
-----------------------------------------------------------------------------------
------------------------------------------
//displaying value of particular field instead of name
-----------------------------------------------------------------------------------
------------------------------------------
????
var inc = new GlideRecord('incident');
inc.addQuery('state', 'new');
inc.addQuery('category=hardware');
inc.query();
while (inc.next()) {
gs.print(inc.number);
}
???
-----------------------------------------------------------------------------------
------------------------------------------
//getting the unique key of the record, typically the sys_id
-----------------------------------------------------------------------------------
------------------------------------------
//inserting of a record:
-----------------------------------------------------------------------------------
-----------------------------------------
//retrieving class name of a record
-----------------------------------------------------------------------------------
-----------------------------------------
//validates if a table exists or not
==>returns false
________________________________________
var inc = new GlideRecord('incident');
gs.print(inc.isValid());
==>returns true
________________________________________
var inc = new GlideRecord('change_request');
gs.print(inc.isValid());
==>returns true
etc etc...
-----------------------------------------------------------------------------------
------------------------------------------
//validates if a field exists or not
etc etc..
-----------------------------------------------------------------------------------
-------------------------------------------
//getproperty || getLink
//retries the link of the current records
-----------------------------------------------------------------------------------
--------------------------------------------
//returns first record from query
-----------------------------------------------------------------------------------
--------------------------------------------
//determines whether record exists or not using isValidRecord method
-----------------------------------------------------------------------------------
--------------------------------------------
//prints all record where specified field value is null