Unable to fetch roles through cart API

Nikhitha Mohan
Tera Contributor

We have created a custom table for counting departments and roles that a user can access through a request. Additionally, we have written a scheduled script to check the user's department in both the user table and the custom table. If any mismatches are found, a request should automatically be generated on behalf of that user. In the task, variables like 'department to be revoked' and 'role to be revoked' should be displayed, and these values should come from the custom table. If a user has two roles under the same department, both roles should be displayed in the 'role to be revoked' field. However, we are currently unable to fetch these roles from the custom table and display them in that particular field.

var gr = new GlideRecord('u_role_count');// u_role_count is custom table

gr.addQuery('u_status', 'Active');

//gr.addEncodedQuery('u_department_nameINDepartment A,Department B,Department C');

gr.query();

 

while (gr.next()) {

 

    var gr1 = new GlideRecord('sys_user');

    gr1.addQuery('name', gr.u_user_name);

    gr1.query();

    while (gr1.next()) {

 

        var sys_user_id = gr1.sys_id;

        var dept_user = gr1.department.name.toString();

        var dept_role = gr.u_department_name.toString();

        //var rolesID=gr.u_user_name.u_department_name.u_role_name.toString();

        var req_for = gr.u_user_name.toString();

 

        //roleList(dept_role, req_for);

 

        if (dept_user != dept_role)

 

        {

 

            var cartId = GlideGuid.generate(null);

 

            var cart = new Cart(cartId);

 

            //give sys_id of catalog item

 

            var item = cart.addItem('6877bf7697332110a000b6cfe153af05');// sys id of catalog

 

            cart.setVariable(item, "requester_name", sys_user_id);//requester_name is variable name in catalog

            cart.setVariable(item, "department_to_be_revoked", dept_role);// department to be revoked is variable in catalog

            //cart.setVariable(item, "role_to_be_revoked", roleList(dept_role, sys_user_id));

 

            var rc = cart.placeOrder();

 

            var reqRecord = new GlideRecord('sc_request');

 

            reqRecord.get(rc.sys_id);

 

            reqRecord.requested_for = req_for;

 

            reqRecord.update();

 

        }

 

    }



}

 

updateRITM(rc.sys_id); //call a function immediately to update the ritm.   This must be a nested function, otherwise inbound actions get weird.

 

// var dept_name_req;

 

// function roleList(dept, name) {

//     gr.addQuery('u_department_name', dept);// u_department_name is the column name in custom table

//     gr.addQuery('u_user_name', name);//u_user_name is the column name in custom table

//     gr.query();

//     while (gr.next()) {

//         return gr.u_role_name;

//        // gs.log(dept_name_req);

 

//     }

//     return dept_name_req;

// }

// //     //also, we're passing the sys_id of the request so we know what RITM to grab.

 

// //  }

 

function updateRITM(req) {

 

    var ritm = new GlideRecord('sc_req_item');

 

    ritm.addQuery('request', req); //req is what we passed from the previous function. the sys_id of the request.

 

    ritm.query();

 

    while (ritm.next()) {

 

        ritm.requested_for = req_for;




        //gs.getUserID(); //my ritm table separately tracks its own customer, since I don't use Request

 

        ritm.configuration_item = '';

 

        ritm.u_business_service = '';

 

        ritm.assignment_group = 'ddadd8360f7335003c10244be1050e2d'; 

 

        ritm.urgency = '1';

 

        ritm.priority = '2';

 

        ritm.impact = '1';

 

        ritm.update();

 

    }

 

}

 

 

 

 

Please refer the attachment and suggest a solution.......

 

1 ACCEPTED SOLUTION

Tushar
Kilo Sage
Kilo Sage

Hi @Nikhitha Mohan  ,

 

can you please try using below code and let me know if it doesn't works -

 

var gr = new GlideRecord('u_role_count'); // u_role_count is the custom table

gr.addQuery('u_status', 'Active');
gr.query();

while (gr.next()) {
    var gr1 = new GlideRecord('sys_user');
    gr1.addQuery('name', gr.u_user_name);
    gr1.query();

    while (gr1.next()) {
        var sys_user_id = gr1.sys_id;
        var dept_user = gr1.department.name.toString();
        var dept_role = gr.u_department_name.toString();
        var req_for = gr.u_user_name.toString();

        if (dept_user != dept_role) {
            var cartId = GlideGuid.generate(null);
            var cart = new Cart(cartId);
            var item = cart.addItem('6877bf7697332110a000b6cfe153af05'); // sys_id of the catalog item

            cart.setVariable(item, 'requester_name', sys_user_id); // requester_name is a variable in the catalog
            cart.setVariable(item, 'department_to_be_revoked', dept_role); // department to be revoked is a variable in the catalog
            cart.setVariable(item, 'role_to_be_revoked', getRolesToBeRevoked(dept_role, req_for)); // role_to_be_revoked is a variable in the catalog

            var rc = cart.placeOrder();
            var reqRecord = new GlideRecord('sc_request');
            reqRecord.get(rc.sys_id);
            reqRecord.requested_for = req_for;
            reqRecord.update();
        }
    }
}

function getRolesToBeRevoked(department, userName) {
    var rolesToRevoke = '';
    var grRoles = new GlideRecord('u_role_count'); // u_role_count is the custom table
    grRoles.addQuery('u_department_name', department);
    grRoles.addQuery('u_user_name', userName);
    grRoles.query();

    while (grRoles.next()) {
        if (rolesToRevoke !== '') {
            rolesToRevoke += ', ';
        }
        rolesToRevoke += grRoles.u_role_name.getDisplayValue();
    }

    return rolesToRevoke;
}

