- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 06-17-2019 10:58 PM
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:
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 -
- 2,068 Views