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.

How to invoke a client script onchange for a new record (Incident) in Service Operations Workspace?

Think Code
Tera Contributor

Greetings all

 

My client script for onchange is getting invoked on existing record, however for a new record, neither onLoad or onChange doesnt seem to fire. Am I missing something obvious? My end goal is to display the assigned user Asset details on a field message. I am a n00b to servicenow world and to service operations workspace.

 

UPDATE : It only works when UI Type is set to ALL and is applied in Global scope and Isolate Script is checked OFF.  The ServiceNow version is Tokyo. I wanted it only for SOW.

 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
alert ("In Func Affected System Name Prefetch Before IsLoading Check");
   if (isLoading || newValue === '') {
      return;	   
   }
 alert ("In Func Affected System Name Prefetch : old:" + oldValue + " ; new: " +  newValue);
 if(oldValue != newValue)
  {
	var userIdCaller = g_form.getValue('u_affected_end_user');
	//console.log(userIdCaller);
    var gaCaller = new GlideAjax('AjaxGetAssetValuesByUser');
    gaCaller.addParam('sysparm_name', 'getFieldValues_JSON');
    gaCaller.addParam('sysparm_fields', 'ci,model_category');
    gaCaller.addParam('sysparm_sys_id', userIdCaller);
    gaCaller.getXML(CallbackAsset);
  }
   
 }

function CallbackAsset(response) {
    var answer = response.responseXML.documentElement.getAttribute('answer');
    var parsedData = JSON.parse(answer);
	var dataLen = parsedData.length;
	var result = [];
	for (var i = 0; i < dataLen; i++) {
		result.push(parsedData[i].model_category + ": " + parsedData[i].ci);
	}
	var text = 'System: ' + result.join(', ');
	//clear the previous message
	g_form.hideFieldMsg('u_affected_system_name', true);
	//show the system names as comma seperated text in a warning block
	g_form.showFieldMsg('u_affected_system_name', text, 'warning');
	
}

 

 
1 ACCEPTED SOLUTION

Abhijit
Tera Expert

My suggestion to you is make copy client script separately for SOW workspace ,

 

Abhijit_0-1691161158083.png

Above is OOB client script on "Resolution details visibility on state"

View solution in original post

8 REPLIES 8

Alex Rose
Tera Guru

Hi there,

Could you please verify the UI Type of the client script?  I would try setting it to All if it isn't already.

Thanks for your response, Alex. It seems to only work when UI Type is set to ALL and is applied in Global scope and Isolate Script is checked OFF.  The ServiceNow version is Tokyo. I wanted this functionality only for SOW forms, but cannot seem to apply only on those views.

Abhijit
Tera Expert

I do not understand your exact query, but when form is loading then below section is getting called , onchange only invoke on fieldvalue is changed

 

if (isLoading || newValue === '') {
      return;	   
   }

 

isloading() function is getting called when form is loading, I would suggest call your GlideAjax in above if loop as well.

That is my issue - the form for Create New Incident in SOW, the onchange is not invoked, even on field value change, until it is in Global Scope and Isolate Script is checked off.  For a normal edit of existing record, it seems to work with me specifying a view and Isolate Script is checked on. I do not want to apply this onchange globally, but only in SOW. Searching for means on how to do it selectively in SOW. Thanks.