function updateRITM(req) {
    // Your existing updateRITM function remains unchanged.
    // ... 
}

 

Please, don't forget to mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!

Regards,
Tushar

 

View solution in original post

7 REPLIES 7

Tushar
Kilo Sage
Kilo Sage

Hi @Nikhitha Mohan  ,

 

can you please try using below code and let me know if it doesn't works -

 

var gr = new GlideRecord('u_role_count'); // u_role_count is the custom table

gr.addQuery('u_status', 'Active');
gr.query();

while (gr.next()) {
    var gr1 = new GlideRecord('sys_user');
    gr1.addQuery('name', gr.u_user_name);
    gr1.query();

    while (gr1.next()) {
        var sys_user_id = gr1.sys_id;
        var dept_user = gr1.department.name.toString();
        var dept_role = gr.u_department_name.toString();
        var req_for = gr.u_user_name.toString();

        if (dept_user != dept_role) {
            var cartId = GlideGuid.generate(null);
            var cart = new Cart(cartId);
            var item = cart.addItem('6877bf7697332110a000b6cfe153af05'); // sys_id of the catalog item

            cart.setVariable(item, 'requester_name', sys_user_id); // requester_name is a variable in the catalog
            cart.setVariable(item, 'department_to_be_revoked', dept_role); // department to be revoked is a variable in the catalog
            cart.setVariable(item, 'role_to_be_revoked', getRolesToBeRevoked(dept_role, req_for)); // role_to_be_revoked is a variable in the catalog

            var rc = cart.placeOrder();
            var reqRecord = new GlideRecord('sc_request');
            reqRecord.get(rc.sys_id);
            reqRecord.requested_for = req_for;
            reqRecord.update();
        }
    }
}

function getRolesToBeRevoked(department, userName) {
    var rolesToRevoke = '';
    var grRoles = new GlideRecord('u_role_count'); // u_role_count is the custom table
    grRoles.addQuery('u_department_name', department);
    grRoles.addQuery('u_user_name', userName);
    grRoles.query();

    while (grRoles.next()) {
        if (rolesToRevoke !== '') {
            rolesToRevoke += ', ';
        }
        rolesToRevoke += grRoles.u_role_name.getDisplayValue();
    }

    return rolesToRevoke;
}

function updateRITM(req) {
    // Your existing updateRITM function remains unchanged.
    // ... 
}

 

Please, don't forget to mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!

Regards,
Tushar

 

Hi @Tushar  

 

Thanks for your valuable support. This code is working perfectly.

Hi @Tushar  , I have one more doubt.

 

Instead of displaying multiple roles in 'role to be revoked filed ' is there any option to display single role?

If a user has two roles under same department, while creating request for role revocation through scheduled script, two request should be created (this one working fine now) and one request should display one role and other request should display another role. 

Hi @Nikhitha Mohan  ,

 

Yes, you can modify the script to create separate requests for each role that needs to be revoked. Instead of displaying multiple roles in the 'role to be revoked' field, you can create individual requests for each role and set the appropriate role to be revoked in each request.

 

below script should split the roles into an array and create individual requests for each role that needs to be revoked

var gr = new GlideRecord('u_role_count'); // u_role_count is the custom table
gr.addQuery('u_status', 'Active');
gr.query();

while (gr.next()) {
    var gr1 = new GlideRecord('sys_user');
    gr1.addQuery('name', gr.u_user_name);
    gr1.query();

    while (gr1.next()) {
        var sys_user_id = gr1.sys_id;
        var dept_user = gr1.department.name.toString();
        var dept_role = gr.u_department_name.toString();
        var req_for = gr.u_user_name.toString();

        if (dept_user != dept_role) {
            var rolesToRevoke = getRolesToBeRevoked(dept_role, req_for);
            var rolesArray = rolesToRevoke.split(', ');

            for (var i = 0; i < rolesArray.length; i++) {
                var cartId = GlideGuid.generate(null);
                var cart = new Cart(cartId);
                var item = cart.addItem('6877bf7697332110a000b6cfe153af05'); // sys_id of the catalog item

                cart.setVariable(item, 'requester_name', sys_user_id); // requester_name is a variable in the catalog
                cart.setVariable(item, 'department_to_be_revoked', dept_role); // department to be revoked is a variable in the catalog
                cart.setVariable(item, 'role_to_be_revoked', rolesArray[i]); // set each role individually

                var rc = cart.placeOrder();
                var reqRecord = new GlideRecord('sc_request');
                reqRecord.get(rc.sys_id);
                reqRecord.requested_for = req_for;
                reqRecord.update();
            }
        }
    }
}

function getRolesToBeRevoked(department, userName) {
    var rolesToRevoke = '';
    var grRoles = new GlideRecord('u_role_count'); // u_role_count is the custom table
    grRoles.addQuery('u_department_name', department);
    grRoles.addQuery('u_user_name', userName);
    grRoles.query();

    while (grRoles.next()) {
        if (rolesToRevoke !== '') {
            rolesToRevoke += ', ';
        }
        rolesToRevoke += grRoles.u_role_name.getDisplayValue();
    }

    return rolesToRevoke;
}

function updateRITM(req) {
    // Your existing updateRITM function remains unchanged.
    // ... 
}

 

Please, don't forget to mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!

Regards,
Tushar