ATF question re: drawing information from a record query to use as reference in a second query

Josh_
Tera Contributor

I am trying to write an ATF test that should be simple, but has one sticking point right at the beginning.

 

I need to impersonate a member of a specific group.  Best practices say that we should not directly impersonate an existing user, so I have two choices: create one of my own, or try to path my way into impersonating someone in the org who is a member of that group, but whose identity is not fixed.

 

I query sys_user_grmember to find a user in that group, which is fine.

 

But the record is a Group Member record, rather than a User record.  There is no way to dot walk my impersonation step to the User field in the Group Member record.  I have tried other, similar paths, but cannot find a way to do it.  I'm sure there is scripting that could do it, but I am unclear as to what would work.

 

If anyone has an idea as to how this might progress, without creating a new user and assigning all roles and groups, I would appreciate it.

 

1.  Record query on sys_user_grmember for [group name] and email > is not empty (empty emails are system integration accounts for us).  Current query finds 41 group members.

2.  Would like to impersonate someone from step 1.  But cannot dot walk at all beyond the record ID itself; if I could get to Group Member.User, this is a simple job.

 

Please advise?

1 ACCEPTED SOLUTION

Chidanand_VK
Tera Expert

Hi @Josh_ ,

Use the below script in the 'Run Server Side Script' step and in the Impersonation test step select this (Run Server Side Script test step) using data pill picker.

 

var group = new GlideRecord("sys_user_group");
group.get('11f6346787ad4990db63ece60cbb35ba'); //SYS ID of the Group

var groupMember = new GlideRecord("sys_user_grmember");
groupMember.addEncodedQuery('group=' + group.sys_id + '^user.emailISNOTEMPTY'); //Adding a query of User.Email is NOT Empty
groupMember.query();

while(groupMember.next()) {
    var userSysId = groupMember.user.sys_id;
    outputs.record_id = userSysId; //Getting Output of this Run Server Side Script test step as the User record
}

View solution in original post

6 REPLIES 6

Josh_
Tera Contributor

Yes, unfortunately.

 

We're having an issue with created users and role/group assignments, so a lot of tests that were working a little while ago are now failing because users aren't being given proper assignments.  I'm looking for a workaround to grab an existing user from a specific group so we don't have to worry about new assignments.  But I believe there is a script posted somewhere below that will do it.

Ankur Bawiskar
Tera Patron
Tera Patron

@Josh_ 

approach shared by @Shaqeel  is easiest

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