autopopulate choice field based on reference field (string field within reference field's form)

Demo24
Tera Expert

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?

1 ACCEPTED SOLUTION

Kalyani Jangam1
Mega Sage
Mega Sage

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);
        }
    }

}

View solution in original post

8 REPLIES 8

Muhammad Khan
Mega Sage
Mega Sage

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');
}

 

Hi @Muhammad Khan ,

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. 

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 ?

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