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.

non-admin try to Add group

Rafael Batistot
Kilo Patron

Hi Team

To add a a group in a specific user i'm using this script. Is not a beauty code, but work 

var users = current.variables.select_the_user.toString().split(",");
var arrayGrupo = current.variables.group_associate.toString().split(",");

users.forEach(function(userSysId) {
    arrayGrupo.forEach(function(grupoSysId) {
        var groupName = utils.getGroupName(grupoSysId);
        addUserGroupWithId(userSysId, grupoSysId);
    });
});

function addUserGroupWithId(userID, groupID) {
        var userGroupGR = new GlideRecord('sys_user_grmember');
        userGroupGR.initialize();
        userGroupGR.user = userID;
        userGroupGR.group = groupID;
        userGroupGR.insert();
    },


i've applicated in a business rule to trigger when a non-admin try to add an user a specific group 

besides not work i'm facing this issue in the log. 

Add user in group(56c2540c470cf610cbcc97a4116d4384): ReferenceError: "item" is not defined. (#0(eval); line 97)


RafaelBatistot_0-1757976503732.png

With Admin works normally, i checked:

ACL,
Business Rule,
Script Include,
UI and DATA polices
Client Script... everything, bot not found 

if anybody got the same problem i would like to hear you. 

Thank's advance

 






 

If you found this response helpful, please mark it as Helpful. If it fully answered your question, consider marking it as Correct. Doing so helps other users find accurate and useful information more easily.
1 ACCEPTED SOLUTION

Nehal Dhuri
Mega Sage

Hey @Rafael Batistot ,

Is this catalog item meant to add users to a group?
If yes, you can easily achieve this using a Flow. Just make sure the flow’s "Run As" property is set to System User to ensure it has the necessary permissions.

I'm also attaching a screenshot of a similar setup we've implemented.

NehalDhuri_0-1758011070304.png

 

Please hit like and mark my response as correct if that helps

View solution in original post

8 REPLIES 8

RaghavSh
Mega Patron

@Rafael Batistot The error is unrelated and seems to be coming from some script include being called in your script.

 

can you explain bit on below line of code:

 

var groupName = utils.getGroupName(grupoSysId);

Raghav
MVP 2023
LinkedIn

@RaghavSh 

 

I forgot to remove this part of code, this is no relevant, I use to get the name of group to use in another situation 

If you found this response helpful, please mark it as Helpful. If it fully answered your question, consider marking it as Correct. Doing so helps other users find accurate and useful information more easily.

@Rafael Batistot This is part of catalog item and also as per the question, this script is in BR. so are you running this on RITM/Task table?


Raghav
MVP 2023
LinkedIn

Ankur Bawiskar
Tera Patron
Tera Patron

@Rafael Batistot 

share the complete script

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