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

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 ?

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

}

Hi @Kalyani Jangam ,

Thanks!

Just curious to know why the callback and get reference method didn't work here. Could you please let me know the reason?

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){

 

}