- 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:12 AM
Hi,
You can utilize onChange() Catalog Client Script on your Requested For variable.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
g_form.getReference('requested_for', callBack);
}
function callBack(grUser){
if(grUser.u_type == 'Contractor')
g_form.setValue('worker_type', '1');
else if(grUser.u_type == 'Employee')
g_form.setValue('worker_type', '2');
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-29-2022 06:30 AM
Hi
Thanks for the reply!
I tried similar code , I used a variable usr = g_form.getReference('requested_for', callBack);
function callBack(usr){
if(usr.u_type == 'Contractor')
g_form.setValue('worker_type', '1');
else if(usr.u_type == 'Employee')
g_form.setValue('worker_type', '2');
}
But this didn't work.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-29-2022 06:37 AM
Did you create a variable of type Requested For in your catalog item ?
Is it visible/available on the catalog item form ?
u_type string values are exactly matching ?
worker_type backend choice values are also matching ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-29-2022 06:44 AM
variable "Requested for" is of type "Reference" and is referring to user table, and is visible on catalog item form,.
It is not of type "requested for". Rest all are matching