The CreatorCon Call for Content is officially open! Get started here.

Redirect in Business Rule (scoped vs not)

Wade Clairmont
Tera Guru

I am working in a scoped app, not sure if that makes any difference.

Trying to use a display BR to check values on load, if they exist then return the user to the previous form with an error message.

However, following all docs and posts out there using gs.setRedirect(url)  doesn't seem to work.

current code :

       var url = <table_name> + '.do?sys_id=' + <previous sys_id>;                 
       gs.setRedirect(url);
 
If I hardcode the entire url, no success.  If I display the url that is built, copy it into another tab, it works. 
Any thoughts?  Thanks in advance.
4 REPLIES 4

Maddysunil
Kilo Sage

@Wade Clairmont 

I think Instead of directly concatenating the URL, use the GlideURL object to construct the URL. This ensures that the URL is properly formatted and respects scoped app constraints.

 

var url = new GlideURL('<table_name>.do');
url.addParam('sys_id', '<previous sys_id>');
gs.addInfoMessage('Redirecting...');
action.setRedirectURL(url);

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

I am receiving the following Error Message now
GlideURL is not allowed in scoped applications

@Wade Clairmont 

If GlideURL is not allowed in scoped applications, you can try using URI instead to construct URLs.

 

// Construct the URI object with the base URL
var uri = new URI('<table_name>.do');

// Add the sys_id parameter to the URI
uri.addQuery('sys_id', '<previous_sys_id>');

// Display an info message
gs.addInfoMessage('Redirecting...');

// Set the redirect URL using action.setRedirectURL()
action.setRedirectURL(uri.toString());

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

Amit Gujarathi
Giga Sage
Giga Sage

HI @Wade Clairmont ,
I trust you are doing great.
In a scoped ServiceNow application, you can't directly use GlideURL to construct URLs. Instead, you can use the URI object. Here's how you can modify your code:

// Construct the URI object with the base URL
var uri = new URI('<table_name>.do');

// Add the sys_id parameter to the URI
uri.addQuery('sys_id', '<previous_sys_id>');

// Display an info message
gs.addInfoMessage('Redirecting...');

// Set the redirect URL using action.setRedirectURL()
action.setRedirectURL(uri.toString());

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi