gs.getProperty('glide.servlet.uri') catalog client script - How to get dynamic url of instance

Edxavier Robert
Mega Sage

Hi, I have catalog client script where I need to get the dynamic URL of the current instance.  I was reading that I need to create a script include and use a GlideAjax to call it. But is returning null. So far this is what I got. 

 

Script Include

Client callable is set to true

 

 

var GetURL = Class.create();
GetURL.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getUrlDetails: function() {
        var instanceURL = gs.getProperty('glide.servlet.uri');
        
		return instanceURL;
    },

    type: 'GetURL'
});

 

 

 Catalog client script

 

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    
    //Type appropriate comment here, and begin script below
    var Sys = g_form.getValue('software_items');
    var instance = new GlideAjax('GetURL');
    instance.addParam('sysparam_name', 'url');
    instance.getXML(GetUrlPlease);
	
	function GetUrlPlease(response){
		var answer = response.responseXML.documentElement.getAttribute("answer");
		alert(answer);
	}
        if (newValue !== 'none') {
        var url = answer + 'sp?id=sc_cat_item&sys_id=' + Sys + '&sysparm_category=69d0e41b1b5f8150a190ece0f54bcb4e',
            _blank;
        top.window.open(url, "_blank");
    }
}

 

 

 

1 ACCEPTED SOLUTION

RaghavSh
Kilo Patron

Replace below line in your code:

 

instance.addParam('sysparm_name', 'getUrlDetails');


Raghav
MVP 2023

View solution in original post

10 REPLIES 10

RaghavSh
Kilo Patron

Replace below line in your code:

 

instance.addParam('sysparm_name', 'getUrlDetails');


Raghav
MVP 2023

Hi @RaghavSh

That work! Now I am getting the correct URL in the alert message that I have, but I am unable to get re-directed. I had a JavaScript error - 'ReferenceError: answer is not defined'

Are you working on portal? If yes window object is not accessible on portal.

 

Refer: https://www.servicenow.com/community/developer-forum/window-location-href-is-not-working-in-client-s... 


Please mark the answer correct/helpful accordingly.


Raghav
MVP 2023

Yes, I am working on the Service Portal.