variable set info to autofill variable in schema

asd22
Tera Contributor

Hello

 

i got a variable set in my schema named "employee info"

in this set i got "employee_name" and "employee_id"

When a name is selected it auto fills in the ID. i got a variable in my schema that is "employee_department".

How do i make it so when i select a user name in the field thats in the variable set, it also auto fill out the variable in the schema?

1 REPLY 1

aKartik
Tera Expert

From what UI understand it seems like you have variables "employee_name" and "employee_id" in Variable set and "employee_department" outside variable set on the item variable list. Considering you are populating the "employee_id" from"employee_name" using Auto-populate tab on the employee_id variable as shown below.

Screenshot 2023-11-03 at 7.31.52 PM.png

To populate the department field which is outside the variable set either you can move the variable into the variable set and use this same method or you will have to use the AJAX.

Client Script:

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var id = g_form.getValue('emp_name'); //from the variable set

    var ga = new GlideAjax('sampleUtils'); //Scriptinclude
    ga.addParam('sysparm_name', 'getUserDetails'); //Method
    ga.addParam('sysparm_userId', id); //Parameters- sys id of the user
    ga.getXMLAnswer(getResponse);

    function getResponse(response) {
        g_form.setValue('emp_dept', response);
    }
}

 

 

Script Include:
(make sure to check the client callable true)

 

var sampleUtils = Class.create();
sampleUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getUserDetails: function() {

        var userId = this.getParameter('sysparm_userId'); //sys id of user
        var deptt;
        var gr = new GlideRecord('sys_user');
        gr.get(userId);
        deptt = gr.department.name; //dot walk department name
        if (deptt) {
            return deptt;
        } else
            return '';
    },
    type: 'sampleUtils'
});

 

 

 If this response clears up your doubt, kindly flag it as both helpful and correct.

Thanks