Get values from "sys_user" using script Include

F_bio Santos
Kilo Sage

Hi everyone, Im trying to make a client script, that will change the fields "email" and "country" when the "assigned_to" changes.

Im doing a Client Script "OnChange()", calling a script include, I will leave the code that Im using bellow.

Script Include:

 

var UserInfo = Class.create();
UserInfo.prototype = {
    initialize: function() {},

    getUserInfo: function(userId) {
        var gr = new GlideRecord('sys_user');
        gr.addQuery('sys_id', userId);
        gr.query();

        if (gr.next()) {
            var results = {
                "country": gr.location.country.getDisplayValue(),
                "email": gr.getValue('email')
            };
        }
        return JSON.stringify(results);
    },

    type: 'UserInfo'
};

 


Client Script:

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }

    g_form.addInfoMessage('Assigned to changed. New Value: ' + newValue);

    var userSysId = newValue;

    if (userSysId) {
        var ga = new GlideAjax('UserInfo');
        ga.addParam('sysparm_name', 'getUserInfo');
        ga.addParam('userId', userSysId);
        ga.getXMLAnswer(response);
    }

    g_form.addInfoMessage(response + 'TesteResponse');

    function response(answer) {
        if (answer) {
            g_form.addInfoMessage('Running the IF');
            var returneddata = JSON.parse(answer);
            g_form.setValue("u_assigned_to_country", returneddata.country);
            g_form.setValue("u_assigned_to_email", returneddata.email);
        }
    }
}

 


When I change the assigned_to nothing happens, and it is not running the "if(answer)"
 

1 ACCEPTED SOLUTION

Servicenow34
Tera Guru

Hey @F_bio Santos 

 

Kindly find below code. This is working for me.

 

Client Script

 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    //Type appropriate comment here, and begin script below
    g_form.addInfoMessage('Assigned to changed. New Value: ' + newValue);

    var userSysId = newValue;

    if (userSysId) {
        var ga = new GlideAjax('UserInfo');
        ga.addParam('sysparm_name', 'getUserInfo');
        ga.addParam('sysparam_assignedto', userSysId);
        ga.getXML(callback);
    }

    // g_form.addInfoMessage(response + 'TesteResponse');

    function callback(response) {

        var answer = response.responseXML.documentElement.getAttribute("answer");
        var returneddata = JSON.parse(answer);
        g_form.setValue("u_email", returneddata.email);
        g_form.setValue("u_country", returneddata.country);
    }


}

 

 

 

 

Script Include (**Make sure it is client callable)

 

 

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

getUserInfo: function() {

		var userId= this.getParameter('sysparam_assignedto');
        var gr = new GlideRecord('sys_user');
        gr.addQuery('sys_id', userId);
        gr.query();

        if (gr.next()) {
            var jsonObj = {};
                jsonObj.country= gr.location.country.getDisplayValue(),
                jsonObj.email= gr.getValue('email');
          
        }
        return JSON.stringify(jsonObj);
    },


    type: 'UserInfo'
});

 

 

 

 

Output:

Assigned to.png

 

Kindly mark my reply as Helpful and/or Accept Solution, if applicable. Thanks!

View solution in original post

13 REPLIES 13

Servicenow34
Tera Guru

Hey @F_bio Santos 

 

Kindly find below code. This is working for me.

 

Client Script

 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    //Type appropriate comment here, and begin script below
    g_form.addInfoMessage('Assigned to changed. New Value: ' + newValue);

    var userSysId = newValue;

    if (userSysId) {
        var ga = new GlideAjax('UserInfo');
        ga.addParam('sysparm_name', 'getUserInfo');
        ga.addParam('sysparam_assignedto', userSysId);
        ga.getXML(callback);
    }

    // g_form.addInfoMessage(response + 'TesteResponse');

    function callback(response) {

        var answer = response.responseXML.documentElement.getAttribute("answer");
        var returneddata = JSON.parse(answer);
        g_form.setValue("u_email", returneddata.email);
        g_form.setValue("u_country", returneddata.country);
    }


}

 

 

 

 

Script Include (**Make sure it is client callable)

 

 

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

getUserInfo: function() {

		var userId= this.getParameter('sysparam_assignedto');
        var gr = new GlideRecord('sys_user');
        gr.addQuery('sys_id', userId);
        gr.query();

        if (gr.next()) {
            var jsonObj = {};
                jsonObj.country= gr.location.country.getDisplayValue(),
                jsonObj.email= gr.getValue('email');
          
        }
        return JSON.stringify(jsonObj);
    },


    type: 'UserInfo'
});

 

 

 

 

Output:

Assigned to.png

 

Kindly mark my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Yup, it worked I did some changes to the code, to better suit what I needed but it worked, also I was forgetting to check the "Client Callable" on the script include ...

oh okay !

Thanks for the acceptance 🙂

Hi @F_bio Santos 

Yes, which is what I called out above.

It's unfortunate that none of my replies were marked as "Accept Solution" consider the information I provided or at a minimum...Helpful.

 

I'm glad the reply above helped you, but it copies a lot of what I already said even including a line from my own signature at the bottom...lol...

 

Take care!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!