How to get a group to populate in a field?

YenGar
Mega Sage

Hi all,

 

So I am not very experienced with this but I am trying to write a script to have a group populate in a custom field in the User table. The custom field is u_default_group. I think I have to create a Business Rule for this and have written something like the below but I don't have an idea of how to complete it. I am trying to pull the information from the related list "Groups" on the table sys_user_grmember, and since that is a reference to the "Group" (sys_user_group) table, I am not sure what to make the Glide Record on.

 

What I want to do is to pull the first group on the related list that contains "App-SNIncident-..." this is how all our groups are written, for example: App-SNIncident-HR or App-SNIncident-StoreSystems.

 

This is the BR that I wrote:

After: Insert/Update

Table: User(sys_user)

script:

autofillDefaultGroup();

function autofillDefaultGroup() {

  var gr = new GlideRecord('group');

  gr.addQuery('group', 'CONTAINS', 'APP-SNIncident');

  gr.query();

  if (gr.next()) {

  gs.setValue('u_default_group', group);

  }

}

 

But I am not sure how to tell it to grab the first group that contains "App-SNIncident-" and assign it to the field on the form. Also, the BR should make an exception for the Service Desk because the group for them is "App-Servicenow-ServiceDesk".

 

This is a screenshot of what I want to do.

Kara.PNG

 

Please help with any ideas of how I can accomplish this.

 

Thank you,

Yeny

1 ACCEPTED SOLUTION

solutioningnow
Giga Guru

Hi Yeny,



For filling default groups for all the existing users, follow the below steps:



1) Go to System navigation type 'Scheduled Jobs' and click on 'New' button.


2) You will get few options in which select 'Automatically run a script of your choosing'


3) Then give a name for the schedule script execution and select the Run field as 'On Demand'


4) Copy the below script and submit.



var users = new GlideRecord('sys_user');


users.addQuery('u_default_group','');


users.addQuery('active','true');


users.query();


while(users.next()) {
          var userGrp = new GlideRecord('sys_user_grmember');
          userGrp.addQuery('user',users.sys_id);
          userGrp.addQuery('group.name','STARTSWITH','APP-SNIncident');
          userGrp.query();
          if(userGrp.next()) {  
                    users.u_default_group = userGrp.group;  
                    users.update();  
          }
}



5) Once submitted you will get a button 'Execute Now' to execute the script on demand.



Try this and do let me know if you have any questions.



Please mark answer as correct/helpful, if it was really helpful.



Regards,


Solutioner


Logo.png


Enhance Knowledge NOW@ www.solutioningnow.com


http://www.solutioningnow.com/


View solution in original post

13 REPLIES 13

nick_2586
Giga Contributor

Hi Yeny,



Excuse me, if I misread the blog, but the above business rule suggested by Solutioner, will only run when you add a user to the group, it wont run for existing group member.


Please make sure you add the user to the group, to verify if the BR works.



Also, you can modify the BR a bit(again as suggested by Solutioner) and schedule a job for one time update of all the existing profiles, thereafter the BR will take care of subsequent updates.



Regards,


Nisheeth


Yeah you are right, this will run only if you add Users to group.



For existing users, run the script as mention before.




Regards,


Solutioner


solutioningnow
Giga Guru

Hi Yeny,



For filling default groups for all the existing users, follow the below steps:



1) Go to System navigation type 'Scheduled Jobs' and click on 'New' button.


2) You will get few options in which select 'Automatically run a script of your choosing'


3) Then give a name for the schedule script execution and select the Run field as 'On Demand'


4) Copy the below script and submit.



var users = new GlideRecord('sys_user');


users.addQuery('u_default_group','');


users.addQuery('active','true');


users.query();


while(users.next()) {
          var userGrp = new GlideRecord('sys_user_grmember');
          userGrp.addQuery('user',users.sys_id);
          userGrp.addQuery('group.name','STARTSWITH','APP-SNIncident');
          userGrp.query();
          if(userGrp.next()) {  
                    users.u_default_group = userGrp.group;  
                    users.update();  
          }
}



5) Once submitted you will get a button 'Execute Now' to execute the script on demand.



Try this and do let me know if you have any questions.



Please mark answer as correct/helpful, if it was really helpful.



Regards,


Solutioner


Logo.png


Enhance Knowledge NOW@ www.solutioningnow.com


http://www.solutioningnow.com/


Hi Solutioner,



Thank you so much for your response. That's what I wasn't understanding at fist. I thought the script will work for users who are already in the group and that's where my confusion was at, but I see now that it will be for new users assigned to the group.


That helped me out a lot and now I got what I wanted to show in the default group field. Both scripts work perfectly. I really appreciate your help and time working with me! I'll be marking your answer correct because it helped me achieve what I wanted.



Thank you again!


Yeny.