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.

Redirecting to external URL from script include

chrisn_
Mega Guru

Hello everyone,

I have created a scoped application that feeds into a third party application via an API Rest Call. After the initial call is complete the requirements I am working with state they want the user to be redirected to an outside URL that will end with a unique value they will send me in the initial API calls response. I am getting this value and entering it on the record so it is usable. The problem I am having is that the redirect straight from the record producer is too fast. I can't seem to delay it for 3 seconds so that the third party can return the value I need so the link in the redirect will work. I am now trying a script include that looks like this:

var MyGlobalScopeUtils = Class.create();
MyGlobalScopeUtils.prototype = {
initialize: function() {
},
sleep: function (duration) {
gs.sleep(duration);
},

type: 'MyGlobalScopeUtils'
};

 

In my record producer I have this script:

var globalUtils = new global.MyGlobalScopeUtils();
globalUtils.sleep(5000);

producer.redirect="https://service/exams/${current.x_unoci_rapid_retu_cover_sheet_link}";

Since I have put the first two lines in the redirect no longer works. Is there something I can do to fix this from the record producer or is there a way I can handle this redirect from the script include?

6 REPLIES 6

Stephen Sturde
Tera Guru

Try this in your script include:

var url =YOUR_URL;

this.response.sendRedirect(url);

-Stephen

Hi Stephan,

So something like this? 

var MyGlobalScopeUtils = Class.create();


MyGlobalScopeUtils.prototype = {


initialize: function() {


},


sleep: function (duration) {
gs.sleep(duration);
var url = 'https://service/rapidReturn/exams/${current.x_unoci_rapid_retu_cover_sheet_link}';
this.response.sendRedirect(url);

},

type: 'MyGlobalScopeUtils'


};

 

Upon further review I am getting the following error when trying to call my script include

 

org.mozilla.javascript.EcmaError: "MyGlobalScopeUtils" is not defined.
Caused by error in <refname> at line 1

==> 1: var initialize = new MyGlobalScopeUtils();
2: initialize.sleep(5000);

Hmm... Interesting. It may be additional utils are needed. Unfortunately there isn't much in the way of documentation for this function. Here's the header to a script I use the "response.sendRedirect()" command in:

gs.include('PrototypeServer');
gs.include('AbstractTransaction');

var MyScriptInclude = Class.create();

MyScriptInclude.prototype = Object.extendsObject(AbstractTransaction, {

 

-Stephen