- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-28-2022 01:48 PM - edited ‎11-17-2022 12:15 PM
Hi Experts,
I have created a workflow where a user needs to get added into groups which he selects in the list collector on a catalog item form. The Script to add user is working in background script but not in workflow runscript. Did I miss something?
The challenge is only user should added to group once the group manager approve it. Let's say if user selects 10 groups on the form, then 10 approval records should be under the RITM(this I've achieved). This approval records contain group name in the field 'u_tags'. Now, based on these approval records, if one manager approved then It should add the user to it's respective group right away. It should not wait for the other approvals to get a response. if the 2nd manger approves, then it should add the user to second group.
For this I have created a loop in workflow with below logic:
The script in the 2nd if activity is like below which is working as expected:
answer = ifScript();
function ifScript(){
var apprecs = new GlideRecord('sysapproval_approver');
apprecs.addQuery('sysapproval',current.sys_id);
apprecs.addQuery('state','requested');
apprecs.query();
var cnt = apprecs.getRowCount();
if(cnt == 0){
return 'yes';
}
return 'no';
}
The run script to add user to groups as below:
var requestor = current.variables.req_for; //req_for is the variable on the form
var addGrps = [];
var appr = new GlideRecord('sysapproval_approver');
appr.addQuery('sysapproval',current.sys_id);
appr.addQuery('state','approved');
appr.query();
while(appr.next()){
if(appr.u_tags != ''){
addGrps.push(appr.u_tags); //u_tags is custom variable (type: reference,table: sys_user_group) contains the group name
}
}
addUserToGroup(addGrps, requestor);
function addUserToGroup(grp, usr) {
gs.log('****AddGroups' + grp.length);
for (var i = 0; i < grp.length; i++) {
if (usr != '') {
gs.log('****Group1'+grp[i]);
var grpToAdd = new GlideRecord('sys_user_grmember');
grpToAdd.addQuery('group', grp[i]);
grpToAdd.addQuery('user', usr);
grpToAdd.query();
if (grpToAdd.next()) {
//already in group
gs.log('***** User sys_id: ' + usr + ' already in group: ' + grp);
} else {
gs.log('****Group2'+grp[i]);
grpToAdd.initialize();
grpToAdd.group = grp[i];
grpToAdd.user = usr;
grpToAdd.insert();
workflow.debug('***** User sys_id: ' + usr + ' added to group: ' + grp);
gs.flushMessages();
}
}
}
}
Any help is greatly appreciable.
Thanks in Advance,
Mani.
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-28-2022 04:13 PM
I have a similar script that works fine for me in a run script. The only difference I see is that I don't have a line for grpToAdd.initialize();.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-28-2022 04:13 PM
I have a similar script that works fine for me in a run script. The only difference I see is that I don't have a line for grpToAdd.initialize();.