Redirect pages (e.g. links, bookmarks) to an other... - ServiceNow Community
Mark Roethof
Tera Patron
Tera Patron

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

 

Hi there,

 

Switching over to a different Service Portals, for example because you are introducing a new Service Portal or because you are going to use Employee Center? One task which comes along when switching over between Service Portals is fixing links, users having to change bookmarks, etcetera. No matter how good your investigation was, after go-live still several broken links will occur. Or maybe even the links will still work, on a Service Portal which you are not supporting/maintaining anymore, though which is technically still active (there's no active flag for Service Portals).

 

In a previous article I shared how to redirect from e.g. /sp to /esc, in this article I'll share a similar approach for redirecting pages from e.g. /sp?id=sc_category to /esc?id=esc_sc_category.


Redirect

In the previous article, an approach was used with JS Includes. For redirecting pages we can use the same approach, only with small differences in code. You might think, "hé, don't we have out-of-the-box page route maps?" Yes that's correct, though… those are for within the same Service Portal. We are actually after switching Service Portals and/or switching pages.

 

One way could be to use the redirect which I shared in the article I mentioned AND applying page route maps. Another way could be to do this all within one place: JS Include. That's what I'll be sharing below.

 
JS Include

We can add JS Includes on a Theme record that is associated with a specific Service Portal. JS Includes will be loaded on attempt to display every URL. Within the JS Includes record, you can create a UI Script which makes it possible to influence the URL using the JavaScript object window.location.


Service Portals > Theme

Open the Theme associated with the Service Portal concerned.

 

find_real_file.png


Theme > JS Includes

Add a new JS Includes record on the Related List at the bottom of the Theme record.

 

find_real_file.png


JS Include > UI Script

Add a new UI Script record on the JS Include record.

 

find_real_file.png


UI Script

The previous three steps were just a copy-paste from my previous article. The scripting part from the UI Script will be different! The scripting might be done several ways, I'll just share a possible way.

 

What we are after achieving, is e.g.:

 

From To
/sp /esc
/sp?id=sc_category /esc?id=esc_sc_category
/sp?id=sc_cat_item&sys_id=
934c7b813ba10010d901655593efc4a6
/esc?id=sc_cat_item&sys_id=
934c7b813ba10010d901655593efc4a6
/sp?id=kb_view2 /esc?id=esc_knowledge_home

 

I already mentioned that we are going to use the JavaScript object window.location. Some good basic information on this object, can be found on W3Schools.

 

Using what is mentioned on W3Schools, I came up with a small script to replace the Service Portal suffix and to replace specific page names like kb_view2 and sc_category. Parameters like sys_id will stay untouched.

 

(function () {

    var url = window.location.href;

    url = url.replace('/sp', '/esc');

    url = url.replace('?id=kb_view2', '?id=esc_knowledge_home');
    url = url.replace('?id=sc_category', '?id=esc_sc_category');

    window.location.assign(url);

})();

 

find_real_file.png


Result

When a user opens the /sp Service Portal, the Knowledge Base or a specific Catalog Item, almost instantly the user is redirected to the Employee Center (/esc) and if applicable the correct Employee Center page.

---


And that's it. Hope you like it. If any questions or remarks, let me know!

 

C

If this content helped you, I would appreciate it if you hit bookmark or mark it as helpful.

 

Interested in more Articles, Blogs, Videos, Podcasts, Share projects I shared/participated in?
- Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

 

Kind regards,


Mark Roethof

ServiceNow Technical Platform Architect @ Quint Technology

2x ServiceNow Developer MVP

2x ServiceNow Community MVP

---

LinkedIn

Comments
Rob Sestito
Mega Sage

Hey @Mark Roethof - 

Great article here!

You had replied to a question post I have Link IT page to the HR Page here.

I see what this article is talking about, and I am looking to see if I can do it in reverse...?

We currently are using sp for the main portal page. We have a widget our users can click on to then go over to HR on ESC.

I am in the process of migrating HR over to EC which we were told we could do with IT as they are not ready for that.

What I am looking to do here, is have a link to the IT page from ECS?id=ec_pro_dashboard back to IT on URL Suffix sp.

Is this obtainable? And If so, are you able to assist?

For what I created/tried thus far - you could find back on my question post.

Greatly Appreciated -

-Rob

Pallavi K1
Kilo Guru

hi @Mark Roethof ,

 

I tried to implement this solution like below as we wanted to modify a parameter in the URL which we have given incorrectly in many scripts:

(function () {

var url = window.location.href;
url = url.replace('/esc?id=kb_article&sys_id=' , '/esc?id=kb_article&sysparm_article=');

window.location.assign(url);

})();

 

But the instance is not loading when I add this to the JS includes of the theme. Wil the above script work? Am i missing something? Please let me know if there is any another way to replace parameters in a URL.

 

Thanks,

Pallavi K.

GoBucks
Kilo Sage

@Pallavi K1did you get this to work?  For me, the browser just continued to appear to loop. Like it was starting to try to resolve the URL and then it tries again before the page even loads.  An infinite loop moving so quickly that the browser stays busy trying to resolve a URL but nothing will ever load.

Pallavi K1
Kilo Guru

@GoBucks   In our case, we are able to achieve this by adding a condition before the script executes as we don't need the script to run every time the portal pages load. 

 

(function () {
if (window.location.toString().indexOf('/esc?id=kb_article&sys_id=XXX') > 0)
{
var url = window.location.href;
url = url.replace('/esc?id=kb_article&sys_id=','/esc?id=kb_article&sysparm_article=');
window.location.assign(url);
}

})();

Hervi
Tera Contributor

WHat if there is no theme to the portal? what to do in that case?

Ravi Chandra_K
Kilo Patron

@GoBucks same happened with me. the page keeps on loading

nmore01
Tera Explorer

@Ravi Chandra_Ktry adding the if condition as suggested by @Pallavi K1 

(function() {
if (window.location.toString().indexOf('/isselfservice') > 0){
    var url = window.location.href;
    url = url.replace('/isselfservice', '/it_self_service');
    window.location.assign(url);
}
 
})();
 
@Mark Roethof Thanks for sharing the knowledge!
JeffPatt
Tera Expert

Implementing as-is had me experiencing the infinite loop mentioned in a few other comments. Slight tweak to the script got it working nicely for me. Might help others so here's my edit:

 

(function() {
    // Get the url
    var url = window.location.href;

    // Check the user is on a service portal link
    if (url.indexOf('now.com/sp') != -1) {
        // If so, play swapsies.
        url = url.replace('/sp', '/esc');
        url = url.replace('?id=kb_view2', '?id=esc_knowledge_home');
        url = url.replace('?id=sc_category', '?id=esc_sc_category');
        // Send them away!
        window.location.assign(url);
    }
})();

 

pruthvirajreddy
ServiceNow Employee
ServiceNow Employee

There is a issue with an ITIL user is facing an issue while checking his ticket status over email. when ever he is clicking on the hyperlink which any user receives over email, it is redirecting him to the backend page(Platform) of the instance as part of his initial click. Once he tries to make a second click it is then routing him to the actual page. He is the only user who is facing this issue. What are the check need to be done to resolve this issue for that particular user?

Version history
Last update:
‎08-17-2024 09:55 PM
Updated by:
Contributors