
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-03-2017 03:17 AM
Hi,
I have a new on change BR that will fire on change of a field.
Now I want all existing records (on sys_user_group) to run the BR once ... so the field X should be updated (by X again) ... .
When creating a job, that runs through all records and update the field with it's own value, no update will be done I assume ...
So how does the best practice would look like here to achieve the run of the BR?
Would be cool if you could provide some script I could modify.
THank you
Solved! Go to Solution.
- Labels:
-
Best Practices
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-03-2017 07:23 AM
var roleID = "011ba5aa0a0a0b3001a439c580549134"; //role_delegator
var userGroup = new GlideRecord('sys_user_group');
userGroup.addNotNullQuery('manager');
userGroup.query();
while(userGroup.next()) {
// add role to new manager
var gr = new GlideRecord("sys_user_has_role");
gr.addQuery("user", userGroup.manager);
gr.addQuery("role", roleID);
gr.addQuery("granted_by", userGroup.sys_id);
gr.query();
if (gr.next())
gs.log(userGroup.manager.getDisplayValue() + " already has the role_delegator role for the " + userGroup.name + " group - not adding");
else {
gr.initialize();
gr.user = userGroup.manager;
gr.role = roleID;
gr.granted_by = userGroup.sys_id;
gr.inherited = false;
gr.insert();
gs.addInfoMessage(gs.getMessage("role_delegator role granted to") + " " +
userGroup.manager.getDisplayValue() + " " + gs.getMessage("in") + " " + userGroup.name + " " + gs.getMessage("group"));
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-03-2017 06:19 AM
sounds good but does not really update the record ;-(
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-03-2017 06:16 AM
Can you please paste in the business rule code? From there I can help come up with a script to run once. You could use a background script to run it or better yet use a Fix Script:
http://wiki.servicenow.com/index.php?title=Fix_Scripts#gsc.tab=0

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-03-2017 06:23 AM
In fact it an OOB BR that has been activated (0672c9970a0a0bad01d7ddbff9d08d1f) > Group Manager Change
Will give the grand role delegation role to the manager (and after a fix also sets the granted by for each group).
But as we had this issue in the past already; i'd like some kind of general solution, to be used on tables that just need to get the update forced.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-03-2017 06:39 AM
Simply forcing an update on all the groups won't get this OOB business rule to execute since it is looking for the manager value to change. You will need to run the script manually.
OK so you can either elevate your priveliges to security_admin and run the script below via background script or you can create a Fix Script and paste the code into it and run it. Either way it will accomplish the same thing. The nice thing about the Fix Script option is you can move this script via update set through your development and production stack. This should update all of your groups appropriately and then assuming the business rule is working appropriately now should maintain this automatically moving forward.
var userGroup = new GlideRecord('sys_user_group');
userGroup.addNotNullQuery('manager');
userGroup.query();
while(userGroup.next()) {
// add role to new manager
var gr = new GlideRecord("sys_user_has_role");
gr.addQuery("user", userGroup.manager);
gr.addQuery("role", role.sys_id);
gr.addQuery("granted_by", userGroup.sys_id);
gr.query();
if (gr.next())
gs.log(userGroup.manager.getDisplayValue() + " already has the role_delegator role for the " + userGroup.name + " group - not adding");
else {
gr.initialize();
gr.user = userGroup.manager;
gr.role = role.sys_id;
gr.granted_by = userGroup.sys_id;
gr.inherited = false;
gr.insert();
gs.addInfoMessage(gs.getMessage("role_delegator role granted to") + " " +
userGroup.manager.getDisplayValue() + " " + gs.getMessage("in") + " " + userGroup.name + " " + gs.getMessage("group"));
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-03-2017 06:55 AM
"role" is not defined.