Why am i getting unreachable code on the following code?

MIck11
Kilo Contributor

Getting 2 error messages, first one says type is defined but never user and the second message says unreachable code can someone help with this?

 

var Bringattributeuser = Class.create();
Bringattributeuser.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
getDepartmentHeadUser: function() {
var userID = this.getparameter('syspara_sys_user');
var Gr_emp = new GlideRecord('x_bifac_hr2h_emp');
Gr_emp.addQuery('user_name', sys_user);
Gr_emp.query();
if (Gr_emp.next()) {

var deppid = Gr_emp.department.dept_head;

var Gr_dept = new GlideRecord('cmn_department');
Gr_dept.addQuery('sys_id', deppid);
Gr_dept.query();
if (Gr_dept.next()) {
var dhid = Gr_dept.sys_user;

return dhid ;
type:'Bringattributeuser' ;

}
}
}

7 REPLIES 7

Sure do keep posted.

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

Regards
Ankur

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

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Mick,

Both errors are probably from misplaced brackets.

Re-formatting the code in the question results in code below. Start and end brackets aren't matching. Class declaration must end with "type: 'Bringattributeuser';".

var Bringattributeuser = Class.create();
Bringattributeuser.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
            getDepartmentHeadUser: function() {
                var userID = this.getparameter('syspara_sys_user');
                var Gr_emp = new GlideRecord('x_bifac_hr2h_emp');
                Gr_emp.addQuery('user_name', sys_user);
                Gr_emp.query();
                if (Gr_emp.next()) {

                    var deppid = Gr_emp.department.dept_head;

                    var Gr_dept = new GlideRecord('cmn_department');
                    Gr_dept.addQuery('sys_id', deppid);
                    Gr_dept.query();
                    if (Gr_dept.next()) {
                        var dhid = Gr_dept.sys_user;

                        return dhid;
                        type: 'Bringattributeuser';

                    }
                }
            }

There is an  error in that "userID" is declared but not used. "Gr_emp.addQuery('user_name', sys_user);" probably should be Gr_emp.addQuery('user_name', userID);

Furthermore, there's no need to query on cmn_department table.

Following code will return user corresponding to department head of the department.

var Bringattributeuser = Class.create();
Bringattributeuser.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getDepartmentHeadUser: function() {
        var userID  = this.getparameter('sysparm_sys_user');
        var Gr_emp = new GlideRecord('x_bifac_hr2h_emp');
        if (Gr_emp.get('user_name', userID )) {
            return Gr_emp.department.dept_head;
        }
    },
    type: 'Bringattributeuser '
});

-O-
Kilo Patron
Kilo Patron

It is because you have stuff after a return statement:

return dhid ;
type:'Bringattributeuser'

But that is the smallest of the problems this SI has. A bunch of closing braces are missing, there is an extra ; after what is supposed to be a property of the prototype (type: 'Bringattributeuser'), not a statement in function property getDepartmentHeadUser.

The S.I. should look like this (and I don't mean formatting):

var Bringattributeuser = Class.create();

Bringattributeuser.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

	// First prototype (function) property
	getDepartmentHeadUser: function () {
		var userID = this.getparameter('syspara_sys_user');
		var Gr_emp = new GlideRecord('x_bifac_hr2h_emp');

		Gr_emp.addQuery('user_name', userID); // have replaced sys_user here
		Gr_emp.query();

		if (Gr_emp.next()) {
			var deppid = Gr_emp.department.dept_head;
			var Gr_dept = new GlideRecord('cmn_department');

			Gr_dept.addQuery('sys_id', deppid);
			Gr_dept.query();

			if (Gr_dept.next()) {
				var dhid = Gr_dept.sys_user;

				return dhid;
			}
		}
	},

	// Second prototype (string) property
	type: 'Bringattributeuser'
});

But there are more problems with this script: table cmn_department has no field named sys_user, you probably mean dept_head, but in that case what is the point of making a 2nd query, you could just return Gr_emp.department.dept_head. You could also "shorten" the query code:

var Bringattributeuser = Class.create();

Bringattributeuser.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

	// First prototype (function) property
	getDepartmentHeadUser: function () {
		var userID = this.getparameter('syspara_sys_user');
		var Gr_emp = new GlideRecord('x_bifac_hr2h_emp');

		if (Gr_emp.get('user_name', userID) && !Gr_emp.department.dept_head.nil()) {
			return '' + Gr_emp.department.dept_head;
		}
	},

	// Second prototype (string) property
	type: 'Bringattributeuser'
});