How to add dynamic URL in instance with URL?

Mani_Mk
Tera Contributor

I have a requirement to change URL dynamically or based on condition on widget in sp_instance_link HREF/URL. How to handle this or is there any way to do this

Mani_Mk_0-1765205104775.png

 

5 REPLIES 5

Shruti
Giga Sage
Giga Sage

Hi

It is possible to create a custom portal page and a custom widget, then update the HREF to use the sp_instance_link

Below is an example demonstrating how to configure a dynamic URL for both admin and non-admin users.

Shruti_0-1765260177560.png

1. Create a new portal page redirect_menu_url

2. Create a custom widget and add it to redirect_menu_url page

Add below code to the server script

(function() {

    // Get the key from the URL parameter
    var key = $sp.getParameter('key');

    // Fetch the system property (JSON string)
    var urlsJson = gs.getProperty('portal.menu.links');

    var obj = JSON.parse(urlsJson);

    var result = '';
    var envKey = '';

    if (gs.hasRole('admin')) {
        envKey = 'admin';
    } else {
        envKey = 'non-admin';
    }

    if (envKey && obj[key]) {
        // obj[key] is a JSON string that needs to be parsed again
        var innerObj = JSON.parse(obj[key].replace(/'/g, '"')); // Convert single to double quotes
        if (innerObj[envKey]) {
            result = innerObj[envKey];
        }
    }
    data.targetUrl = result;


})();

3. Create a system property portal.menu.links

Value : {
"get_help": "{'admin':'https://www.google.com/','non-admin':'https://www.youtube.com/'}"
}

 

This setup enables dynamic redirection from a portal page based on both the provided key in the URL and the user's role (admin or non-admin). The widget script retrieves the proper destination link from the system property and assigns it to data.targetUrl for use in navigation.

 

Actually my requirement is to use existing page but functionality wise i need to add filter part appended in url

For Example: if i add csm?id=csm_my_lists&table=sn_customerservice_case&target_page_id=csm_ticket in HREF url when widget loads filter = something need to appended on initial load

Ankur Bawiskar
Tera Patron
Tera Patron

@Mani_Mk 

what's your business requirement?

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

I’m using sp_instance_link for icon redirection in a Service Portal widget. The current URL looks like this:

?id=csm&table={table_name}&view={view}

I need to modify this URL dynamically based on certain conditions by appending a filter, for example:

?id=csm&table={table_name}&view={view}&filter={some_condition}

What’s the best way to achieve this?

  • Can it be done on the client side or server side within the widget?
  • Or is there a way to handle this directly in the instance link configuration?