Redirect to a URL from Script (from Arguments)

mkm1
Mega Guru

I have requirement when a user click on a module, user should be directed to a URL based on his/her role. I am using a link type Script (from Argument) for a module to run a script to dynamically generate a URL based on user's role.

How do I redirect to the URL from the script automatically? 

1 ACCEPTED SOLUTION

shloke04
Kilo Patron

Hi @mkm 

Please go through the below post where similar query has been answered.

https://community.servicenow.com/community?id=community_question&sys_id=1c0c4725db9cdbc01dcaf3231f9619fc

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

View solution in original post

5 REPLIES 5

-O-
Kilo Patron
Kilo Patron

It seems you can place a piece of JavaScript into the Arguments field if it starts with the keyword javascript:. What follows would be a client side JavaScript.

mkm1
Mega Guru

Thanks Janos. I have used script and created the function to build the URL. I am calling this function using Script (from Arguments), but challenge is how to automatically redirect to the generated URL to open a page.

 

I also tried creating a UI Page, tried calling the same function using <g:evaluate which is returning correct URL, but not I want to redirect to URL to open the page. Is there any way to pass the variable to processor script?

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<g:evaluate var="jvar_error_message" object="true" jelly="true">

var v_url=getRoleHomepage();
gs.addInfoMessage(v_url);

</g:evaluate>
</j:jelly>

 

-O-
Kilo Patron
Kilo Patron

I'm sorry - I just now realize - I have answered beside the point: was thinking of "URL (from Arguments:)" not "Script (from Arguments:)".

Modules of type "Script (from Arguments:)" basically execute the script as if executed in Scripts - Background. And I have no idea how one could redirect the output of Scripts - Background. So I don't think you should be using that. Those seem to have been created to execute something on server side when clicked and that's it.

As for redirecting, that is a function of a HTTP server. It happens in a layer encompassing UI Pages, or any pages in fact. That layer is not easily and for sure not everywhere accessible, it can be done in Scripted REST API for instance. Or in Processors, but that is no longer officially supported.

@Tim Woodruff wrote up an article on how to create a re-directing REST endpoint. I recommend to go that way. You would define the logic of redirecting based on role in that REST end-point. Then you would "navigate" to that REST end-point both in the Module and in the UI Page (or from anywhere else). That means that you would be changing the Module from type Script (from Arguments:) to simple URL (from Arguments:). The value of field Arguments would be the path to the REST API you have defined. In Client Script, like in URLs you would use g_navigation.open(). Thus when someone clicks on the module or the g_navigation API is executed, the REST API end-point would execute and would redirect as you defined it.

mkm1
Mega Guru

Thanks, Janos.

I was able accomplish it within the UI Page. Using the variable, value that was set within <g2: evaluate >, in the client script in the UI Page to redirect using document.location.href .