How to add role to user when user enters in field?

Supriya P
Tera Contributor

Hello all,

In sn_bcp_plan table i have BCP reviewer field.

SupriyaP_0-1691033696664.png

 

If we add user or users to this field bc_reviewer role should be added automatically to that user.

How can we achieve this?

 

 

1 ACCEPTED SOLUTION

use log statements for current.contributors and groupID and check if they are returning expected values.

use hasNext() instead of next.

Please hit the thumb and Mark as correct if it helped!

 

Kind Regards,

Ravi Chandra

View solution in original post

3 REPLIES 3

Ravi Chandra_K
Kilo Patron
Kilo Patron

Hello @Supriya P 

Greetings!

You have to write update Business Rule on BCP Plan table 

(function executeRule(current, previous /*null when async*/) {

 

    // Add your code here

 

var roleRecord = new GlideRecord('sys_user_has_role');

roleRecord.initialize();

roleRecord.user = gs.getUserID();

roleRecord.role = 'role sys_id here'; // give here the role sys_id

roleRecord.insert();

 

})(current, previous);

Please mark the answer as correct solution and helpful if helped.

 

Kind Regards,

Ravi chandra

Hello Ravi,

 

This script is not working, can we add user in a group instead of adding role to user. I have written below script but it is also not working.

var users2;
var groupID2;
users2= current.contributors;

groupID2 =gs.getProperty('sn_bcp.bcm_add_member_to_bcm_reviewer_group');

var memberRec = new GlideRecord('sys_user_grmember');
memberRec.addQuery('user',users2);
memberRec.addQuery('group', groupID2);
memberRec.query();
if(!memberRec.next()) {
memberRec.initialize();
memberRec.user = users2;
memberRec.group = groupID2;
memberRec.insert();
}

OR

 

var grpRecord = new GlideRecord('sys_user_grmember');

grpRecord.initialize();

grpRecord.user = gs.getUserID();

grpRecord.role = gs.getProperty('sn_bcp.bcm_add_member_to_bcm_reviewer_group'); // give here the role sys_id

grpRecord.insert();

use log statements for current.contributors and groupID and check if they are returning expected values.

use hasNext() instead of next.

Please hit the thumb and Mark as correct if it helped!

 

Kind Regards,

Ravi Chandra