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.

Redirect URL in Widget Client Script without hard-coding the instance URL

Richard S Lars1
Kilo Contributor

I want to redirect the user in a Service Portal Widget to either:

1. The previous page in the Service Portal

2. Or the category page

after adding a sc item to the cart. I've been able to do it by hard-coding the instance name in the client script, but that means we would need to have 3 separate lines of code (one per dev, test and prod instance)

$window.location.href = "https://[instancename]/sp?id=sc_home"; // Redirect to Category page

How can I write this so that it takes the current instance url instead of hard-coding the url?

I have tried by doing the following but the user doesn't get redirected:

var url = gs.getProperty('glide.servlet.uri')
$window.location.href = url + "/sp?id=sc_home"; // Redirect to Category page

I have also tried to redirect to the previous page but I didn't manage to, therefor directing the user to the Category page instead. 

1 ACCEPTED SOLUTION

rad2
Mega Sage

Hello,

I don't think we can use gs.getPropery in client script of a widget.

I have tried the following in the client script of the widget and it seems to work (redirect to the catalog home page)

var hosturl = 'https://' + $window.location.host;
$window.location.href = hosturl + "/sp?id=sc_home"; 
		

 

Hope this helps!

 

View solution in original post

4 REPLIES 4

Mark Roethof
Tera Patron
Tera Patron

Hi there,

If I do a gs.info, it gives me:

https://dev*****.service-now.com/

Looking at your code, you are adding:

/sp?id=sc_home

So a double slash I guess? Maybe that's already the issue.

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

---

LinkedIn

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Apparently gs.getPropery doesn't work in a SP widget but 

'https://' + $window.location.host;

worked as rad suggested in this post

rad2
Mega Sage

Hello,

I don't think we can use gs.getPropery in client script of a widget.

I have tried the following in the client script of the widget and it seems to work (redirect to the catalog home page)

var hosturl = 'https://' + $window.location.host;
$window.location.href = hosturl + "/sp?id=sc_home"; 
		

 

Hope this helps!

 

This did help! Thanks a bunch!