User Criteria based on user manager's company

Angel32
Tera Contributor

Hi,

 

I'm desperately trying to set a user criteria depending of the company of the user and the user's manager.

So far among other things, I've tried adding the following script using the advanced mode of the user criteria with no luck.

Any idea of what I'm doing wrong ?

 

// Condition: utilisateur.company == targetCompanySysId OU manager.company == targetCompanySysId

answer = (function() {
var targetCompanySysId = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; // xxxxxxx

// Current user
var userId = gs.getUserID().toString();
if (!userId) return false;

var u = new GlideRecord('sys_user');
if (!u.get(userId)) return false;

// --- user company ---
var userCompanyId = u.getValue('company');
if (userCompanyId && userCompanyId === targetCompanySysId) {
return true;
}

var managerId = u.getValue('manager'); 
if (!managerId) return false;

var m = new GlideRecord('sys_user');
if (!m.get(managerId)) return false;

var managerCompanyId = m.getValue('company');
if (!managerCompanyId) return false;

return managerCompanyId === targetCompanySysId;
})();

6 REPLIES 6

Brad Bowman
Kilo Patron

To be nit-picky, once you return a GlideRecord you can dot-walk without instantiating another GlideRecord on the same table, and there are some unnecessary checks going on here - just return false if the script makes it to the end without having already returned true is a much more straight-forward and easier approach.  The actual pre-set comments when creating a User Criteria record with a script tell you not to use gs.getUserID(), rather 'user_id' which contains the sys_id.  I assume you're not actually using 'xxx...' as the CompanySysId, and not really sure why one would bother obfuscating that as it only leads to confusion and uncertainty.  To summarize, this will be a lot better script to start with and troubleshoot:

answer = (function() {
    var targetCompanySysId = '58473babc3453010069aec4b7d40dd50'; 
    var u = new GlideRecord('sys_user');
    if (u.get(user_id)) {
        if (u.getValue('company') == targetCompanySysId) {
            return true;
        }
        if (u.manager.company.toString() == targetCompanySysId) {
            return true;
        }
    }
    return false;
})();

 

Hi Brad,

 

Indeed the xxx part was intended to hide this company sys_id which is 

cb221d84db7fdc90228e641d0b96199b if it is needed.
Thanks a lot for your help but I tried your script with no luck, the user criteria diagnostics module is still returning me no access for a user who should.
 

Is your test case a user who's company value matches the supplied one, or who's manager's company matches?  You should test with 2 users that each matches only one of the criteria. Also try adding some log statements to confirm the values for u.company and u.manager.company

I've tried to add some logs, but none of them appeared in my system logs >> systems logs >> all. If you have a you to make them display in the logs I'll be more than happy.

(Many thanks for your help by the way).