The CreatorCon Call for Content is officially open! Get started here.

Catalog Client Script - Return value with Script Includes

Jeff16
Tera Contributor

When I test this script in the Service Catalog, the alert returns null, and the "accounts' field doesn't get displayed.  This script runs when the Employee (Reference sys_user table) changes.  

Catalog Client Script:

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
	//Call Script Include
	var gr = new GlideAjax("getUserPositionRole");
	gr.addParam('sysparm_name', 'getPositionRole');
	gr.addParam('sysparam_newValue', newValue);
	gr.getXML(getPosRole);
	
	//Return Employee's Position's Role
	function getPosRole(response){
		var answer = response.responseXML.documentElement.getAttribute("answer");
		alert(answer);
		if(answer == "Basic"){
			g_form.setDisplay("accounts", false);
		}else if(answer == "Advanced"){
			g_form.setDisplay("accounts", true);
		}
	}
}

Script Includes:

var getUserPositionRole = Class.create();
getUserPositionRole.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	//Find The User's Position and return the Position's Role
	getPositionRole: function(){
		var newPos = "";
		var userSysID = this.getParameter('sysparam_newValue');
		var gr = new GlideRecord('sys_user');
		gr.addQuery('sys_id', sysparam_newValue);
		gr.query();
		
		//Find User's Position
		while(gr.next()){
			newPos = gr.title;
		}
		
		//Find Position's Role
		var gr2 = new GlideRecord('u_positions');
		gr2.addQuery('u_title', newPos);
		gr2.query();
		
		//Return Position's Role
		while(gr2.next()){
			return gr2.u_role;
		}
	},

    type: 'getUserPositionRole'
});

 

What am I missing here?  Thanks.

1 ACCEPTED SOLUTION

Hitoshi Ozawa
Giga Sage
Giga Sage

Try the following.

Client Script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    //Call Script Include
    var gr = new GlideAjax("getUserPositionRole");
    gr.addParam('sysparm_name', 'getPositionRole');
    gr.addParam('sysparm_newValue', newValue);
    gr.getXMLAnswer(function(answer) {
        alert(answer);
        if (answer == "Basic") {
            g_form.setDisplay("accounts", false);
        } else if (answer == "Advanced") {
            g_form.setDisplay("accounts", true);
        }
    });
}

Script Include:

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

    //Find The User's Position and return the Position's Role
    getPositionRole: function() {
        var userSysID = this.getParameter('sysparm_newValue');
        var gr = new GlideRecord('sys_user');
        if (gr.get(userSysID)) {
            var gr2 = new GlideRecord('u_positions');
			if (gr2.get('u_title', gr.title.toString())) {
				return gr2.u_role.toString();
			}
        }
    },
    type: 'getUserPositionRole'
});

View solution in original post

9 REPLIES 9

sethivarun
Kilo Guru

Hi Jeff, 

in the line gr2.addQuery('u_title', newTitle); I don't see a variable newTitle in your code. 

Did you mean to write newPos?

Yes, I meant to write newPos.  I edited the script to when posting.  I have updated it, and the script still returns null.  Thanks.

Dan H
Tera Guru

You are assigning gr.title to newPos, so i've updated your script to reflect that

 

Try:

var getUserPositionRole = Class.create();
getUserPositionRole.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	//Find The User's Position and return the Position's Role
	getPositionRole: function(){
		var newPos = "";
		var userSysID = this.getParameter('sysparam_newValue');
		var gr = new GlideRecord('sys_user');
		gr.addQuery('sys_id', sysparam_newValue);
		gr.query();
		
		//Find User's Position
		while(gr.next()){
			newPos = gr.title;
		}
		
		//Find Position's Role
		var gr2 = new GlideRecord('u_positions');
		gr2.addQuery('u_title', newPos);
		gr2.query();
		
		//Return Position's Role
		while(gr2.next()){
			return gr2.u_role;
		}
	},

    type: 'getUserPositionRole'
});

Please mark my answer as Correct/Helpful based on impact

Regards,

Dan H

Jeff16
Tera Contributor

Thanks.  I meant to write newPos and I edited the script to when posting.  I have updated it, and the script still returns null.  Thanks.