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.

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

 

2 REPLIES 2

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.

 

Ankur Bawiskar
Tera Patron
Tera Patron

@Mani_Mk 

what's your business requirement?

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