- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2022 04:47 AM
Hello,
We have a catalog item and there is a field "Requested for" on this item that refers to "sys_user" table
We have a field "u_type" on user table which is of string type. usually this string field takes two main values.
1- Contractor
2- Employee.
We have a variable "worker type" which is a select box, has choice values 1 and 2
Requirement is that, based on the "u_type" on user table, the worker type should be autopopulated and be a read only field.
When we select a "Requested for" , and if this person has u_type as Contractor, then in RITM , worker type should be set as 1 (contractor), and if u_type of this person on user table is "Employee", then worker type should be set as 2 (employee). Can someone provide me the script for this please?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2022 06:29 AM
Hi Please use onchange client script using GlideAjax
Script include code
checkType: function() {
var num_user = this.getParameter('sysparm_user');
var gr = new GlideRecord("sys_user");
gr.addQuery("sys_id", num_user);
gr.query();
if (gr.next()) {
return gr.u_type;
}
***********************************************************************
Onchange Client script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ajax = new GlideAjax('Validation');
ajax.addParam('sysparm_name', 'checkType');
ajax.addParam('sysparm_user', newValue);
ajax.getXML(populateCampus);
function populateCampus(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
alert("Onload answer-----" + answer);
if(answer=='employee'){
alert("In New CLient script-----");
g_form.setValue('requested_user_type','employee');
g_form.setReadOnly('requested_user_type',true);
}
else{
g_form.setValue('requested_user_type','contract');
g_form.setReadOnly('requested_user_type',true);
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2022 06:56 AM
Well, it is working for me.
Are you getting any logs in browser console ?
And your onChange() client script is on Requested For variable, right ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2022 06:29 AM
Hi Please use onchange client script using GlideAjax
Script include code
checkType: function() {
var num_user = this.getParameter('sysparm_user');
var gr = new GlideRecord("sys_user");
gr.addQuery("sys_id", num_user);
gr.query();
if (gr.next()) {
return gr.u_type;
}
***********************************************************************
Onchange Client script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ajax = new GlideAjax('Validation');
ajax.addParam('sysparm_name', 'checkType');
ajax.addParam('sysparm_user', newValue);
ajax.getXML(populateCampus);
function populateCampus(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
alert("Onload answer-----" + answer);
if(answer=='employee'){
alert("In New CLient script-----");
g_form.setValue('requested_user_type','employee');
g_form.setReadOnly('requested_user_type',true);
}
else{
g_form.setValue('requested_user_type','contract');
g_form.setReadOnly('requested_user_type',true);
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2022 06:34 AM
Hi
Thanks!
Just curious to know why the callback and get reference method didn't work here. Could you please let me know the reason?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2022 07:56 AM
Generally, we never use get reference if we want value from reference field because it not include in best practice in servicenow. In above example it should like below
var grUser=g_form.getReference('requested_for', callBack);// variable declaration missing
function callBack(grUser){
}