Can you use g_form in a script include?

Mitch Moses
Giga Expert

I am running a script include to check the current user and display assignment groups accordingly. I now have a requirement to look at the current customer on the form first before searching the user and then display assignment groups based upon that customer.

 

For this I have made a new table u_customer_assignment_groups(not a reference table), with 2 fields customer and group where end users will be able to connect customers with groups. So I am trying to get my Script Include to look at the current customer on the form then look through the table and if the customer is in the table then display the groups listed with the customer.

 

In my script include I try to call g_form, but I don't think you can call this in a Script Include. So does anyone have any suggestions on how to run through this.

 

getGroupsOfType: function (groupTypes) {
		/*look here for customer on the other table to make*/
		var groups2;

var currComp = g_form.getValue('company');//returns an error
		gs.log(currComp);

		var grGroups4 = new GlideRecord('u_company_assignment_groups');
		grGroups4.addQuery('u_company', currComp);//filter on the current company
		grGroups4.query();
		
		if(grGroups4.next()){
			while(grGroups4.next()) {
				groups2.push(grGroups4.getValue('sys_id'));
			}
			return groups2; // return the groups assocaited with the customer in the table
		}
		//this part below is working fine for me. Just this part above ^^
		var groups = [];
		var isdType = "63600de5db6673c0192a6ac2ca961999"; //ISDagent group type sys_id
		var nocType = "9d5089e5db6673c0192a6ac2ca9619bd"; //NOCagent group type sys_id
		
		var index = groupTypes.toString().split(',').indexOf(isdType) > -1;
		var index2 = groupTypes.toString().split(',').indexOf(nocType) > -1;
		
		gs.log(index);
		gs.log(index2);
		gs.log(groupTypes);
		
		if(index){//show isd agent groups only
			var grGroups = new GlideRecord('sys_user_group');
			grGroups.addQuery('type','CONTAINS','e797a021dbddbb00192a6ac2ca96198d');//show ISD group type
			grGroups.query();
			
			while(grGroups.next()) {
				groups.push(grGroups.getValue('sys_id'));
			}
			
			return groups;
			
			
		}
		else if(index2){//show noc agent groups only
			var grGroups2 = new GlideRecord('sys_user_group');
			grGroups2.addQuery('type','CONTAINS','5c330789db2233c0192a6ac2ca9619a9');//show NOC group type
			grGroups2.query();
			
			while(grGroups2.next()) {
				groups.push(grGroups2.getValue('sys_id'));
			}
			
			return groups;
			
			
		}
		else{ //show itsm groups
			var grGroups3 = new GlideRecord('sys_user_group');
			grGroups3.addQuery('type','CONTAINS','efa71a776f7f21406ca6687f8e3ee48d');//itsm sys_id
			grGroups3.query();
			
			while(grGroups3.next()) {
				groups.push(grGroups3.getValue('sys_id'));
			}
			
			return groups;
		}
		
	},
	

 

 

 

9 REPLIES 9

Well I am not sure how your current complete code looks like, but you can pass the current variable received in the first function to other functions.

