
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2014 06:55 AM
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.
Please help with any ideas of how I can accomplish this.
Thank you,
Yeny
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-02-2014 06:20 AM
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
Enhance Knowledge NOW@ www.solutioningnow.com
http://www.solutioningnow.com/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2014 07:02 AM
You're probably going to want to make the field itself a reference field to the group table instead of a string. That way when you query to get the sys_id for the group, all you need to do is set the field value. It'll already contain the sys_id. Plus, having it as a reference field will allow you to view additional information about the group.
If you're looking at typical groups, you'd be querying the sys_user_group table. The field you'd likely search on here is Name.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2014 07:22 AM
Hi Yeny,
As mentioned by Aaron, its a good practice to create default group as reference data type.
Solution 1: If it's a one time/on-demand activity to update the default group for all users, I would recommend to create an on-demand schedule job instead of business rule.
Here is what you should be doing,
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();
}
}
Solution 2: If this activity needs to be running everytime when a user is associated with these type of groups, create a BR as below:
Table: sys_user_grmember
After: Insert/Update
Condition: (current.group.name.indexOf('APP-SNIncident') > -1) && current.user.u_default_group.nil()
Script:
autofillDefaultGroup();
function autofillDefaultGroup() {
var userObj = current.user.getRefRecord();
userObj.u_default_group = current.group;
userObj.update();
}
Please mark answer as correct/helpful, if it was really helpful.
Regards,
Solutioner
Enhance Knowledge NOW@ www.solutioningnow.com
http://www.solutioningnow.com/

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2014 09:41 AM
Hi Solutioner and Aaron,
Thank you for your response... I originally had the field as reference to the group table but I changed it to String thinking it would be better to populate the info from the related list, but after you clarified that it's better to have it as a reference field, I switched it back.
I have a question about the BR... should I add 'name' to the line userObj.u_default_group = current.group; to read userObj.u_default_group = current.group.name;? Would that make a difference? When I ran the BR, it didn't work for me...
Thank you,
Yeny
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2014 09:48 AM
Hello,
For comparing, below line of code should work fine:
userObj.u_default_group = current.group;
userObj.u_default_group = current.group.name;
I am assuming "u_default_group" is reference field , hence above line of code will not work as "u_default_group" is holding a sys_id ie., reference to group table not the group name. You can try below line of code:
userObj.u_default_group.name = current.group.name;
Please mark answer as correct/helpful, if it was really helpful.
Regards,
Solutioner
Enhance Knowledge NOW@ www.solutioningnow.com
http://www.solutioningnow.com/