How to check view name using client script?

dianemiro
Kilo Sage

Hi,

I am trying to make a client script with the condition View == self_service and if true I want to set the state to Active. Here's my code but it is not working. Do you have any other way to meet what I am trying to do?

function onLoad() {

    //Type appropriate comment here, and begin script below

    var view = getView();

  if (view == 'self_service'){

  g_form.setValue('state','2');

  }

}

1 ACCEPTED SOLUTION

BALAJI40
Mega Sage
  1. function onLoad() {  
  2.     //Type appropriate comment here, and begin script below  
  3.     var view = getView();  
  4.   if (view == 'ess'){   //self service view name is ess so make to ess
  5.   g_form.setValue('state','2');  
  6.   }  
  7. }  

try this script.


View solution in original post

11 REPLIES 11

snehabinani26
Tera Guru

View name is not self_service rather its ess.


If you are not aware of the view name please print the view name and validate the same. For eg :- alert(view); This will give you the correct value.


parvinder2
Tera Expert

Hi Diane,


var strView = g_form.getParameter('view');


alert(strView);



Please try with this code it will help for you.



Regards,


Parvinder


amlanpal
Kilo Sage

Hi Diane,



First of all getView() is a Server side script which you can not use in the Client Script. You need to write the script like below. The script is untested but hope this will work. Also, please remember if you are dealing with 'Default' view, you must remember that the variable strView will return null.


For details, you might refer section 4.8.11 here: http://wiki.servicenow.com/index.php?title=Client_Scripts



function onLoad() {


  var objView = document.getElementById('sysparm_view');


  var strView = objView.getAttribute('value');


  if (strView == 'ess') {


      g_form.setValue('state','2');  


  }


}



I hope this helps.Please mark correct/helpful based on impact



Community Alums
Not applicable

Hi amlanpal,

what ever you post it's correct. i have one doubt for best practices find_real_file.png

how to write this with out DOM 

Gurpreet07
Mega Sage

Below function will provide you with form view in client scripts


g_form.getViewName()