On display business rule to hide form section

JJG
Kilo Guru

Hello,

I have an on display business rule that checks if a user is an admin. I would like to hide a form section with the name of 'TEST' if the user does not have the admin role. What is the code to hide the section?

Heres what I have so far:

(function executeRule(current, previous /*null when async*/ ) {
var usr = gs.getUser();
if (!usr.hasRole('admin')) {


} else if (usr.hasRole('admin')) {


}
})(current, previous);

1 ACCEPTED SOLUTION

Mark Roethof
Tera Patron
Tera Patron

Hi JJG,

I don't know if this can be done with Business Rule actually. Though a example onLoad Client Script:

function onLoad() {

	if(!g_user.hasRole('admin')) {
		g_form.setSectionDisplay('your_section_name', false);
	}
}

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

View solution in original post

4 REPLIES 4

Mark Roethof
Tera Patron
Tera Patron

Hi JJG,

I don't know if this can be done with Business Rule actually. Though a example onLoad Client Script:

function onLoad() {

	if(!g_user.hasRole('admin')) {
		g_form.setSectionDisplay('your_section_name', false);
	}
}

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

Its not possible server-side?

Don't know for sure. Though never found anything server side on this before.

Don't really like this myself, because it is client side, and client side can sometimes be slow/corrupt/able to influence.
If these for example are important fields, you might want to consider restricting with ACL's. That way you could hide (read acl) the fields server side, only thing would be that the section would be empty (for non-admin), section which you only need to hide with client script which feels more acceptable.

Obviously, this is far more work 😞

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

karthik120
Giga Expert

Hi,

Try below code to achieve above functionality using client script.

function onLoad() {

	if(g_user.hasRoles('admin') == true) {
		g_form.setSectionDisplay('TEST', true);
	}
       else{
                g_form.setSectionDisplay('TEST', false);
        }

}

Thanks,

karthik