URL in OnSubmit client script without hardcoding

Jake Adams
Tera Contributor

Hi,

 

I want to provide an URL to a Service Catalog based on condition.

 

At the moment I'm using Onsubmit client script and have stored the URL using HTML tag and showing that as Link in adderrormessage popup.

 

I want to do it without hardcoding the URL. How can I achieve this?

5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

@Jake Adams 

then do this

1) use a string variable and store the URL in it, hide this string variable using UI policy, give the order for this variable as highest, create system property and store the URL and give the default value

javascript: gs.getProperty('propertyName');

2) then use onSubmit and access this hidden variable value and redirect

use this for native

g_navigation.open(url, '_blank');

for portal view use this

top.window.open(url,"_blank");

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Ankur Bawiskar
Tera Patron
Tera Patron

@Jake Adams 

since you edited your question, the logic still remains the same.

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Ankur Bawiskar
Tera Patron
Tera Patron

@Jake Adams 

Would you mind closing your earlier questions by marking appropriate response as correct?

Members have invested their time and efforts in helping you.

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

sunil maddheshi
Tera Guru

@Jake Adams 

Script inlcude:

var CatalogURLGenerator = Class.create();
CatalogURLGenerator.prototype = {
    initialize: function() {},

    getCatalogURL: function(category) {
        var url = ''; // Default empty URL

        // Define dynamic URLs based on category or any condition
        if (category == 'IT') {
            url = '/sc?id=sc_category&sys_id=1234567890abcdef'; // IT Category
        } else if (category == 'HR') {
            url = '/sc?id=sc_category&sys_id=abcdef1234567890'; // HR Category
        } else {
            url = '/sc?id=sc_home'; // Default Service Catalog homepage
        }

        return url;
    },

    type: 'CatalogURLGenerator'
};

OnSubmit client Script:

function onSubmit() {
    var ga = new GlideAjax('CatalogURLGenerator');
    ga.addParam('sysparm_name', 'getCatalogURL');
    ga.addParam('sysparm_category', g_form.getValue('category')); // Assuming "category" is a field

    ga.getXMLAnswer(function(response) {
        if (response) {
            var link = '<a href="' + response + '" target="_blank">Click here</a>';
            g_form.addErrorMessage('Your request has been submitted. ' + link);
        }
    });

    return true; // Allow form submission
}

Please mark correct/helpful if this helps you!