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.

Parsing error : unexpected token next to my return

displayWhatever
Tera Contributor

Hi,

i am not sure why this error in coming and how can we remove it. 

find_real_file.png

1 ACCEPTED SOLUTION

Dan H
Tera Guru

Hi,

Just some slight issues with syntax. Here is the correct code:

 

var getUserDataSI = Class.create();
getUserDataSI.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	checkrun: function(){
		var userid = this.getParameter('sysparm_user_name');
		var p = [];
		var gr = new GlideRecord('sys_user');
		gr.addQuery('sys_id', userid);
		gr.query();
		while(gr.next()){
			var q = {};
			q.name = gr.name;
			q.email = gr.email;
			p.push(q);
		}
		return JSON.stringify(p);
	},

    type: 'getUserDataSI'
});

 

Just two points:

  • Your return was outside of the function.
  • Missing a comma at the end of the function.

Hope this helps.

 

Please mark this answer as helpful/solved based on impact

View solution in original post

4 REPLIES 4

Dan H
Tera Guru

Hi,

Just some slight issues with syntax. Here is the correct code:

 

var getUserDataSI = Class.create();
getUserDataSI.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	checkrun: function(){
		var userid = this.getParameter('sysparm_user_name');
		var p = [];
		var gr = new GlideRecord('sys_user');
		gr.addQuery('sys_id', userid);
		gr.query();
		while(gr.next()){
			var q = {};
			q.name = gr.name;
			q.email = gr.email;
			p.push(q);
		}
		return JSON.stringify(p);
	},

    type: 'getUserDataSI'
});

 

Just two points:

  • Your return was outside of the function.
  • Missing a comma at the end of the function.

Hope this helps.

 

Please mark this answer as helpful/solved based on impact

Maik Skoddow
Tera Patron
Tera Patron

Hi

the correct solution has been given by @Dan H , but I recommend reading my article DevBasics: Naming Conventions for JavaScript code. In your code example, you break nearly all possible rules.

Kind regards
Maik

Thanks for the help!

Hitoshi Ozawa
Giga Sage
Giga Sage

FYI, the script would run but probably won't provide the correct result.

I've corrected the script by adding .toString(). Changed the class name to "UserData" and function name to "getUserInfo".

I've also change the query condition to .addQuery('user_name', userName).

var UserData = Class.create();
UserData.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getUserInfo: function() {
        var userName = this.getParameter('sysparm_user_name');
        var grUser = new GlideRecord('sys_user');
        grUser.addQuery('user_name', userName);
        grUser.query();
        var resultArray = [];
        while (grUser.next()) {
            var userDict = {};
            userDict['name'] = grUser.name.toString();    // need to .toString()
            userDict['email'] = grUser.email.toString();  // need to .toString()
            resultArray.push(userDict);
        }

        return JSON.stringify(resultArray);
    },
    type: 'UserData '
});

If the function is to query on user's sys_id, there would be only one match so there isn't a need to return an array with just one element in it. In this case, following script will return the user info of matching sys_id.

var UserData = Class.create();
UserData.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getUserInfo: function() {
        var userSysId = this.getParameter('sysparm_user_name');
        var grUser = new GlideRecord('sys_user');
        if (grUser.get(userSysId)) {
            var userDict = {};
            userDict['name'] = grUser.name.toString();
            userDict['email'] = grUser.email.toString();
            return JSON.stringify(userDict);
        }
    },
    type: 'UserData '
});