Calling Script Include from Client Script error - "There is a JavaScript error in your browser"

mamason
Tera Contributor

Not sure what the issue is.  I am able to call the script include from the window Scripts - Background and works as expected so I am assuming the issue is with how I am calling from client script.  I do have the script include Client Callable box checked.

 

Client Script

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
        varUser = g_form.getValue('add_user_to_group');
        varaddedUser = varUser.split(',').pop();

            var ga = new GlideAjax('CheckUserLicense');
            ga.addParam('sysparm_name', 'getUserLicense');
            ga.addParam('userId',varaddedUser);
            callTask.getXML(licenseResult);

            function licenseResult(response) {
            var userLic = response.responseXML.documentElement.getAttribute('answer');
            alert(userLic);
        }
           
}
 
SCRIPT INCLUDE
var CheckUserLicense = Class.create();
CheckUserLicense.prototype = Object.extendsObject(AbstractAjaxProcessor,{
    initialize: function() {
    },

    getUserLicense: function(userId){
        //var userId = this.getParameter('userId');
        var result = '';
        var grUser = new GlideRecord("sys_user_has_role");
        grUser.addEncodedQuery('user.sys_id=' + userId + '^role.nameLIKEitil');
        grUser.query();
        if(grUser.next()){
            result =  "license";
        }
        else
        {
            result = "noLicense";
        }
        return result;
    },

    type: 'CheckUserLicense'
});
 
CALLING SCRIPT INCLUDE FROM SCRIPTS - BACKGROUND

var CheckUserLicense = new CheckUserLicense();

var result = CheckUserLicense.getUserLicense('6cccca19f4ead980776706f2f4caa3c6');

gs.print(result);

 

Depending on the user it will return string of license or noLicense but when calling from client script on the change of the variable I get "There is a JavaScript error in your browser"

 

Any Help would be greatly appreciated!

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@mamason 

Things I would like to inform

1) Client callable script include should not have initialize() method

2) I believe you are trying to re-use the same function from client and server so update as this

Script Include: It should be client callable

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

    getUserLicense: function(uId) {
        var userId = this.getParameter('sysparm_userId') || uId;
        var result = '';
        var grUser = new GlideRecord("sys_user_has_role");
        grUser.addEncodedQuery('user.sys_id=' + userId + '^role.nameLIKEitil');
        grUser.query();
        if (grUser.next()) {
            result = "license";
        } else {
            result = "noLicense";
        }
        return result;
    },

    type: 'CheckUserLicense'
});

AnkurBawiskar_0-1744340595096.png

 

Client Script:

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

    var varvarUser = g_form.getValue('add_user_to_group');
    var varaddedUser = varUser.split(',').pop();
    var ga = new GlideAjax('CheckUserLicense');
    ga.addParam('sysparm_name', 'getUserLicense');
    ga.addParam('sysparm_userId', varaddedUser);
    ga.getXML(licenseResult);

    function licenseResult(response) {
        var userLic = response.responseXML.documentElement.getAttribute('answer');
        alert(userLic);
    }
}

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

View solution in original post

6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

@mamason 

Things I would like to inform

1) Client callable script include should not have initialize() method

2) I believe you are trying to re-use the same function from client and server so update as this

Script Include: It should be client callable

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

    getUserLicense: function(uId) {
        var userId = this.getParameter('sysparm_userId') || uId;
        var result = '';
        var grUser = new GlideRecord("sys_user_has_role");
        grUser.addEncodedQuery('user.sys_id=' + userId + '^role.nameLIKEitil');
        grUser.query();
        if (grUser.next()) {
            result = "license";
        } else {
            result = "noLicense";
        }
        return result;
    },

    type: 'CheckUserLicense'
});

AnkurBawiskar_0-1744340595096.png

 

Client Script:

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

    var varvarUser = g_form.getValue('add_user_to_group');
    var varaddedUser = varUser.split(',').pop();
    var ga = new GlideAjax('CheckUserLicense');
    ga.addParam('sysparm_name', 'getUserLicense');
    ga.addParam('sysparm_userId', varaddedUser);
    ga.getXML(licenseResult);

    function licenseResult(response) {
        var userLic = response.responseXML.documentElement.getAttribute('answer');
        alert(userLic);
    }
}

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

mamason
Tera Contributor

Thank You.  The adjustments you showed worked as expected.