Change Form View Client Script

Service-now allows administrators a lot of flexibility in defining which elements appear on a particular form or list. The set of fields and related lists that appear are collectively defined as a View. One common configuration task is to somehow limit access to a particular view based on a user role or some information on the record being viewed. Instructions for working in some of these scenarios can be found here…

View Rules
Restricting Form Views by Role

Neither of these methods work if you need to change the view of a form from a client script or a UI action. In order to do that, you can call the ‘switchView’ function as follows…


The table and view parameters should be self explanatory. The ‘type’ parameter is either ‘list’ (to redirect to a list view) or ‘section’ (to change to a form view). Here’s an example client script that shows how you could use ‘switchView’ to change the incident form view to ‘ess’ if the ‘Category’ field changed to a value of ‘hardware’.

One thing to keep in mind when using this function is that the form HAS to reload in order to change the form view. This means that the system will attempt to save the record before moving from one view to another.
function onChange(control, oldValue, newValue, isLoading) {
   //If the page isn't loading
      //If the new value isn't blank
      if(newValue != ''){
         //Change to the 'ess' view if the Category is 'Hardware'
         if(newValue == 'hardware'){

Date Posted:

July 23, 2010

Share This:


  1. Priya J January 3, 2011 at 5:20 pm

    Hi Mark,

    I want to change the form view based on the role of user, severity and particular assignment group entered on incident form.

    I can not use view rule, as there is no scripting space in view rule to check desired role of user. I want similar functionality as “view rule” where on change of severity, group and based on user role form view should be changed.

    I tried to use switchview() method on submit of incidnet form for form section of incident table but observed that it changes the list view and not the existing form view. For all the existing incident records which matches to the conditions I’m not able to see the changed view.

    Also, not able to use business rule – incident functions, this BR changes the view for the first time but on change of severity and assignment group it doesn’t change to desired view. Can you please suggest how to change form view based on user role with changes in incident fields?



    • Mark Stanger January 3, 2011 at 11:39 pm

      It sounds like the function is working then, but you’re triggering it at the wrong time. Based on what you’ve explained, It sounds like you should be triggering the script in an ‘onChange’ or ‘onLoad’ client script.

      • Priya January 4, 2011 at 2:42 am

        Hi Mark,

        Thanks for your quick response. I have tried to use switchView in an onLoad script. I have created views in global domain and scripts are domain specific. For onLoad, switchView loads the page again and goes into infinite loop and it changes the list view but not the form view. View change should be only to specific incident record for which condition matches and not for all. In view rule, it changes the view only if condition matches for form section and not for the list. Here is the code snippet for onLoad script –

        var ignoreNextRequest = false;
        function onLoad() {
                var severity = gel('incident.severity').value;
                var assignment_group = g_form.getReference('assignment_group').name;
                var isRole = g_user.hasRoleExactly('service_desk');
                if (!ignoreNextRequest) {
                       ignoreNextRequest = true;
                	if (isRole && severity == 4) {
        		else if(!isRole && severity == 4 && assignment_group == 'XYZ') {
        		else {
               ignoreNextRequest = false;

        Please let me know if I need to handle this by any other way.



        • Mark Stanger January 4, 2011 at 3:53 am

          I think you’ll need to check for the view as part of your conditions to prevent the endless loop. Something like this should allow you to detect the view and only switch the view if it needs switching.

          function onLoad() { 
             //Get the view name 
             var strView = document.getElementById('sysparm_view').value; 
             //Switch the view if the current view is 'ess' and the user has 'itil' role 
             if(strView == 'ess' && g_user.hasRole('itil')){ 
          • Priya January 5, 2011 at 12:40 am

            Thanks Mark, now I can change the form view based on condition on load of form but it changes the list view as well. I need to change only the form section view for particular incidents where conditions matches. I am passing ‘section’ as parameter to method but it still changes the list view. Can we restrict view change to the form only?

          • Mark Stanger January 5, 2011 at 3:41 am

            The list view on a redirect will always be the same as the form view you’re coming from. You’ll need to set that view back to the original view in an onSubmit client script (using the ‘switchView’ function) or a business rule (using ‘gs.setRedirect()’).

  2. Priya January 6, 2011 at 12:30 am

    switchView method saves the data before changing the view therefore when onLoad script tries to switch view, it calls the onSubmit script where I need to reset the list view to default and that doesn’t change the form view to desired one and changes it to default one. Also, with the business rule tried to create it after update on incident table to redirect it to default list view by passing sysparam_view as blank – gs.setRedirect(‘ ‘); this doesn’t change the list view.

    • Mark Stanger January 6, 2011 at 12:41 am

      This seems to work just fine for me in an ‘after’ business rule. Try it out on the Service-now demo site to confirm.

  3. Priya January 6, 2011 at 9:58 pm

    Business rule works only if there is no onLoad script to change the form view. I have onLoad script which changes form view, when I update that record it redirects it to list view but view is not getting set to default. I have tried this on demo as well. Please see client script – force default view and BR – Force default view. In client script I’m setting form view to ess and in business rule I want to set list view to default. Once the list view gets set by switchView method business rule is not able to reset it. Please let me know if there is any other way to set the list view to default view.

    • Mark Stanger January 7, 2011 at 12:22 am

      I’m not sure what else to tell you. The only other thing I can think of is that your system has this property set under ‘System Properties->System UI’. I noticed that was the case in demo this morning even though I couldn’t find your scripts.

      It looks like this property… ‘When a user changes the view used for a list or for a record, continue to use that view for future displays.’…needs to be set to ‘false’ for this to work.

      If this doesn’t work, then you may need to ask on the Service-now forums. I’ve been able to get this to work several times without issue and I don’t have any other suggestions to give.

  4. Dave February 25, 2011 at 4:32 am

    I’ve been working with this today, couldn’t understand why ITIL user wasn’t switching….

    Until I checked the various roles in the system, added ‘view_changer’ to my ITIL user and now everything seems peachy…

  5. John Gilaspy April 29, 2011 at 6:41 am

    I have a requirement from a business unit that a form not be visible to certain personnel, unless actually assigned to them. Any way to easily script a rule like this?

    • Mark Stanger April 29, 2011 at 6:45 am

      You could script it using something like this, but it would probably be pretty clunky. What I would probably do is leave the form alone and set up ACLs so that users couldn’t see the data in the record. It’s probably not the form that people are concerned about, but the information displayed on it.

      • Jerry May 20, 2011 at 8:50 am

        Hi Mark,

        I can not change the view for an ESS user.

        Seems that the system prevents this and allows only default or ess view for an self-service user.

        Even when they are coming from different companies and we have domain separation by company.

        • Mark Stanger May 20, 2011 at 9:11 am

          The behavior you are seeing is caused by the ‘incident functions’ business rule. It forces users with no role to always see the ‘ess’ view for incident records. What you’ll want to do is modify the script in that business rule so that it allows users to see the ‘ess’ view or your new view. Here is a wiki article explaining the details.

          You can either make sure your new view name starts with ‘ess’ or you can modify this portion of the script to allow for both views…

          if (view.startsWith("ess")) 
  6. Bill Collins May 9, 2013 at 12:33 am

    (With a UI Action form button) I’ve tried the following and other variations to switch the view for a project record. The goal to to give the Project User the ability to toggle back and forth between the “project_report” view and the “default” view. It is not yet working.

    //gs.setRedirectURL("" + current.sys_id + "&sysparm_view=project_report");

    action.setRedirectURL("" + current.sys_id + "&sysparm_view=project_report");

  7. Akshat Chawla June 11, 2013 at 11:20 pm

    Hi Mark,

    I have the requirement to hide the filter on the basis of view for e.g I want to hide filter for every user on the ess view.
    I wrote a script include i.e:
    function incidentDisplayFilter() {
    if (view.startsWith(“ess”))
    answer = false;
    answer = true;
    return answer;

    But my problem is that it’s working fine only for incident if I apply this for any other table it hides the filter for every view.


  8. Peter Foreman August 5, 2014 at 9:01 pm

    Is there a way to force the list view like there is for forms by using the View Rules module?
    Thanks Peter

    • Mark Stanger August 5, 2014 at 10:09 pm

      Not with view rules. View rules are based on the conditions of a specific record, which you can’t determine on a mixed result set in a list. You can force a view in a list view only by using the ‘sysparm_view=’ parameter in the URL that points to that list.

      • Peter Foreman August 6, 2014 at 5:05 pm

        Thank you for the reply. The list view can be specipied from the navigation panel modules, the Homepage filters but the URL is left open to manipulation.
        Does it mean changing the Script Include: ViewManager, _listSelected function if you want to force a change to a list view ?
        Say for a particular type of non-roled user, similar to what sys_script_ajax does for HomeScripts, homeGetViewName
        Thanks Peter

        • Mark Stanger August 6, 2014 at 5:23 pm

          Role-based view rules for entire tables can be accomplished for lists in the system. An example of this is forcing non-roled end users to the ‘ess’ view of the incident table. You can check out the ‘incident functions’ global business rule to see how this is done and apply it to other tables as needed.

  9. Scott October 29, 2015 at 9:03 pm

    Not having script in View Rules is a major oversight in the product, in fact the whole view management is poor IMHO.
    It works well as long as you want to have one view for every end user and one for ITIL users and nothing else – everything else is a clunky kludge.

  10. John December 12, 2016 at 2:48 pm

    Hi Mark,

    Is there anyway to force a view on to a record in a related list? For example, if I’m in a view called ‘test’ for a company record and click on a company a related list, i don’t want to see the ‘test’ view, I want to see ‘default’ view.


Comments are closed.




Fresh Content
Direct to Your Inbox

Just add your email and hit subscribe to stay informed.