Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Catalog client script is not working

Rosy14
Kilo Sage

Hi,

I have a ref type variable. Based on that field the first and last name will be populated. below script is not working. Please help.

 

Rosy14_0-1756805648449.png

Client script :
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue == '') {
        return;
    }
    var ga = new GlideAjax('getAffectedUserDetails');
    ga.addParam('sysparm_name', 'getUserFirstName');
    ga.addParam('sysparm_user_id', newValue);
    ga.getXML(SetFirstName_ref);
	
    function SetFirstName_ref(response) {
        var first_name = response.responseXML.documentElement.getAttribute("answer");
        g_form.setValue('first_name', first_name);
    }

    ga = new GlideAjax('getAffectedUserDetails');
    ga.addParam('sysparm_name', 'getUserLastName');
    ga.addParam('sysparm_user_id', newValue);
    ga.getXML(SetLastName_ref);

    function SetLastName_ref(response) {
        var last_name = response.responseXML.documentElement.getAttribute("answer");
        g_form.setValue('last_name', last_name);
    }
}

Rosy14_1-1756805667912.png

Script include:

var getAffectedUserDetails = Class.create();
getAffectedUserDetails.prototype = {
    initialize: function() {},
    
    getUserFirstName: function() {
		gs.addInfoMessage("test");
        var user_sys_id = this.getParameter('sysparm_user_id');
        var user = new GlideRecord('sys_user');
        user.addQuery("sys_id", user_sys_id);
        user.query();
        if (user.next()) {
            //gs.addInfoMessage(user_sys_id);
            return user.getValue("first_name");
        }

    },
    getUserLastName: function() {
        var user_sys_id = this.getParameter('sysparm_user_id');
        var user = new GlideRecord('sys_user');
        user.addQuery("sys_id", user_sys_id);
        user.query();
        if (user.next())
            return user.getValue("last_name");

    },
    type: 'getAffectedUserDetails'
};

 

 

 

 

 

1 ACCEPTED SOLUTION

Rosy14
Kilo Sage
After adding global.AbstactAjaxProcessor it is working.
 
getAffectedUserDetails.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

View solution in original post

13 REPLIES 13

Mark Manders
Mega Patron

Is your variable the reference to the user table? Because that would make the most sense, otherwise it will never work, if you can just add any email address in there, it is prone to mistakes and you won't ever get the name from the user record.

Assuming my assumption is correct, you can just add the name fields as dependent on the email address. 

 

However, why would you ever need these two values separately in two different variables? Why not just handle that after the item is submitted, since the name is always the same and coming from the user table?

 

What is your business case here?


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

Ankur Bawiskar
Tera Patron
Tera Patron

@Rosy14 

no scripting required if

1) your 1st variable is reference to sys_user

2) other 2 variables are of string type

You can use auto populate feature which came from Utah onwards

Auto-populate a variable based on a reference type variable (Utah) 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Rosy14 

Thank you for marking my response as helpful.

I believe I also answered your question and it has no scripting in it.

As per new community feature you can mark multiple responses as correct.

If my response helped please mark it correct as well so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Rosy14
Kilo Sage
After adding global.AbstactAjaxProcessor it is working.
 
getAffectedUserDetails.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {