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

Anubhav24
Mega Sage
Mega Sage

Hi @F_bio Santos ,

What is the issue you are facing?

Also in your script include you need to receive the param set in your client script:

var user= this.getParameter('userid');

Your script include function should look like this :

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

Also make sure the script include created is marked as client callable.

Please mark helpful/correct if my response helped you.

 

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

Allen Andreas
Administrator
Administrator

Hi @F_bio Santos 

Unfortunately, you didn't provide any information as to what your issue is...is it not working? Is it throwing an error? What do you think is the problem? etc.

 

Instead, you've pasted your code here without any indication from you as to what you're stuck on.

 

Please refer to the this GlideAjax cheat sheet: https://www.servicenow.com/community/developer-articles/glideajax-example-cheat-sheet-updated/ta-p/2...  -- and consider reviewing your script include for the "client callable" checkbox to be check prior to having saved the record when you initially create it. If you don't check the box prior to saving, your script include will not be created properly with the correct script that enables this for being client callable.


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

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