Robin John
Kilo Sage

Using Service Portal it is easy to use the window $window object to easily redirect the user to any url. Although, it is not so straight forward when using UI builder. To achieve this, you can always hack the global window object via some currying (I will write that in another article). But it is not the recommended way. The recommended way is to dispatch an event with the uri in the payload and then use the OOB Link to destination event handler to redirect the user. Follow the step by step instruction to achieve this.

  1. Create a page script. Here you can do the logic to determine the uri the user needs to be redirected to.
    find_real_file.png

  2. There is a .emit() method in the api class. This method takes two arguments: the name of the event and the payload. The payload is a JavaScript Object literal. This can be declared in the script as such:

    function handler({api, event, helpers, imports}) {
        const uri = 'https://www.servicenow.com/';
        api.emit('CUSTOM_REDIRECT_URL', {url: uri});
    }
  3. Now this event has to be handled on the page. This will tell the application what to do when this event is fired. To do this go to the Body of your page layout and navigate to events:
    find_real_file.png
    Then let us add a new handled event on the page. For this look for the handled events section on the events section and then click on add
    find_real_file.png
    After clicking on 'Add' enter the event label as the name of the event that you emitted in step 2 (of your page script). Then click on add.
    find_real_file.png
    Save the page. 
    find_real_file.png

    After saving you will see this event in the Page Event mapping section.
    find_real_file.png
    Click on 'Add a new event handler' and a popup should appear. In the popup search for 'Link to destination' event handler.
    find_real_file.png
    In the popup click on 'Select destination' button and that will let you select further options. 
    find_real_file.png
    There click on External URL and in the chosen route enter @payload.url as the key of the object in the emit() argument is 'url'. This will dynamically bind the payload that was emitted with the event in the script. Press OK when completed.
    find_real_file.png
  4. Now the script that was created in step 2 can be bound to an event on a component in the page. For eg. you can bind it to a click event. In my example, I added a button to the page and then on the button clicked event added the script as event handler.
    find_real_file.png

    In the event handler you should be able to find your script in the client script
    find_real_file.png
  5. Once you have bound the events with the event handler(s). You are good to go.
Comments
Velma
Tera Guru

Thank you. Can you explain the difference between this and just using Link to Destination with an external link that is stored in a Client State Parameter? I tried your steps and don't see the difference in behavior.  What is the use case? Would it be easier to use onLoad (a conventional use for redirect) maybe? (I've been trying to get Link to Destination to open in the same browser tab with an external link--I tried this just in case, but it opened in a different tab just like the button did before.)

Robin John
Kilo Sage

Hello Velma,

Thanks for pointing this out. I will explain the use case:

We have a custom table that is populated periodically with records via an API call to an external resource. On the agent workspace we have a new page that shows these records in a list. Although, when an agent clicks on one of these records, based on the values stored in the record the agent needs to be redirected to either an internal route or an external url.

To achieve this, as mentioned in step 2, we created a page script that called a scripted REST api. In the logic we processed the url. The response contained a type value to determine whether the url was internal or external. Based on the the type, we emitted an event that was handled by the 'Link to destination'. This ultimately made the row click event to dynamically redirect the agent to an external or internal url.

Hope this clarifies the use case.

 

PS: The external links always open in a new tab. You can always create a new page with the iFrame component in it and then pass the url value in the parameter and then load the page in the iframe. But this might lead to CORS issue.

Velma
Tera Guru

Thanks again, Robin. I think you built this before the Advanced mode was there on Link to Destination (which allows scripting and making different choices based on whatever you can get at). It was very interesting to me because I learned something about how to use api.emit and a handled event together--seems like a way around some gaps I perceive in UI Builder.

Robin John
Kilo Sage

Yes, you are right. The solution was designed for the Quebec release. And with San Diego, the possibilities are much higher. I should probably update the title to emphasise on event handling. Thanks for your queries. It helped me too 🙂 

Andre241
Tera Contributor

@Robin John 

The steps you provided unfortunately did not work for me.

Robin John
Kilo Sage

That is unfortunate.

Did you make it work somehow? if not, we can debug it together. If yes, could you point out how you made it work?

Andre241
Tera Contributor

Hello Robin,

It never worked at all. How would we connect on this issue? Thanks again.

oclemons
Tera Contributor

@Velma did you find a way to do this?  I'm pretty frustrated at this point.  I want to redirect the user in the same tab as well and the link to destination is not what I need.  I should mention im on Tokyo, btw.

Version history
Last update:
‎04-27-2022 01:21 AM
Updated by: