Query Business rule on RITM to access specific item records

Ram141
Tera Expert

Hello Team,

I got a requirement where I need to filter or show 'HR' catalog Item (RITM) records to specific user i.e.,

If is part of group xyx or

If user is part of group abc or

If user is Requested For.

I tried writing query business rule on RITM table with the below script but somehow it's not working could any one please help me with your inputs to achieve this scenario.

 

(function executeRule(current, previous /*null when async*/ ) {

    var curUserID = gs.getUserID();
    var user = gs.getUser();
    var gr = current.addQuery('cat_item.name', 'HR System Requests');
    var xyz = '1ab75ae587e19a90b865206acebb3591';
    var abc = '4f37a9983b465ad0cb51624a85e45afe';
   
    if(!(user.isMemberOf(abc) || user.isMemberOf(xyz))){
        var requestedFor = current.addQuery('requested_for',curUserID);
        requestedFor.or();
    }
})(current, previous);
1 ACCEPTED SOLUTION

Abhijit4
Mega Sage

Hi @Ram141 

 

I have create query BR which handles all of your 3 scenarios:

- If user is not part of XYZ and ABC group-> HR system requests will be hidden.

- If user is not part of XYZ and ABC group but he is requested by or opened by person-> only those records will be visible to user.

- If user is neither part of XYZ, ABC group nor he is requested for -> All HR system requests will be hidden.

 

I have created new Query BR on PDI and tested and it works prefectly.

 

PFB script for your reference:

 

(function executeRule(current, previous /*null when async*/ ) {

    var curUserID = gs.getUserID();
    var user = gs.getUser();
    var xyz = '1ab75ae587e19a90b865206acebb3591';
    var abc = '4f37a9983b465ad0cb51624a85e45afe';


    if (!user.isMemberOf(abc) && !user.isMemberOf(xyz)) {
		
		var currentQuery=current.addQuery('cat_item.name','!=','HR System Requests');
		var currentQuery2 = currentQuery.addOrCondition('requested_for', curUserID);
		currentQuery2.addOrCondition("opened_by",curUserID); //Hide HR System Requests RITM's for all users except for requester for and opened by user

	}

})(current, previous);

 

 

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

View solution in original post

10 REPLIES 10

Arun_Manoj
Mega Sage

Hi @Ram141 ,

 

arunm1_0-1746443330457.png

 

Ankur Bawiskar
Tera Patron
Tera Patron

@Ram141 

try this

(function executeRule(current, previous /*null when async*/) {
    var curUserID = gs.getUserID();
    var user = gs.getUser();
    var xyzGroupID = '1ab75ae587e19a90b865206acebb3591';
    var abcGroupID = '4f37a9983b465ad0cb51624a85e45afe';

    // Query for 'HR System Requests' catalog items
    var gr = current.addQuery('cat_item.name', 'HR System Requests');

    // Check if the user is a member of group xyz or abc
    if (user.isMemberOf(xyzGroupID) || user.isMemberOf(abcGroupID)) {
        // If the user is a member of either group, no additional query is needed
        return;
    } else {
        // If the user is not a member of either group, add a query to check if they are the requested_for user
        current.addQuery('requested_for', curUserID);
    }
})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Abhijit4
Mega Sage

Hi @Ram141 

 

I have create query BR which handles all of your 3 scenarios:

- If user is not part of XYZ and ABC group-> HR system requests will be hidden.

- If user is not part of XYZ and ABC group but he is requested by or opened by person-> only those records will be visible to user.

- If user is neither part of XYZ, ABC group nor he is requested for -> All HR system requests will be hidden.

 

I have created new Query BR on PDI and tested and it works prefectly.

 

PFB script for your reference:

 

(function executeRule(current, previous /*null when async*/ ) {

    var curUserID = gs.getUserID();
    var user = gs.getUser();
    var xyz = '1ab75ae587e19a90b865206acebb3591';
    var abc = '4f37a9983b465ad0cb51624a85e45afe';


    if (!user.isMemberOf(abc) && !user.isMemberOf(xyz)) {
		
		var currentQuery=current.addQuery('cat_item.name','!=','HR System Requests');
		var currentQuery2 = currentQuery.addOrCondition('requested_for', curUserID);
		currentQuery2.addOrCondition("opened_by",curUserID); //Hide HR System Requests RITM's for all users except for requester for and opened by user

	}

})(current, previous);

 

 

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

Ram141
Tera Expert

@Abhijit4 @Ankur Bawiskar 

Thanks for providing quick response.

 

I have updated the script as below but it's not showing any RITM records when I checked with admin access not getting where exactly it's failing, also we need to show RITM Records to both xyz and abc group users and if user is requester then we need to show that records to requester,

 

(function executeRule(current, previous /*null when async*/ ) {
    var curUserID = gs.getUserID();
    var user = gs.getUser();
    var groupIMManageHR = '1ab75ae587e19a90b865206acebb3591';
    var groupHrIs = '4f37a9983b465ad0cb51624a85e45afe';

    // Query for 'HR System Requests' catalog items
    var gr = current.addQuery('cat_item.name', 'HR System Requests');

    // Check if the user is a member of group xyz or abc
    if (user.isMemberOf(groupIMManageHR) || user.isMemberOf(groupHrIs)) {
        // If the user is a member of either group, no additional query is needed
        return;
    } else {
        // If the user is not a member of either group, add a query to check if they are the requested_for user
        current.addQuery('requested_for', curUserID);
    }
})(current, previous);