Sunday, Sep 05, 2010
Login

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…

switchView(type,table,view);

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(!isLoading){
      //If the new value isn't blank
      if(newValue != ''){
         //Change to the 'ess' view if the Category is 'Hardware'
         if(newValue == 'hardware');{
            switchView('section','incident','ess');
         }
      }
   }
}

 

Recent Comments

  • Scott Stechmesser: Awesome script to use. Works great. How would you modify it to be able to copy a Catalog UI Policy?
  • Tulio: Perfect!!! Thanks for this.
  • Ron Methias: Another reason why I have stopped going to the official SN documentation sites and make the GURU my...
  • Richard Huss: Ingenious – and somewhat simpler than the way the Incident Resolution best practice plugin does...
  • valor: Joe, that’s one of the reasons why I’m parsing out the URL instead of trying to get the page...