How to automatically add new users to a group when they are added to users

Gemma4
Mega Sage

 

Hi everyone!

I would like to ask your assistance, I have this requirement when a new user is added to the sys_users table I need them automatically added to the PM submitter group. I created a business rule but it is not automatically adding new users. Any suggestions on what the best practice to achieve this would be. 

 

 

thanks

1 ACCEPTED SOLUTION

Hello @Gemma4 ,

What I observed in the First screenshot, You added Group as PM Submitter, but you forgot to add the User field with the value from Trigger->User Record

Please check the Second Screenshot from my Answer

 

I hope this clears your question

 

Please mark my answer helpful and correct, If it helps you

Thank you

 

 

Please mark my answer helpful  & correct if it helps you
Thank you

G Ramana Murthy
ServiceNow Developer

View solution in original post

11 REPLIES 11

Revanth Karra
Tera Expert

Dear Gemma,

 

@Gemma4  , hope you're doing great!

 

Sample Business Rule:

 

 

// Business rule
function onAfter(current, previous) {
  // Get the PM submitter group.
  var group = GlideRecord('sys_user_group');
  group.get('name', 'PM submitter');

  // Add the new user to the group.
  var groupMember = GlideRecord('sys_user_grmember');
  groupMember.initialize();
  groupMember.user = current.sys_id;
  groupMember.group = group.sys_id;
  groupMember.insert();
}

 

 

This business rule will be triggered after a new user is added to the sys_users table. The business rule will then get the PM submitter group and add the new user to the group.

 

To attach the business rule to the sys_users table, follow these steps:

  1. Navigate to System Administration > Business Rules.
  2. Click the New button.
  3. Enter a name and description for the business rule.
  4. Select the Table that you want to apply the business rule to. In this case, you would select the sys_users table.
  5. Select the Condition that you want to use to trigger the business rule. In this case, you would select the After condition and the Insert event.
  6. Select the Action that you want to perform when the business rule is triggered. In this case, you would select the Insert action and the sys_user_grmember table.
  7. Click the Save button.

Once you have attached the business rule to the sys_users table, new users will be automatically added to the PM submitter group when they are added to the sys_users table.

 

TIPS:

  • Test your business rules thoroughly before deploying them to production. Make sure that the business rules are working as expected and that they are not causing any unexpected behavior.
  • Document your business rules so that you can easily revert to the original configuration if needed.
  • Use a version control system to manage your business rules. This will help you to track changes to your business rules and to recover from any problems.

 

Kindly, please mark my solution as Helpful/Correct, if applicable. If I could help you with your Query then, please hit the Thumb Icon and mark as Correct!!!

 

Thanks & Regards, 

Revanth. K

Product Test Automation Engineer

Hi @Revanth Karra 

Thank you for all the details but unfortunately the user did not get automatically added to the group. Under the "actions" step I can not add a different table. When I add sys_user_grmember it automatically saves as sys_user. Perhaps this is the issue? See attached. 

advanced.PNGwhentorun.PNGaction.PNG 

RAMANA MURTHY G
Mega Sage
Mega Sage

Hello @Gemma4,

We can do it with Flow Designer with two simple steps

1. You need to create a Trigger Create Record on User [sys_user] table.

RAMANAMURTHYG_0-1695369467297.png

 

2. Create an action Create Record under trigger

Table should be Group Member [sys_user_grmember]

Fields

      User - Drag sys_id data pill  Trigger -> User Record -> sys_id  (You can drag entire User Record data pill from Tigger)

      Group - <select your required group, PM Submitter in your case

RAMANAMURTHYG_1-1695369619664.png

 

Save and Active your Flow, It works as you expected.

 

Please mark this as correct and helpful, if it resolves your query.

Thank you

 

 

Please mark my answer helpful  & correct if it helps you
Thank you

G Ramana Murthy
ServiceNow Developer

Hi @RAMANA MURTHY G 

I added the flow and feel we are really close to resolving this. Unfortunately when the user is added, the record is displaying as empty? see attached. Not sure why the userid is not getting copied. 

flopw2922.PNGflow1922.PNGtestgroup.PNG

Hello @Gemma4 ,

What I observed in the First screenshot, You added Group as PM Submitter, but you forgot to add the User field with the value from Trigger->User Record

Please check the Second Screenshot from my Answer

 

I hope this clears your question

 

Please mark my answer helpful and correct, If it helps you

Thank you

 

 

Please mark my answer helpful  & correct if it helps you
Thank you

G Ramana Murthy
ServiceNow Developer