How to change view based on role of user?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-18-2018 04:02 AM
Suppose there are two roles, roleA and roleB. There are few users present in each of the role.
When user of roleA visits incident form, he should be able to see viewA and similarly when user of roleB visits incident form, he should be able to see viewB.
Please help me to know, how can I achieve above scenario?
- Labels:
-
Best Practices
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-18-2018 04:06 AM
You can do this with a View Rule.
It's possible to script it, so look at any of the out of box view rules containing a script and you can probably figure out how to do this.
Example from other view rule, modify to your needs.
(function overrideView(view, is_list) {
// Add your code here
answer = view;
if (view == "")
answer = 'Automated'; // set the new view to answer
})(view, is_list);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-19-2018 12:17 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-19-2018 04:31 AM
Make sure you have proper view names in the code, same as for roles.
Not sure if you have to declare a variable for anser, otherwise code looks good to me, add some log statements into the rule, one in beginning to make sure the rule itself is executed and then a log statement within each if statement to see if it takes that part or somewhere else.
You could try with a very simple rule like this, just to make sure you got the syntax correct.
(function overrideView(view, is_list) {
answer = 'your_view_name';
})(view, is_list);
If this code works for you to show your_view_name, then add the condition to it.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-19-2018
12:53 AM
- last edited on
‎07-17-2024
08:42 PM
by
ServiceNow
Hi sindhu21,
You created one view with name "ViewA" this view should be visible for RoleA users only, and similarly for ViewB also.
So you have to write View Rule script. Try this code.
if (!gs.hasRole('RoleB'))
{
if (gs.hasRole('RoleA'))
{
answer = "ViewA";
}
}
Let me know, if you have any questions.
I hope it work, please mark it Correct or Helpful based on Impact....!!!!
Warm Regards,
Priyanka