Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Identify recent users who got admin role assigned, check for a vaild RITM against it

Ankita9793
Tera Contributor

 

I have a requirement to identify user who recently got admin role assigned and check if there is a valid ritm against it.

 

I am able to fetch users who  got Admin role assigned recently, then in the recent ritm's i have to check for a valid ritm where catalog item is XYZ, ritm.variable.beneficary/ requested for is the identified users/admin user(this is where i'm facing issue'.  

 

var arr = [];
var arr2 = [];
var urole = new GlideRecord('sys_user_has_role');
urole.addEncodedQuery('role=2831a114c611228501d4ea6c309d626d^sys_created_onONYesterday@javascript:gs.beginningOfYesterday()@javascript:gs.endOfYesterday()'); //role is admin and created on yesterday
urole.query();
while (urole.next()) {
    arr.push(urole.user + '');

    var validRITM = new GlideRecord('sc_req_item');
    //validRITM.addQuery('cat_item', 'Service Now Access Request');
    //validRITM.addQuery('sys_created_on', '>=', gs.daysAgo(6));
    validRITM.addEncodedQuery('cat_item.nameSTARTSWITHService Now Access Request^sys_created_onONThis month@javascript:gs.beginningOfThisMonth()@javascript:gs.endOfThisMonth()^variables.fab0fc6fdbeea41095f249803996192b=admin^variables.0f00742fdbeea41095f2498039961924=New access^approval=approved'); //ritm created in this month, catalog item variables is'New Access'
    validRITM.query();
    while (validRITM.next()) {
        arr2.push(validRITM.number);

        var requestedFor = validRITM.variables.u_beneficiary;

    }
   gs.print('Admin user requestedFor' +requestedFor);
}

 TIA!!
3 REPLIES 3

GlideFather
Tera Patron

Hi @Ankita9793,

you are able to locate the recently added admins, and for the RITMs - you get any error or what's the question?

 

How do you test it?

———
/* If my response wasn’t a total disaster ↙️ drop a Kudos or Accept as Solution ↘️ Cheers! */


palanikumar
Giga Sage
Giga Sage

Hi @Ankita9793 

Can you tell me what is the output do you see?

I see 2 issues and 1 question in your script:

 1) arr2.push(validRITM.number) - Since validRITM is object it will change for each iteration, and you will see a blank value for each entries. It should be replaced with arr2.push(validRITM.number.toString())

2) gs.print('Admin user requestedFor' +requestedFor); - Variable requestedFor is declared inside the while loop. So this will not work. 

3) var requestedFor = validRITM.variables.u_beneficiary - Is variable name prefixed with u_? Just a question. Correct it if it is different

Try this updated code:

var arr = [];
var arr2 = [];
var urole = new GlideRecord('sys_user_has_role');
urole.addEncodedQuery('role=2831a114c611228501d4ea6c309d626d^sys_created_onONYesterday@javascript:gs.beginningOfYesterday()@javascript:gs.endOfYesterday()'); //role is admin and created on yesterday
urole.query();
while (urole.next()) {
    arr.push(urole.user + '');

    var validRITM = new GlideRecord('sc_req_item');
    //validRITM.addQuery('cat_item', 'Service Now Access Request');
    //validRITM.addQuery('sys_created_on', '>=', gs.daysAgo(6));
    validRITM.addEncodedQuery('cat_item.nameSTARTSWITHService Now Access Request^sys_created_onONThis month@javascript:gs.beginningOfThisMonth()@javascript:gs.endOfThisMonth()^variables.fab0fc6fdbeea41095f249803996192b=admin^variables.0f00742fdbeea41095f2498039961924=New access^approval=approved'); //ritm created in this month, catalog item variables is'New Access'
    validRITM.query();
    while (validRITM.next()) {
        arr2.push(validRITM.number.toString()); // .toString() added
        var requestedFor = validRITM.variables.u_beneficiary.toString(); // Since this is variable I'm not sure whether this variable is prefixed by u_
        gs.print('Admin user requestedFor' + requestedFor); // Moved this inside the while loop
    }
}

 

 

Thank you,
Palani

Community Alums
Not applicable

Hi @Ankita9793 ,

Assuming that variables.fab0fc6fdbeea41095f249803996192b corresponds to the beneficiary variable in your catalog item, you can modify your script as follows:

var validRITM = new GlideRecord('sc_req_item');
    //validRITM.addQuery('cat_item', 'Service Now Access Request');
    //validRITM.addQuery('sys_created_on', '>=', gs.daysAgo(6));
    validRITM.addEncodedQuery('cat_item.nameSTARTSWITHService Now Access Request^sys_created_onONThis month@javascript:gs.beginningOfThisMonth()@javascript:gs.endOfThisMonth()^variables.fab0... + urole.user + '^variables.0f00742fdbeea41095f2498039961924=New access^approval=approved'); //ritm created in this month, catalog item variables is'New Access'
    validRITM.query();
    while (validRITM.next()) {
        arr2.push(validRITM.number);
        var requestedFor = validRITM.variables["dc773765831072104a38b8a6feaad36f"].name;
}
gs.print('Admin user requestedFor: ' +requestedFor);

Thanks & Regards,
Muhammad Iftikhar
If my response helped, please mark it as the accepted solution so others can benefit as well.