petercawdron
Kilo Guru

The ServiceNow CMDB has a concept of recommended fields (rather than required/mandatory) which are used to assess the health of CIs, but when users are managing their CIs there's no way to know which fields are recommended.

find_real_file.png

This global on load client script will automatically highlight any field that is recommended, making it easy for users to complete.

function onLoad() {

	if(g_form.getTableName().indexOf('cmdb_ci')==0){
		var gr = new GlideRecord('cmdb_recommended_fields');
		gr.addQuery('active','true');
		gr.addQuery('table',g_form.getTableName());
		gr.query(displayResults);
	}

	function displayResults(response){
 
		response.rows.forEach(function(thisRow){

			thisRow.forEach(function(thisColumn){

				if(thisColumn.name=='recommended'){
					var thisField = jQuery('input[id="sys_display.'+g_form.getTableName()+'.'+thisColumn.value+'"]').length ==0 ?   jQuery('input[id="'+g_form.getTableName()+'.'+thisColumn.value+'"]'): jQuery('input[id="sys_display.'+g_form.getTableName()+'.'+thisColumn.value+'"]');
					thisField.css('background','lightyellow');	
				}	
			});
		});
	}
}
Comments
tana_stewart
Tera Contributor

This is great! I was looking for something similar for identifier entries, when a data source has a reconciliation (precedence rule) for the class, so that manually you could see: without these fields we may see duplicates.

peter_cawdron_p
Giga Expert

Hey, glad you found it useful 🙂

psyherin
Kilo Sage

Hi @petercawdron ,

Thanks for the information and it is very helpful. I have added this script but it doesn't seem to work. I have configured some recommended fields but they are not highlighted on the form. I am currently testing in PDI which is on San Diego.

 

Any pointers ?

 

Thanks 

Brad Bowman
Kilo Patron
Kilo Patron

Updated version, removing Client Script GlideRecord. Native UI only.

BradBowman_0-1727260579445.png

 

 

 

function onLoad() {
	var tableName = g_form.getTableName();
	
    if (tableName.indexOf('cmdb_ci') == 0) {
		var ga = new GlideAjax('CMDBUtils');
		ga.addParam('sysparm_name','checkRecFields');
		ga.addParam('sysparm_tablename',tableName);
		ga.getXML(getResponse);		
	}

	function getResponse(response) {
		var answer = response.responseXML.documentElement.getAttribute("answer");
		var thisColumn = answer.split(',');
		for (i=0; i<thisColumn.length; i++) {
			var thisField = jQuery('input[id="sys_display.'+tableName+'.'+thisColumn[i]+'"]').length ==0 ?   jQuery('input[id="'+tableName+'.'+thisColumn[i]+'"]'): jQuery('input[id="sys_display.'+tableName+'.'+thisColumn[i]+'"]');
			thisField.css('background','lightyellow');
		}
    }
}

 

 

The Script Include, with Client callable box checked:

 

 

var CMDBUtils = Class.create();
CMDBUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	checkRecFields: function() {
		var answer = 'none';
		var tablename = this.getParameter('sysparm_tablename');
		var gr = new GlideRecord('cmdb_recommended_fields');
		gr.addActiveQuery();
		gr.addQuery('table', tablename);
		gr.query();
		if (gr.next()) {
			answer = gr.recommended;
		}
		return answer;
	},

    type: 'CMDBUtils'
});

 

BradBowman_2-1727260753270.png

BradBowman_1-1727260704979.png

 

Version history
Last update:
‎09-02-2019 08:39 PM
Updated by: