- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 03-11-2021 10:38 PM
Hi All,
I had hard time recently to find a solution to redirect to URL when user clicks on a Server Side UI Action on Service Portal in scoped application. Basically what i was trying to achieve is to create a UI action which copies the existing record and then navigates to newly created record and all that on Service Portal.
I noticed there are several challenges here:
1- UI Action can only be Server Side if you have to display it on Service Portal which already limits your options for url re-direction.
2- action.setRedirectURL() or gs.setRedirect() works in Server Side UI Actions but is not supported on Service Portal
3- setPreference on Server Side does not work on Portal either, you need to use savePreference instead.
4- UI Script does not have access to getPreference method.
Here is the solution which I developed to overcome the challenges and get the redirection as described in title.
UI Action
Client: False (as its server side UI action which is visible on Service Portal)
Script: After all your code for functionality you would like to develop just add two lines at the bottom as below in the script.
var url="<your service portal URL where you would like to redirect>";
gs.getUser().savePreference("sfRedirectPortal",url);
the above code sets your url in user preference "sfRedirectPortal" which is later retrieved to redirect.
UI Script
Create a ui script as follows:
UI Type: All
Script:
redirectToUrl();
function redirectToUrl() {
grUserPreference = new GlideRecord('sys_user_preference');
grUserPreference.addQuery('user', top.window.NOW.user_id); // picking up current user as g_user doesnt work here.
grUserPreference.addQuery('name', 'sfRedirectPortal');
grUserPreference.query(response);
function response(result) {
var url = '';
var sflastRedirect = '';
if (result.next()) {
url = result.value;
sflastRedirect = localStorage.getItem("sflastRedirect");
if (url != "" && top.window.location.toString().indexOf(url) == -1 && (grUserPreference.sys_updated_on > sflastRedirect || sflastRedirect == null)) {
localStorage.setItem("sflastRedirect", grUserPreference.sys_updated_on);
top.window.location = url;
}
}
}
}
above code retrieves user preference using glide record (note that getPreference does not work in UI Script/Service Portal) and then is used to redirect.
Basically in this solution this UI Script is executed each time Service Portal form is reloaded or a UI action is clicked.
Please try this solution and share your feedback.
Cheers
Syed Faheem.
Principal Technical Consultant
ServiceNow.
- 16,815 Views
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
This is awesome we were struggling to solve the redirect and are using your script now 🙂
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Wow, thanks for this solution.
Still can't believe we have to do such things for such a basic and common need on a platform claiming to be "Low-Code" ...
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hey, if I want to delete the record with this UI Action the Service Portal form isn't reloaded. Does somebody know a workaround for this problem?
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi
thank you very much for this work!
I have a behavior i am wondering about:
After the redirect while openeing a record from the same table again, the user gets reffered a second time to the first redirected page. After the second refferal, i can use the portal as I like w/o refferals... do you have a idea, where this comes from?
How can i empty "sfRedirectPortal" after the redirect?
here`s my code from my ui-action (UI-Script is as yours):
doInsertAndStay();
function doInsertAndStay() {
var saveMe = current;
if (typeof current.number != 'undefined' && current.number)
current.number = ""; // generate a new number
else if (typeof current.u_number != 'undefined' && current.u_number)
current.u_number = ""; // generate a new number
var temp = current.insert();
var temp_table = current.getTableName();
action.setRedirectURL(saveMe);
var url = '?id=form&table=' + temp_table + '&sys_id=' + temp + '&view=sp';
gs.getUser().savePreference("sfRedirectPortal",url);
}
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi Stepehan
Its basically the last line mentioned below which sets the value of sfRedirectPortal. Please debug on value of sfRedirectPortal as if it is getting updated with the new URL?
Syed.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi @syed_faheem ,
While opening the other record from the same table, it will fetch the already existing data of the previous record. Can you please let us know how to debug the code to behave normally when we open the record for the second time.
Thanks
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
You can debug by monitoring values in system preference i.e. "sfRedirectPortal" and "sfLastRedirect".
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
I discovered buggy behavior w/ savePreference(). It creates the sys_user_preference record, deletes it, then re-creates the sys_user_preference record. This results in a discrepancy between sys_updated on and sflastRedirect, which causes the copied record to be opened in a new tab once more whenever the user opens another form in the portal.
This behavior does not occur if you insert the system property starting with GlideRecord('sys_user_preference').
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi Syed_faheem I am also trying the same but it not redirecting the value .
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi @Patrick71, I have tried this method, some users are able to see the redirection but I as an admin, am unable to see that redirection. And I cleared the cache, browser's cache also, it remains the same.
Could you please explain why that behavior is happening for some users?
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi @syed_faheem ,
Thanks for sharing the great solution. two comments/improvements
- If you want the url to be opened in a new tab, need to change the top.window.location = url; in the UI script to top.window.open(url);
- to allow an external user (for example wm_ext_manager in the contractor portal) to run this action, need to add this role to the read ACL of the sys_user_preference table
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
This code is not working for me. I have literally copied both, UI Script and UI Action, and when I click the link of the UI Action it does not redirect me to the desired page. What can be happening?