torsten-b
4/26/2016 - 10:46 PM

GlideRecord Examples

GlideRecord Examples

// Query
var rec = new GlideRecord('incident');
rec.query();
while (rec.next()) {
    gs.print(rec.number + ' exists');
}

// Update
var rec = new GlideRecord('incident');
rec.addQuery('active', true);
rec.query();
while (rec.next()) {
    rec.active = false;
    gs.print('Active incident ' + rec.number = ' closed');
    rec.setWorkflow(false);
    rec.autoSysFields(false);
    rec.update();
}

// Insert
var rec = new GlideRecord('incident');
rec.initialize();
rec.short_description = 'Network problem';
rec.caller_id.setDisplayValue('Joe Employee');
rec.insert();

// Delete
var rec = new GlideRecord('incident');
rec.addQuery('active', false);
rec.query();
while (rec.next()) {
    gs.print('Inactive incident ' + rec.number + ' deleted');
    rec.deleteRecord();
}

function deleteRecords(_table, _query) {
	gs.print('Deleting records table: ' + _table + ' using query: ' + _query);
 
	var gr = new GlideRecord(_table);
	if (_query) {
		gr.addEncodedQuery(_query); 
	} else {
		gs.print('Delete all records of table: ' + _table);
		var _query = '';
	}
	gr.query();
	var records = gr.getRowCount();
	gr.deleteMultiple();
	gs.log('SYSTEM-CLEAN-UP: Deleted ' + records + ' records of table: ' + _table + ' using query: ' + _query);
}

// Advanced Query
var rec = new GlideRecord('sys_ui_list');
rec.addQuery('view', 'f99816b6a84ba1b50d032cf5f0447ee5');
rec.addQuery('sys_domain', 'DOES NOT CONTAIN', 'global');
rec.addNotNullQuery('parent');
rec.addNullQuery('sys_user');
rec.orderBy('name');

rec.query();
var rows = rec.getRowCount();

while (rec.next()) {}

/*
=	 Field must be equal to value supplied. 	               addQuery('priority', '=', 1);
>	 Field must be greater than value supplied	             addQuery('priority', '>', 1);
<	 Field must be less than value supplied.	               addQuery('priority', '<', 3);
>=	 Field must be equal or greater than value supplied    addQuery('priority', '>=', 1);
<=	 Field must be equal or less than value supplied       addQuery('priority', '<=', 3);
!=	 Field must not equal the value supplied.	             addQuery('priority', '!=', 1);



STARTSWITH  	 Field must start with the value supplied.   The example shown on the right will get all records where the short_description field starts with the text Error.	 
addQuery('short_description', 'STARTSWITH', 'Error');

CONTAINS	 Field must contain the value supplied somewhere in the text.  The example shown on the right will get all records where the short_description field contains the text Error anywhere in the field.	 
addQuery('short_description', 'CONTAINS', 'Error');

IN	 Takes a map of values that allows commas, and gathers a collection of records that meet some other requirement. Behaves as Select * from <table> where short_description IN ('Error'), which is identical to Select * from <table> where short_description='Error'. For example, to query all variable values that belong to a specific Activity, use the IN clause to query all Activities that are of that type, and store their sys_ids in a map, or comma-separated list. Then query the variable value table and supply this list of sys_ids.
addQuery('short_description', 'IN', 'Error,Success,Failure');

ENDSWITH	 Field must terminate with the value supplied.  The example shown on the right will get all records where the short_description field ends with text Error.
addQuery('short_description', 'ENDSWITH', 'Error');

DOES NOT CONTAIN	 Field must not have with the value supplied anywhere in the text.  The example shown on the right will get all records where the short_description field does not have the word Error.
addQuery('short_description', 'DOES NOT CONTAIN', 'Error');

NOT IN	 Takes a map of values that allows commas, and gathers a collection of records that meet some other requirement. Behaves as: Select * from <table> where short_description NOT IN ('Error').	 
addQuery('short_description', 'NOT IN', 'Error,Success,Failure');

INSTANCEOF	 Special operator that allows you to retrieve only records of a specified "class" for tables which are extended.  For example when going after configuration items (cmdb_ci table) you many want to retrieve all configuration items that are have are classified as computers.  The code to the right will do that.	
addQuery('sys_class_name', 'INSTANCEOF', 'cmdb_ci_computer');
*/