Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to get view name from the portal using client script ?

Virendra K
Kilo Sage

Hi All,

 

I am opening Preapproved change request from the portal. I have written the Onchange client script on 'planned start date' field where I want capture the name of the view name from below url which should only work when Preapproved change request is opened. There are few suggestions to use "var view = getView()" but I can see the java browser error saying "its not defined"

 

How I could get the view name from below url or put such a constraint in client script so that defined code only should work when Preapproved change request is selected ?

 

Portal url :

https://demotest.service-now.com/change?id=preapproved_form&table=change_request&sys_id=-1&view=sip_preapproved&spa=1  

 

Thanks,

Virendra

4 REPLIES 4

YogB
Tera Expert

Hi I believe the following code should help. You can write this code on onLoad client script.

 

var gUrl = new GlideURL();
gUrl.setFromCurrent();
var value = gUrl.getParam("view");
alert(value);

 

Mark as helpful if this solves your issue

 

 

Thank for the reply @YogB

I tried but getting below error.

 

VirendraK_0-1726673717930.png

 

Virendra K
Kilo Sage

I have found the answer for this(link is given below).

https://servicenowguru.com/client-scripts-scripting/parse-url-parameters-client-script/

 

We can use fetch the parameter from the url using below code. 

 

//call /place below 2 lines in client script

var myparm = getParmVal('view');  // write any parameter name. example like sysparm_view , sys_id etc
g_form.addInfoMessage('view name '+ myparm );
 
//call /place below function within client script ( This is for portal fetch) 
function getParmVal(name) {  
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");  
    var regexS = "[\\?&]" + name + "=([^&#]*)";  
    var regex = new RegExp(regexS);  
    var results = regex.exec(top.location);  
    if (results == null) {  
        return "";  
    } else {  
        return unescape(results[1]);  
    }  
}
 

This is one of the ways of doin it by manually parsing the string. Try this once I think this will work on portal

 
var url = top.location.href;
    var value = new URLSearchParams(url).get("view");
 
So the glideUrl doesn't works on portal but this will.
 
Mark as helpful if this enhances your solution