Autofill Primary group when a user added to some specific group.

Rakesh Behera
Tera Contributor

Hi All,

I have a custom field "Primary group" in user table which is reference to group table. 

There are some particular group if the user is added to any of those groups then his primary group will be auto-filled with that  group. I tried to achieve this through  BR in User table but my code is not working as expected.

Use this piece of code:

      var userGrp = new GlideRecord('sys_user_grmember');
        userGrp.addQuery('user', current.sys_id);  userGrp.addEncodedQuery('group=8a4dde73c6112278017a6a4baf547aa7^ORgroup=287ebd7da9fe198100f92cc8d1d2154e^ORgroup=287ee6fea9fe198100ada7950d0b1b73');
        userGrp.query();
        if (userGrp.next()) {
            current.u_primary_group = userGrp.group;
             current.update();
  

And put this condition in when to run :

After Insert &Update

Active is True 

and

Primary support group is empty.

 

 

Thanks

@Ankur Bawiskar 

@Mark Roethof 

@Kalaiarasan Pushpanathan 

@Sandeep Dutta 

@Aman Kumar 

@Musab Rasheed 

@Mohith Devatte 

 

5 REPLIES 5

Mohith Devatte
Tera Sage
Tera Sage

hello @Rakesh Behera ,

mark sure your BR is on sys_user_grmember table and remove the encoded query and put this in the trigger condition of BR in when to run as 

group is your group name OR group is your group name

and try this script below

      var userGrp = new GlideRecord('sys_user');
        userGrp.addQuery('user', current.user);  
        userGrp.query();
        if (userGrp.next()) {
           userGrp.u_primary_group =current.group;
            userGrp.update();
}

please mark my answer correct if it helps you

Aman Kumar S
Kilo Patron

Just a recommendation, script that Mohith suggested will work for you but also consider the false cases, so whenever a user is removed from the primary group, its user record should be updated accordingly.

In that case have a Before Delete BR on your sys_user_grmember table to handle it

Best Regards
Aman Kumar

Ankur Bawiskar
Tera Patron
Tera Patron

@Rakesh Behera 

Hi,

you are running this BR on sys_user table right. But when you add user to group the record belongs to sys_user_grmember

So do this

BR: After insert on sys_user_grmember

Condition:

current.user.u_primary_group == '' && (current.group == '8a4dde73c6112278017a6a4baf547aa7' || current.group == '287ebd7da9fe198100f92cc8d1d2154e' || current.group == '287ee6fea9fe198100ada7950d0b1b73')

Script:

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

	// Add your code here
	var user = current.user.getRefRecord();
	user.u_primary_group = current.getValue('group');
	user.update();

})(current, previous);

Ensure you discuss with client what should happen when user is removed from the primary group

You should be clearing it out from user record as well I believe

BR: Before Delete on sys_user_grmember

Condition:

current.user.u_primary_group != '' && current.user.u_primary_group == current.group

Script:

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

	// Add your code here
	var user = current.user.getRefRecord();
	user.u_primary_group = '';
	user.update();

})(current, previous);

regards
Ankur

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

Thank you so much its working as expected now.