- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-04-2022 01:22 AM
When "u_department" value of sys_user record is changed, I would like that user to be removed from the OLD group and then added to the NEW group.
For example, when an user's [u_department] value is changed from "Dep-A" to "Dep-B", the user should be removed from "Dep-A" sys_user_group and then added to "Dep-B" sys_user_group automatically.
I believe that After/Update Business Rule is required, but I'm not very familiar with scripting, so could someone please give me the sample BR for this?
Best Regards,
Aki
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-04-2022 03:53 AM
Hi,
so u_department holds group name then do this
(function executeRule(current, previous /*null when async*/) {
// Add your code here
var gr = new GlideRecord("sys_user_grmember");
gr.addQuery("group.name", previous.u_department);
gr.addQuery("user", current.getUniqueValue());
gr.query();
if (gr.next()) {
gr.deleteRecord();
var gr1 = new GlideRecord("sys_user_grmember");
gr1.initialize();
gr1.setDisplayValue('group',current.u_department);
gr1.user = current.getUniqueValue();
gr1.insert();
}
})(current, previous);
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-04-2022 01:39 AM
Hi,
yes you require after update BR on sys_user
Condition: current.u_department.changes() && current.u_department != ''
Script:
// remove and then add
Note: u_department field is of what type?
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-04-2022 02:00 AM
Hi Ankur,
Thank you your response.
u_department field type is String. Could you please give me the sample script?
I'm not sure how to add queries with previous/current value for the requirements.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-04-2022 02:07 AM
Hi,
why to make u_department as string if it holds group.
why not make it reference to sys_user_group?
If you make it reference to sys_user_group then here is the BR script
(function executeRule(current, previous /*null when async*/) {
// Add your code here
var gr = new GlideRecord("sys_user_grmember");
gr.addQuery("group", previous.u_department);
gr.addQuery("user", current.getUniqueValue());
gr.query();
if (gr.next()) {
gr.deleteRecord();
var gr1 = new GlideRecord("sys_user_grmember");
gr1.initialize();
gr1.group = current.u_department;
gr1.user = current.getUniqueValue();
gr1.insert();
}
})(current, previous);
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-04-2022 02:50 AM
Hi Ankur,
Thank you for the script.
> why not make it reference to sys_user_group?
Actually, Group (sys_user_group) records are created by 3rd party tool (Azure AD) via API and "G" is added as a suffix due to company requirements. *e.g) Dep-AG, Dep-BG, Dep-CG...
[u_department] field of User (sys_user) record also populated by Azure AD, but it doesn't has the suffux "G". *e.g) Dep-A, Dep-B, Dep-C...
There is a discrepancy between them, so I think I can't set [u_department] to Reference field, and need some logic in the script.
Do you have any ideas for the requirements?