Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Search sys_id in entire Servicenow instance

balu3
Kilo Guru

Hi,

I have a doubt regarding searching records in Servicenow. Given the sys_id and the table, there is a possibility to get the corresponding record. My doubt is given only the sys_id, is it possible to get a record corresponding to the sys_id?

I'm asking this because I read that the sys_id is unique across an instance. So given a sys_id, there must be only one corresponding record in an entire instance.

I'd like to know the possibility using code (Glide API), and searching in UI.

Please assist.

Thanks in advance.

17 REPLIES 17

Parneet1
Tera Expert

This statement is incorrect that given a sys_id, there must be only one corresponding record in an entire instance. The same sys_id can exist for 2 or more records, but those records would not belong to the same table. In one table, every record will have a unique sys_id, not necessarily in 1 instance.

I am so sorry but I would have to disagree with you on that. Here's a screenshot from the docs site-

find_real_file.png

 

Bravo

Rajender1
Mega Sage
Mega Sage

Hi in the background script use below code (**do not run in production instance**)and do some appropriate modifications as per your requirement

var grIncident = new GlideRecord('incident');
var returnValue = grIncident.get('27cf23772fc64c108b3157ab2799b6fb');
gs.info(returnValue); // logs true or false
gs.info(grIncident.number);

Mark correct and helpful if you able resolve issue

Thanks

Rajender

senthilvaithees
Tera Guru

Use the below code to search for sys id 

var GlobalSearch = Class.create();

GlobalSearch.prototype = Object.extendsObject(AbstractAjaxProcessor, { 
    bySysId: function(sys_id){
		var response = [];
		response[0] = false;
		var tables = new GlideRecord('sys_db_object');
		tables.addNullQuery('super_class');
		// This table doesn't admit aggregated queries
		tables.addQuery('name','!=','sn_m2m_note_template_for_table');
		
		// These tables don't have sys_ids
		tables.addQuery('name','!=','pa_scores_l1');
		tables.addQuery('name','!=','pa_scores_l2');
		tables.addQuery('name','!=','sys_rollback_incremental');
		tables.addQuery('name','!=','sys_rollback_sequence');
		tables.addQuery('name','!=','pa_migration_ignored_scores');
		tables.addQuery('name','!=','pa_favorites');
		tables.orderBy('name');
		tables.query();

		var avoidTables = /^(ts_|v_|sn_hr_)/;
			
		while(tables.next()){
			var table = tables.getValue('name');
			try{
				if(!table.match(avoidTables)){
					var agg = new GlideAggregate(table);
					agg.addAggregate('COUNT');
					agg.addQuery('sys_id',sys_id);
					agg.query();

					if(agg.next())
						if(agg.getAggregate('COUNT') > 0){
							response[0] = true;
							response[1] = tables.getValue('name');
							
							return response;
						}
				}
			}catch(e){
				// Some tables might cause exceptions we don't need to hear about
			}
		}
		return response;
	},
    type: 'GlobalSearch'
});