Some PDIs are currently unavailable, and PDI actions are paused. View the latest updates here. Read More

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

14 REPLIES 14

Hi @F_bio Santos 

Please refer to my reply above that mentions the GlideAjax cheat sheet for you to compare your code as well as my suggestion as it relates to the "client callable" script include. If you're curious about what code might be missing, open a brand new script include, don't save, just click and unclick the client callable checkbox, you'll see some additional code is added for client callable script includes. With the box checked, compare against your original script include and you'll see the gap.

 

The above may not be the only issue you have in your code...so please refer to the cheat sheet as mentioned.


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

Hi @F_bio Santos 

I'm glad you found a solution that worked for you, but the goal here, at least for me with helping you, is for you to learn the actual issue, learn to troubleshoot, and learn how to fix it. Being provided the code to use is great and all, but that doesn't help you in the long term.

 

As I mentioned below, it's quite unfortunate that none of my replies have been marked with any sort of indication of acknowledgement that it was Helpful and/or helped guide you to a solution.

 

Thanks!


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

Hi @Allen Andreas I actually did what you wrote in the comment above and it helped, I accepted the other solution because it worked even tho I didnt used the code that was provided like you said, it helped me knowing what I was doing wrong.

Hi Allen, hope you are doing well !!

Pls help me with the correct script of the below problem:-

 

(Q) When I change the Caller name in the Incident form, the Manager Field, Assigned To & Assignment Group, should also get changed for that User/Caller ?

 

(A) For this I had written 2 scripts but are giving null value on alert, can you pls provide the correct & simple scripting for the same, without using JSON or parse or any other complex method. As I am just learning SNOW.

 

(1) 

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

    getUserDetails: function() {

 var callerValue = this.getParameter('sysparm_caller');
 var callerAssiGr = this.getParameter('sysparm_assignment_group');
 var callerAssiTo = this.getParameter('sysparm_assigned_to');

var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', callerValue + callerAssiGr + callerAssiTo);
gr.query();

if (gr.next()) {
    return gr.getValue('assignment_group' + 'assigned_to');
}
},
    type: 'fetchUserFieldsOnForm'
});
 
(2) 
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
    return;  
  }
   var ga = new GlideAjax('fetchUserFieldsOnForm');
   ga.addParam('sysparm_name','getUserDetails');
   ga.addParam('sysparm_caller', g_form.getValue('caller_id'));
   ga.addParam('sysparm_caller', g_form.getValue('assignment_group'));
   ga.addParam('sysparm_caller', g_form.getValue('assigned_to'));
   ga.getXML(test);

   function  test(response){
    var answer = response.responseXML.documentElement.getAttribute('answer');
    alert (answer);
    g_form.setValue('assignment_group', answer);
    g_form.setValue('assigned_to', answer);
   }
  }

Servicenow34
Tera Guru

Hey @F_bio Santos 

You can use getReference in client script.

 

Thank you!