- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-19-2023 09:11 AM
I have created a before business rule and ,insert as true and used the following script ,but i am not getting the results,please adivce
(function executeRule(current, previous /*null when async*/ ) {
if (current.operation() == 'insert') {
var assignmentGroup = current.assignment_group;
var caller = current.caller;
if (assignmentGroup && caller) {
var group = new GlideRecord('sys_user_group');
if (group.get(assignmentGroup)) {
group.addMember(caller);
}
}
}
})(current, previous);
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-20-2023 05:08 AM - edited 10-20-2023 05:19 AM
Hi @spectura77
Can you try below code :
/*1. Get values from current Incident form */
var caller = current.getValue('caller_id');
var group = current.getValue('assignment_group');
/*2. glide record on sys_user_grmember table */
var grMem = new GlideRecord('sys_user_grmember');
grMem.addEncodedQuery('group=' + group +'^user=' + caller);
grMem.query();
if(!grMem.next()){
/*3. Add user to group */
var grAddMem = new GlideRecord('sys_user_grmember');
grAddMem.initialize();
grAddMem.setValue('group',group); //group
grAddMem.setValue('user',caller); //caller
grAddMem.insert();
} else {
gs.log("User already a member of group");
}
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-20-2023 05:48 AM
Hello @spectura77 ,
The script is correct. It worked in my PDI. Try to test it again with creating a new record. It should work.
if it doesn't work then please refer to @Vishal Birajdar script that is also another way to achieve the requirement.
Thanks,
Alka
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-20-2023 05:48 AM
Hello @spectura77 ,
The script is correct. It worked in my PDI. Try to test it again with creating a new record. It should work.
if it doesn't work then please refer to @Vishal Birajdar script that is also another way to achieve the requirement.
Thanks,
Alka
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-20-2023 06:07 AM
Hi @Alka_Chaudhary ,this script worked there was another business rule that was overriding the business rule that i created
thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-20-2023 05:08 AM - edited 10-20-2023 05:19 AM
Hi @spectura77
Can you try below code :
/*1. Get values from current Incident form */
var caller = current.getValue('caller_id');
var group = current.getValue('assignment_group');
/*2. glide record on sys_user_grmember table */
var grMem = new GlideRecord('sys_user_grmember');
grMem.addEncodedQuery('group=' + group +'^user=' + caller);
grMem.query();
if(!grMem.next()){
/*3. Add user to group */
var grAddMem = new GlideRecord('sys_user_grmember');
grAddMem.initialize();
grAddMem.setValue('group',group); //group
grAddMem.setValue('user',caller); //caller
grAddMem.insert();
} else {
gs.log("User already a member of group");
}
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-20-2023 06:08 AM