kevclark
Tera Contributor

Hi all,

I just finished working on a POC for something which I thought I'd share quickly here in case anyone is in need of a similar solution.

I have multiple user types who need to be shown different question values in the "Get Help" record producer based on who they are.   It might be based on their role, or who they work for, or possibly something else -- who knows!  

Hmm.... what's a good way to pick the difference between users based on random attributes of their account...?   Well, User Criteria of course! 

My reasoning is that if I have a lookup/reference table containing the values I want to make available to various users, all I need to do is pop some User Criteria records on the table, and I should be able to script a reference qualifier to control what people can and can't see in the Record Producer.

I found some useful content on community (such as this thread with some stuff from Chuck Tomasi) which lead me to a useful SNC.UserCriteriaLoader.getAllUserCriteria() method.

I start by creating my "Assignment Mapping" table, populating whatever value I want to return:

find_real_file.png

And what's that Related List below?  Why it's an M2M table (extended from sys_m2m) with a relationship from my "u_assignment_mapping" table to the "user_criteria" table.

So cool - at this point I have my reference table and my User Criteria records related with some data.  What's next?  Well, I need a method to call this relationship from my Reference Qualifier - enter the Script Include below:

 

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

	/**SNDOC
	@name getCurrentUserCriteriaID
	@description Retrieves the current user's user criteria and returns an array of User Criteria IDs which are currently applied to this user.
	@returns {Array} An array of User Criteria IDs
	@example
	var UCUtil = new userCriteriaUTIL();
	var criteria = UCUtil.getCurrentUserCriteria();
	*/

	getCurrentUserCriteria: function () /*object*/ {
		var criteriaArr = SNC.UserCriteriaLoader.getAllUserCriteria();
		return criteriaArr;
	},


	/**SNDOC
		@name getCurrentUserCriteriaName
		@description Retrieves the current user's user criteria and returns an array of User Criteria names which are currently applied to this user.
		@returns {Array} An array of User Criteria Names
		@example
		var UCUtil = new userCriteriaUTIL();
		var criteria = UCUtil.getCurrentUserCriteriaName();
		*/

	getCurrentUserCriteriaName: function () /*object*/ {
		var currentCriteria = this.getCurrentUserCriteria();
		var criteriaNameArr = [];
		var criteriaGR = new GlideRecord('user_criteria');
		criteriaGR.addQuery('sys_id', 'IN', currentCriteria);
		criteriaGR.query();
		while (criteriaGR.next()) {
			criteriaNameArr.push(criteriaGR.name.toString());
		}
		return criteriaNameArr;
	},


	/**SNDOC
			@name getM2MCriteria
			@description Retrieves the current user's user criteria and compares it to a M2M table (Parm 1) containing a User Criteria field (Parm 2) and return the sys_ID of the related record (Parm 3)
			@param {String} [tableName] - The name of the User Criteria M2M table
			@param {String} [userCriteriaField] - The field name (string) of the User Criteria Field on the M2M record
			@param {String} [relatedRecordField] - The field name (string) of the Related Record Field on the M2M record
			@returns {Array} An array of Related Record SysIDs
			@example
			var UCUtil = new userCriteriaUTIL();
			var relatedMappings = UCUtil.getM2MCriteria('u_m2m_asmt_mapping_user_criteria','u_user_criteria','u_assignment_mapping');
			// in a reference qualifier:
			javascript:new userCriteriaUTIL().getM2MCriteria('u_m2m_asmt_mapping_user_criteria','u_user_criteria','u_assignment_mapping')
			*/

	getM2MCriteria: function (tableName, userCriteriaField, relatedRecordField) /*object*/ {

		returnRecords = [];
		var currentCriteria = this.getCurrentUserCriteria();
		var mappingM2M = new GlideRecord(tableName);
		mappingM2M.addQuery(userCriteriaField, 'IN', currentCriteria);
		mappingM2M.query();
		while (mappingM2M.next()) {
			returnRecords.push(mappingM2M[relatedRecordField].sys_id.toString());
			}
		return returnRecords;
	},


	type: 'userCriteriaUTIL'
});

It has three methods - 

getCurrentUserCriteriaID which simply gets the current User's Criteria IDs
getCurrentUserCriteriaName - which gets the human names of the User Criteria
getM2MCriteria - which takes a table name (my M2M table) a field name on that table for the User Criteria reference, and a Field name for the target related record.
 
Using the third method, getM2MCriteria, I can get my reference qualifier to feed the target table name and fields to get back the User Criteria Records that I'm interested in based on whomsoever is logged in - like so:
find_real_file.png
This gives me the freedom to add and remove User Criteria and add multiple user types to any Reference that I want to make available based on who the viewer is, without needing to write lots of weird and wonderful scripts to hide and show things.  In our case we have a couple of different organisation entities who would benefit from a single entry point but who have quite different requirements.
 
Et Viola!
 
Version history
Last update:
‎06-17-2019 10:58 PM
Updated by: