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

Replace all the 'sysparam_' with 'sysparm_'

and let me know if that fixes it.

 

Please mark my answer as Correct/Helpful based on impact

Regards,

Dan H

Jeff16
Tera Contributor

No, it still returns null.

I found more issues with the scripts, please try this version (tested + working):

 

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 newPos = '';
    var userSysID = this.getParameter('sysparm_newValue');

    var userRec = new GlideRecord('sys_user');
    userRec.addQuery('sys_id', userSysID);
    userRec.query();

    //Find User's Position
    while (userRec.next()) {
      newPos = userRec.title.toString();
    }

    //Find Position's Role
    var positionsRec = new GlideRecord('u_positions');
    positionsRec.addQuery('u_title', newPos);
    positionsRec.query();

    //Return Position's Role
    while (positionsRec.next()) {
      var ans = positionsRec.u_role;
    }

    return JSON.stringify(ans);
  },

  type: 'getUserPositionRole',
});

 

Client Script:

 

function onChange(control, oldValue, newValue, isLoading) {
  if (isLoading || newValue == '') {
    return;
  }
  //Call Script Include
  var userPos = new GlideAjax('getUserPositionRole');
  userPos.addParam('sysparm_name', 'getPositionRole');
  userPos.addParam('sysparm_newValue', newValue);
  userPos.getXML(getPosRole);

  //Return Employee's Position's Role
  function getPosRole(response) {
    var answer = response.responseXML.documentElement.getAttribute('answer');

    var clearvalue; // Stays Undefined
    if (answer) {
      var returneddata = JSON.parse(answer);
      if (returneddata == 'Basic') {
        alert(false);
        g_form.setDisplay('accounts', false);
      } else {
        if (returneddata == 'Advanced') {
          alert(true);
          g_form.setDisplay('accounts', true);
        }
      }
    } else {
      alert('no answer');
    }
  }
}

Please mark my answer as Correct/Helpful based on impact

Regards,

Dan H

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

Thank You!