Built something you're proud of? Tell the story. A quick G2 review of App Engine or Build Agent helps other developers see what's possible on ServiceNow. Share your experience.

Before Query Business Rule to restrict access

Zubair Alam2
Tera Contributor

Can someone please help me with a query business rule to achieve this requirement?

 

Vendors should only be able to see records actively assigned to them via their group.

 

 

All help is greatly appreciated. 

5 REPLIES 5

@Zubair Alam2 

try this

(function executeRule(current, gScripting, gRequest, gResponse) {
    // Get the logged-in user's groups
    var userGroups = gs.getUser().getMyGroups();
    var vendorGroups = [];

    // Check if any of the user's groups are vendor groups
    var grGroup = new GlideRecord('sys_user_group');
    grGroup.addQuery('sys_id', 'IN', userGroups.toString());
    grGroup.addQuery('u_vendor_group', true);
    grGroup.query();
    while (grGroup.next()) {
        vendorGroups.push(grGroup.sys_id.toString());
    }

    // If the user is in any vendor group, restrict the records they can see
    if (userGroups.length > 0) {
        current.addQuery('assignment_group', 'IN', userGroups.join(','));
    } else {
        current.addQuery('sys_id', ''); // No records will be returned
    }
})();

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

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