Show specific table records in Document iD field type

aagarwal59
Tera Contributor

The requirement is to show active table records for a document ID field 

5 REPLIES 5

Bert_c1
Kilo Patron

Query each of the tables returned by:

 

https://[instance].service-now.com/sys_dictionary_list.do?sysparm_query=internal_type%3Ddocument_id&sysparm_view=advanced 

 

The requirement is to show only active records from the selected table in Document ID field

For Ex: I need to show only active incident records if user select incident table in Document ID field

Bert_c1
Kilo Patron

Document ID fields are tricky, since they depend on a reference table. and without more details on which tables and fields it hard to recommend a good solution. I did come up with a business rule for my 'test table' that has 'u_document_table' column for the reference table, and 'u_document_id' column for the Document ID field. the BR aborts the update if an in-active record is selected.

 

Screenshot 2025-08-22 120002.png

The script logic follows:

(function executeRule(current, previous /*null when async*/) {

	// Add your code here
	var refTable = current.u_document_table;
//	gs.info("CheckTable: reference table is: " + refTable);

	// see if table is extended from task
	var table = new TableUtils(refTable);
	var baseTable = table.getAbsoluteBase();
	if (baseTable == 'task')
		refTable = 'task';

	// see if the table has an 'active' field
	var dict = new GlideRecord('sys_dictionary');
	dict.addQuery('name', refTable);
	dict.addQuery('element', 'active');
	dict.query();
	if (!dict.next())
		return;		// don't check
//	gs.info("CheckTable: reference table has active field.");

	var rec = new GlideRecord(refTable);
	rec.addQuery('sys_id', current.u_document_id);
	rec.query();
//	gs.info("CheckTable: Found " + rec.getRowCount() + " records.");
	if (rec.next()) {
		result = rec.active;
//		gs.info('CheckTable: active = '+ result);
		if (!rec.active) {
			gs.addInfoMessage('Record is not active');
			current.setAbortAction(true);
		}
	}

})(current, previous);

You will have to change the table, reference field and document id field names for your use-case. And there are likely better approaches depending on specific details of your use-case.