- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-25-2023 09:08 AM - edited 08-25-2023 09:18 AM
Hello Developers,
Can someone help me with below requirement.
There is a field "Owner" in a custom application form. whenever any user added to that field, a specific role should be automatically added to the user's profile and vice versa when removed from the field. How can we achieve this?
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2023 08:25 AM
@Puneet4418 In your business rule, can you update as follows.
if(current.u_owner!=previous.u_owner){
var userRole = new GlideAggregate('sys_user_has_role');
userRole.addQuery('user',current.u_owner);//replace with your
userRole.addQuery('role','b2d8f7130a0a0baa5bf52498ecaadeb4');//Add your role sys_id here
userRole.addAggregate('COUNT');
userRole.query();
if(userRole.next()){
var roleCount=0;
roleCount= userRole.getAggregate('COUNT');
gs.info(roleCount);
if(roleCount==0){//Only add role if the user doesn't have it
var glideRole = new GlideRecord('sys_user_has_role');
glideRole.initialize();
glideRole.user=current.u_owner;
glideRole.role='b2d8f7130a0a0baa5bf52498ecaadeb4'; //replace with your role sys_id
glideRole.state='active';
glideRole.insert();
if(previous.u_owner!=''){//Code to remove the role from previous owner.
var removeRole = new GlideRecord('sys_user_has_role');
removeRole.addQuery('user',previous.u_owner);
removeRole.addQuery('role','b2d8f7130a0a0baa5bf52498ecaadeb4');//Replace with you role sys_id
gs.info('Previous owner '+previous.u_owner);
if(removeRole.next()){
gs.info('Deleting the record '+ removeRole.getValue('sys_id'));
removeRole.deleteRecord();
}
}
}
}
}
Can you check if both the info logs are printed and what values do they print.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2023 07:02 AM
@Jaspal Singh Its our client's requirement.