If you are not comfortable in using multiple functions / passing values in other functions you can have all your logic in a single function (however the best practice is to keep your code modular, and create functions for each unique functionality.

Anyway, you can pass the variable to other function as follows:

 

firstFunction: function (firstVariable) {

//calling second function from the first function and passing the variable to the function

secondFunction(firstVariable);

}

 

secondFunction(secondVariable) {

//perform the logic with secondVariable - which is same as the parameter which was passed to first function, just using a different local name within this function

}

Typo in my email, read it as:

secondFunction: function (secondVariable) {

//perform the logic with secondVariable - which is same as the parameter which was passed to first function, just using a different local name within this function

}

Hi,

Unfortunately, this sort of spiraled a bit since I last replied so I'm not sure where you're at with your code now and the other poster basically replied with the same thing I did so I don't know who is helping who at this point.

But...I'm still back on the part about what you're wanting to do. So instead of trying to make it fit to your code, I think you need to revisit what you're trying to do and perhaps there's a more efficient way than what you've got set up. In your original post, you said you made a custom table that has two fields, user and group. Are these reference fields to sys_user and sys_user_group? It sounds like the group field is like a list collector or something?

And so customer is a field on the form? Then you have the "user" who is the caller/submitter?

I guess I'm trying to understand what fields are all at play here, what type they are, and what you're trying to do.

In your code you posted, I see many glide queries going on and you're returning what appears to be an array?

For the reference qualifier you'd need to return a query string...something like:

return 'typeLIKEresource'; ya know...something like that.

So, if you've gotten to a good place from the other user helping, by all means, keep going, but if you're able to clarify some of my questions, then maybe I can assist a bit more. Sorry!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Haha we really did. I think I confused you Here's a picture of the table I made with a company field and group field. End users will be able to come in and list a company and specify specific groups they want to see if this customer is selected on the current form an end user is on. 

 

I have my script working fine, but I want to add the ability to look at the current form the user is on when they click assignment groups and check if that customer is in this table. If it is, then show the groups associated with that customer in this table and if NOT then go through my script normally how it is now. 

 

I want to implement this functionality in the getGroupsOfType function

 

find_real_file.png

find_real_file.png

 

 

 

 

var GetGroupsOfTypeRefQual = Class.create();
GetGroupsOfTypeRefQual.prototype = {
	
	initialize: function() {
	},
	
	getGroupsOfMyTypeQuery: function() {
		var myGroups = this.getMyGroups();
		var myGroupsTypes = this.getGroupTypesOfGroups(myGroups);
		var groupsOfMyTypes = this.getGroupsOfType(myGroupsTypes);
		var encQry = 'sys_idIN' + groupsOfMyTypes.join(",");
		
		return encQry;
	},
	
	getMyGroups: function() {
		var usr = gs.getUser();
		var javaGroupsArray = usr.getMyGroups(); // Returns Java array, need to convert to JS array
		var jsGroupsArray = javaGroupsArray.toArray();
		var groupsStr = jsGroupsArray.join(",");
		
		return groupsStr;
	},
	
	getGroupTypesOfGroups: function(groups) {
		var groupTypes = [];
		var gaUserGroup = new GlideAggregate('sys_user_group');
		gaUserGroup.addAggregate('COUNT', 'type');
		gaUserGroup.addQuery('sys_id','IN',groups);
		gaUserGroup.query();
		
		while (gaUserGroup.next()) {
			groupTypes.push(gaUserGroup.getValue('type'));
		}
		
		return groupTypes;
	},
	
	getGroupsOfType: function (groupTypes) {
		/*look here for customer on the other table to make*/
		//all this works fine, but this is where I want to do the current customer query on the new table I made

		var groups = [];//show specfic groups by checking if the user is apart of group that has one of thses 2 tags
		var isdType = "63600de5db6673c0192a6ac2ca961999"; //ISDagent group type sys_id
		var nocType = "9d5089e5db6673c0192a6ac2ca9619bd"; //NOCagent group type sys_id
		
		var index = groupTypes.toString().split(',').indexOf(isdType) > -1;//groupTypes has all of the types pulled from the groups there in and checks to see this type is in the groupTypes array
		var index2 = groupTypes.toString().split(',').indexOf(nocType) > -1;
		
	
		
		if(index){//show isd agent groups only if they a ISDagent type
			var grGroups = new GlideRecord('sys_user_group');
			grGroups.addQuery('type','CONTAINS','e797a021dbddbb00192a6ac2ca96198d');//show groups with ISD group type
			grGroups.query();
			
			while(grGroups.next()) {
				groups.push(grGroups.getValue('sys_id'));
			}
			
			return groups;
			
			
		}
		else if(index2){//show noc agent groups only if they a NOCagent type
			var grGroups2 = new GlideRecord('sys_user_group');
			grGroups2.addQuery('type','CONTAINS','5c330789db2233c0192a6ac2ca9619a9');//show groups with NOC group type
			grGroups2.query();
			
			while(grGroups2.next()) {
				groups.push(grGroups2.getValue('sys_id'));
			}
			
			return groups;
			
			
		}
		else{ //show itsm groups with groups type itsm
			var grGroups3 = new GlideRecord('sys_user_group');
			grGroups3.addQuery('type','CONTAINS','efa71a776f7f21406ca6687f8e3ee48d');//itsm sys_id
			grGroups3.query();
			
			while(grGroups3.next()) {
				groups.push(grGroups3.getValue('sys_id'));
			}
			
			return groups;
		}
		
	},
	
	type: 'GetGroupsOfTypeRefQual'
};

 

Manish Vinayak1
Tera Guru

Hi Mitch,

 

No you cannot use g_form in a Script Include as it is a client side API whereas Script Include is server side code. I believe you need to get the assignment groups based on the user who has logged in and viewing the form. And the logic / relationship is maintained in a table.

 

If I understand your requirements correctly, you need to show only those assignment groups which are related to the current logged in user's company / company record on the form. And I assume the group field is a reference field on the form.

Why don't you go with a dynamic reference qualifier / advanced reference qualifier on that assignment group field and call your script include? You can also pass the company value in the function call.

 

Reference Qualifier:

Type - Advanced

Value - javascript:new YourScriptInclude().yourFunctionName(current.company)

 

Thanks,

Manish