Check if user is part of a department

Amy Palaghia
Tera Contributor

Hi,

I've got a catalog item and I need to check if the user's department is a certain one in order to allow him to continue to the form (pop-up message if he's not allowed). I've created a script include and client script but it doesn't work, not sure what I've done wrong - I've used something similar before, but I was checking if the user is part of a group and that one worked.

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

    isMemberOfDept: function() {
        var gr = new GlideRecord('sys_user');
        gr.addQuery('user', gs.getUserID());
        gr.addQuery('department', '6333883a1b5d05106cd7a9f9bc4bcbbe');
        gr.setLimit(1);
        gr.query();
        return gr.hasNext();
    },
    type: 'userDeptUtils'
});

-----

function onLoad() {
    var ga = new GlideAjax('userDeptUtils');
    ga.addParam('sysparm_name', 'isMemberOfDept');
    ga.getXML(getServerParse);
}

function getServerParse(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    if (answer === 'true') {
        alert('Your department needs to request orders through the Finance Office');
        top.window.location = '/sp';
    }
}

Thanks,

Amy

1 ACCEPTED SOLUTION

Hitoshi Ozawa
Giga Sage
Giga Sage

I'll code it like below.

Client Script

function onLoad() {
    var ajax = new GlideAjax('userDeptUtils');
    ajax.addParam('sysparm_name', 'isMemberOfDept');
    ajax.getXMLAnswer(function(answer) {
        alert(answer);
        if (answer.length > 0 && answer == 'true') {
            alert('Your department needs to request orders through the Finance Office');
            top.window.location = '/sp';
        }
    });
}

Script Include. I'm using name of the department instead of sys_id of department to make it easier to maintain.

var userDeptUtils = Class.create();
userDeptUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    isMemberOfDept: function() {
            var gr = new GlideRecord('sys_user');
            if (gr.get(gs.getUserID())) {
                return (gr.department.name == '<name of department>');
            }
            return;
    },
    type: 'userDeptUtils '
});

View solution in original post

3 REPLIES 3

Allen Andreas
Administrator
Administrator

Hi,

Please use the appropriate forum feature such as: "Insert/Edit code sample" when pasting code on the forums. This helps organize your code and make it easier to read:

find_real_file.png

It's recommended to also use log statements so you can see what you're doing...otherwise you're just coding in the dark. Unfortunately, you didn't do that it appears.

Please add them and at least confirm to us if the script include is being called correctly and you're entering it and then check your return value and check that via alert in your client script.

You didn't share what was working in another script for us to help you compare. We have no idea what you've changed if the other script was working and now this one is not...

Please mark reply as Helpful/Correct, if applicable. Thanks!


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

Hi,

I'm glad you found a correct answer that worked for you.

I'd still recommend using log statements in the future. This helps you learn versus being given exact code to you.

Take care! 🙂


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

Hitoshi Ozawa
Giga Sage
Giga Sage

I'll code it like below.

Client Script

function onLoad() {
    var ajax = new GlideAjax('userDeptUtils');
    ajax.addParam('sysparm_name', 'isMemberOfDept');
    ajax.getXMLAnswer(function(answer) {
        alert(answer);
        if (answer.length > 0 && answer == 'true') {
            alert('Your department needs to request orders through the Finance Office');
            top.window.location = '/sp';
        }
    });
}

Script Include. I'm using name of the department instead of sys_id of department to make it easier to maintain.

var userDeptUtils = Class.create();
userDeptUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    isMemberOfDept: function() {
            var gr = new GlideRecord('sys_user');
            if (gr.get(gs.getUserID())) {
                return (gr.department.name == '<name of department>');
            }
            return;
    },
    type: 'userDeptUtils '
});