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

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"

Thanks Abhijit. The sow_new_record worked for new record. Can we set multiple views instead of duplicating the client script, one of existing vs. one for new??

Not sure, but I guess if you keep it blank it should work for all views, please refer to documentation 

KingSukh
Tera Expert

The code below does not work in sow_new_record view Interaction Management for Service Operations Workspace Application Scope, it does not work in Global on the new Record either. It is highlighting the field but not setting the values on New Record page. UI Type is set to All.

 

function onChange(control, oldValue, newValue, isLoading) {
if (!g_form.hasField('opened_for'))
return;
if (!newValue)
return;

g_form.getReference('opened_for', ref);

function ref(user) {
if (user.vip == 'true') {
g_form.setValue('vip', user.vip.toString());
g_form.showFieldMsg('opened_for', 'This is a VIP user', 'error');
}
if (g_form.hasField('user_name')) {
g_form.setValue('user_name', user.user_name.toString());
}
}
}