How to add roles using business rule

Ansuha Chipuri
Kilo Guru

Hi,

on Creation of a user in csm_consumer_user table i want to add of consumer to that user.

how can i script this in business rule

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

You can have after insert BR on csm_consumer_user table

Script:

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

    // Add your code here

var roleRecord = new GlideRecord('sys_user_has_role');
roleRecord.initialize();
roleRecord.user = current.sys_id;
roleRecord.role = 'role sys_id here'; // give here the role sys_id
roleRecord.insert();

})(current, previous);

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

4 REPLIES 4

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Something like this should work (only change the roles!). You might also consider playing around with Flow Designer, with that you can achieve the same no code.

Business rule
Table csm_consumer_user
After (or Async)
Insert

+ Script:

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

	var gr = new GlideRecord('sys_user_has_role');
	gr.initialize();
	gr.setValue('user', current.getUniqueValue());
	gr.setValue('role', 'sysid_of_the_other_role');
	gr.insert();

})(current, previous);

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

You can have after insert BR on csm_consumer_user table

Script:

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

    // Add your code here

var roleRecord = new GlideRecord('sys_user_has_role');
roleRecord.initialize();
roleRecord.user = current.sys_id;
roleRecord.role = 'role sys_id here'; // give here the role sys_id
roleRecord.insert();

})(current, previous);

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Would you mind marking my answer as correct? Ankur is just posting almost the same what I already mentioned before.

Kind regards,
Mark

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Hi Mark,

Seems your post was edited later possibly with the script after I posted mine.

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader