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

Chaitanya ILCR
Mega Patron

Hi @Rosy14 ,

 

update the script include as below

 

client callable script include should always extend from "AbstractAjaxProcessor"

var getAffectedUserDetails = Class.create();
getAffectedUserDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
    getUserDetails: function() {
        var res = {};
        gs.info("getAffectedUserDetails test");
        var user = new GlideRecord('sys_user');
        if (user.get(this.getParameter('sysparm_user_id'))) {

            res.first_name = user.getValue('first_name')
            res.last_name = user.getValue('last_name')

        }
        return JSON.stringify(res)

    },

    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'
});

 

 

you don't have to call the server 2 times you can get the response in one call (this is not the issue though)

you can update the client script as (this is optional the issue is with the script include but as a better approach you can update the client script as below)

 

 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue == '') {
        return;
    }
    var ga = new GlideAjax('getAffectedUserDetails');
    ga.addParam('sysparm_name', 'getUserDetails');
    ga.addParam('sysparm_user_id', newValue);
    ga.getXMLAnswer(SetFirstName_ref);

    function SetFirstName_ref(answer) {
        if (answer) {
            var userDetails = JSON.parse(answer)
            g_form.setValue('first_name', userDetails.first_name);
            g_form.setValue('last_name', userDetails.last_name);
        } else {
            alert('no response')
        }

    }

}

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

 

It is still not calling

Add logging and provide more information about what you do and don't get. Repeating 'it's not calling' is not going to help us in helping you.


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

The script include is not calling from client script. tried to show one info message from this.

 

As mentioned added this on script include.

 

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

Hi @Rosy14 ,

did you update both client script and script and check?

 

Alos check if there any ACL on the script include and the user you are testing with have the role

go to ACLs and check name contains the name of the script include and operation = execute and type is client_callable_script_include

ChaitanyaILCR_0-1756808051464.png

 

 

OR 

 

you can try the no-code auto populate functionality here (refer below blogs)

Using the Variable Auto-populate Feature with Vari... - ServiceNow Community

Auto-populate a variable based on a reference type... - ServiceNow Community

 

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya