Using GlideRecord we can Create,Update,Read,Delete (CURD) in ServiceNow. Here is some example that can help you understand quickly.

You can Query:

var gr = new GlideRecord('table_name'); 
gr.addQuery('active', true); //return all active record
//gr.addEncodedQuery('active=true^numberSTRATWITHINC); // You can also use encoded query
//gr.get('fild_name','value'); // You can also use get for query
gr.orderByDesc('sys_created_on'); // if you are expecting multiple record then you can use. sort decending
//gr.orderBy('sys_created_on');  // Return record ascending order
//gr.setLimit(10); // it will only return 10 record
gr.query();
while (gr.next()) {  
if(gr.category == 'software'){
gs.log('Category is ' + gr.category);
//gr.update() //here you can also update
}
}

Update Reference Record from the parent record

var caller = current.caller_id.getRefRecord(); //Returns the GlideRecord
caller.email = '[email protected]';
caller.update();

Insert Record

var gr = new GlideRecord('incident');
gr.initialize();
gr.short_description = 'Network problem';
gr.category = 'software';
gr.caller_id.setDisplayValue('Joe Employee');
gr.insert();

Delete Record

var gr = new GlideRecord('incident');
gr.addQuery('active',false);
gr.query();
while (gr.next()) {
gr.deleteRecord(); // It will be delete record one by one
}


var gr = new GlideRecord('incident');
gr.addQuery('active', false);
gr.deleteMultiple(); //Deletes all records together and its more faster

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *