Recommended Fields on load

EDCT
Tera Contributor

Hi, 

I found an on load script to highlight fields which are recommended on a CI.

However the load script did not work and the user is no longer active on this forum. 

Can anybody advise an issue in the script, or a way to highlight a recommended field on a CI form view.

Thanks

 

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');	
				}	
			});
		});
	}
}

 CI Class Manager - Recommended Fields - ServiceNow Community

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

Here is a slightly better way to do the same, at least eliminating the Client Script GlideRecord.  This will work in the native UI.

BradBowman_0-1727260597395.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-1727260782295.png

 

 

BradBowman_1-1727260667857.png

 

 

 

 

 

View solution in original post

15 REPLIES 15

Akash4
Kilo Sage
Kilo Sage

Hello,

Maybe you can use g_form instead of jQuery. Here is a modified script with corrections.

 

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(function (grResult) {
while (grResult.next()) {
var recommendedField = grResult.getValue('field');
if (g_form.hasField(recommendedField)) {
g_form.getControl(recommendedField).style.backgroundColor = 'lightyellow';
}
}
});
}
}

 

Regards, Akash
If my response proves useful, please mark it "Accept as Solution" and "Helpful". This action benefits both the community and me.

EDCT
Tera Contributor

Unfortunately this code did not work for me either. Trying this in a Vancouver PDI first.  

Brad Bowman
Kilo Patron
Kilo Patron

This Client Script contains a GlideRecord and DOM manipulation, both of which are not best practice / supported / etc.  Having said that, it might still work in the native UI, not Service Portal / ESC / a workspace / ... Ensure the Isolate script box is present on the Client Script form and unchecked.  Try adding some alerts to see if the script enters the function, and how far it is getting.

Brad Bowman
Kilo Patron
Kilo Patron

Here is a slightly better way to do the same, at least eliminating the Client Script GlideRecord.  This will work in the native UI.

BradBowman_0-1727260597395.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-1727260782295.png

 

 

BradBowman_1-1727260667857.png