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.

syed_faheem
ServiceNow Employee
ServiceNow Employee

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.

 

 
Comments
Ellie2
Kilo Expert

This is awesome we were struggling to solve the redirect and are using your script now 🙂

Fred Jean
Tera Expert

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" ...

PaKe
Kilo Sage

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?

Stephan S_
Tera Expert

Hi @syed.faheem ,

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);
      
}

syed_faheem
ServiceNow Employee
ServiceNow Employee

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.

Jatoth Vijay Ku
Tera Contributor

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 

 

syed_faheem
ServiceNow Employee
ServiceNow Employee

You can debug by monitoring values in system preference i.e. "sfRedirectPortal" and "sfLastRedirect".

Patrick71
Tera Contributor

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').

Rithik1
Tera Explorer

Hi Syed_faheem I am also trying the same but it not redirecting the value .

 

_Omkar_sai_
Tera Contributor

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?

Jonathan Zur
Tera Contributor

Hi @syed_faheem  ,

 

Thanks for sharing the great solution. two comments/improvements

 

  1. 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);
  2. 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

 

 

 

jordimsant
Tera Guru

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?

Version history
Last update:
‎03-11-2021 10:38 PM
Updated by: