
- 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 10:37 AM
Hi Solutioner,
Thank you for your response. I tried it again but I still get nothing in the u_default_group field in the user form. I did switch it to a reference field to the Group table. I copied exactly how you wrote the BR but it is not working as it should as the u_default_group field is still empty when I check it. I don't understand what I could be doing wrong?
Please advise.
Yeny.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2014 10:50 AM
Hello Yeny,
Since you updated dictionary field of existing Default Group. Can you create another reference field like Default Assignment Group(u_default_assignment_group) & try below script:
Condition:
(current.group.name.indexOf('APP-SNIncident') > -1) && current.user.u_default_assignment_group.nil()
Script:
autofillDefaultGroup();
function autofillDefaultGroup() {
var userObj = current.user.getRefRecord();
userObj.u_default_assignment_group= current.group;
userObj.update();
}
Just FYI this Business Rule will run for Group having phrase "APP-SNIncident" in name.
Let me know the outcome
Regards,
Solutioner

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2014 11:05 AM
Hi Solutioner,
I created the field as a reference to the Group table and copied the BR like you wrote but when I check the field in a user that has an "App-SNIncident-..." group, it's still not pulling the information...
I was looking over the wiki, and it says that the .getRefRecord() gets a GlideRecord object for a given element... Do you think I need to add a Glide Record element in the business rule like the one in the screen shot below?
Thank you,
Yeny!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2014 11:15 AM
Hello,
I have setup the same configuration in my instance and its working fine. Can you try below methods also:
autofillDefaultGroup();
function autofillDefaultGroup() {
var userObj = new GlideRecord('sys_user');
userObj.get(current.user);
userObj.u_default_assignment_group= current.group;
userObj.update();
}
Or using GlideRecord as same mention in wiki:
autofillDefaultGroup();
function autofillDefaultGroup() {
var userObj = new GlideRecord('sys_user');
userObj.addQuery('sys_id',current.user);
userObj.query();
while( userObj.next()){
userObj.u_default_assignment_group= current.group;
userObj.update();
}
}
Let me know the outcome
Regards,
Solutioner

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2014 11:32 AM
Hi Solutioner,
I tried both of your scripts in the BR but still doesn't pull the information to the field... I'm not understanding why it's not working... Could you share a screen shot of what you have in the user table and in the business rule for me to see if I'm doing something different than you? What could I be doing wrong?
I have also checked all the business rules to make sure nothing is conflicting but nothing is pointing to that field in the user form... What do you think?
Thank you so much for all your help!!
Yeny.