How to get value from selected Watch List user?

Andrew61
Kilo Contributor

So I'm fairly new to SN and JavaScript. I'm attempting to add an alert to notify if the user adds someone to the Watch List who isn't from the same company that was assigned to the Incident. What I'm having trouble with is how I can grab the selected Watch List user's company value to compare with the Incident's company value. Below is what I have so far with the client script.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	// Show info message popup to notify that the user being added to the watchlist is not from the same company as the incident creator
	
	if (isLoading || newValue === '') {
		return;
	}
	
	var watchListUser = g_form.getValue('watch_list');
	var watchListUserCompany = [???];
 	var incidentCompany = g_form.getDisplayBox('company').value;
	
  	if (watchListUserCompany != incidentCompany) {
  		alert("watchListUser: " + watchListUser + ". "
			  + "watchListUserCompany: " + watchListUserCompany + ". "
			  + "incidentCompany: " + incidentCompany +". ");
 	}
}

Any help is appreciated!

1 REPLY 1

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hello Andrew,

 

As the watch list output will be a user record sys_id, you will have to do GlideRecord to the sys_user table and get the company field value. This can be achieved via GlideAjax if you want to this operation via Client Script. More info here.

 

https://docs.servicenow.com/bundle/helsinki-application-development/page/script/server-scripting/reference/r_GlideAjax.html

https://community.servicenow.com/community?id=community_blog&sys_id=f8ccee25dbd0dbc01dcaf3231f961978

https://docs.servicenow.com/bundle/geneva-servicenow-platform/page/script/server_scripting/reference/r_ExamplesOfAsynchronousGlideAjax.html

 

Thanks,

Pradeep Sharma