We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

Pass variable from function to function in script include

Lisa Silvaroli
Tera Guru

I have a Script Include with a couple of expression functions. I need to pass a variable from on expression to another and am not sure how to. 

 

I define userDetails.DirectReport in the getUserName function but need to access its value in getStaffAccess. Any insight on how I can do that would be very much appreciated! Snippet of my code below (I took out some of it that I don't think is relevant to solving this) 

var GetUserInfo2 = Class.create();
GetUserInfo2.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
		
	getUserName: function(){		
		var userName = this.getParameter('sysparm_user');
		var userDetails = {};
		
			var gr = new GlideRecord("sys_user");
			gr.addQuery('sys_id',userName);
			gr.query();
		
		if(gr.next()) {
			//gs.log('User found:' + userName);
			userDetails.firstname = gr.getValue('first_name');
			userDetails.lastname = gr.getValue('last_name');
			userDetails.location = gr.getValue('location');
			userDetails.title = gr.getValue('title');
			userDetails.email = gr.getValue('email');
			userDetails.department = gr.getValue('department');
			userDetails.division = gr.getValue('u_division');
			userDetails.manager = gr.getValue('manager');
			userDetails.DirectReport = gr.getValue('u_direct_reports'); //Need to push this to 'getStaffAccess'
			
		}
		var json = new JSON();
		var data = json.encode(userDetails);
		gs.log('data: ' + data);
		return data.toString();

	},

	getStaffAccess: function() {
			
			var usrId = this.getParameter('sysparm_staff');
			var accessAction = this.getParameter('sysparm_access_action');
			
			var usrRec = new GlideRecord('u_user_application_access');
			usrRec.addQuery('u_sn_userid', usrId);
			usrRec.addQuery('u_access_removed_yn', 'false');
			usrRec.addQuery('u_current','true');
		
			if ((accessAction == 'Disable') || (accessAction == 'Change')) {
				usrRec.addQuery('u_app_action', 'Add');
				
			}
			else {
				var orCond = usrRec.addQuery('u_app_action', 'Add');
				orCond.addOrCondition('u_app_action', 'Disable');
			}
		//gs.log('User Details: ' + userDetails);
			usrRec.query();

			var DataWarehouse = false;
			var CellPhone = false;
			var AdminAccount = false;
			var hasDirectReport = false;
			
			var appAccess = [];
			while (usrRec.next()) {
			appAccess.push(usrRec.u_app_access.getDisplayValue());
			
				if (usrRec.u_app_access.getDisplayValue() == 'Cell Phone'){
					CellPhone = true;
				}
				if (usrRec.u_app_access.getDisplayValue() == 'Data Warehouse'){
					DataWarehouse = true;
				}
				if (usrRec.u_app_access.getDisplayValue() == '011A Admin Account'){
					AdminAccount = true;
				}
			//	if (userDetails.DirectReport == '1'){ //This is where I need to look at the userDetails.DirectReport to see if it is true or false
			//		hasDirectReport = true;
			//	}
1 REPLY 1

DrewW
Mega Sage

I think I understand what you are trying to say.  So you can do this

userDetails.DirectReport = this.getStaffAccess(staff, accessAction);

getStaffAccess: function(staff, action) {
	
	var usrId = ""; 
	if(staff) {
		usrId = staff;
	} else {
		usrId = this.getParameter('sysparm_staff');
	}
	var accessAction = "";
	if(staff) {
		accessAction = action;
	} else {
		accessAction = this.getParameter('sysparm_access_action');
	}