Need help for Script include

soumyadaggula
Tera Contributor

Hi Team  , 

am using below catalogue client script to re-direct a catalogue item to Order guide , but I want to write a script include for this , so that it can be used for other order guides also . can you please guide me on how to use this script for Script include .

function onLoad() {
var tstr= top.window.location;
tstr=tstr.toString();
var sstr = "id=sc_cat_item&sys_id";
var ct = -2;
   ct = tstr.indexOf(sstr);  
if (ct != -1) {
top.window.location = "esc?id=sc_cat_item_guide&table=sc_cat_item&sys_id=21470fe11b9601108be4eb53604bcb3a";
 
}
1 ACCEPTED SOLUTION

Ashish37
Mega Guru

Hi @soumyadaggula ,

 

You can do this by creating client callable script include and adding this code to it. you have to use GlideAjax to call that script from you client script. I  tried this and it did worked, you have to pass tstr as a parameter to script include.

.

 

Script include:

 

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

    openNewCatalog: function() {
        var tstr = this.getParameter("sysparm_tstr");
        tstr = tstr.toString();
        var sstr = "id=sc_cat_item&sys_id";
        var ct = -2;
        ct = tstr.indexOf(sstr);
        if (ct != -1) {
            return "esc?id=sc_cat_item_guide&table=sc_cat_item&sys_id=0317ba9d47120510f53d37d2846d43bb";

        }
    },

    type: 'Ashish_Catalog_opening'
});
----------------------------------------------
Client script:
function onLoad() {
    //
    var ga = new GlideAjax("Ashish_Catalog_opening");
    var tstr = top.window.location;
    ga.addParam('sysparm_name', 'openNewCatalog'); 
    ga.addParam('sysparm_tstr', "tstr"); 
    ga.getXML(callback);
 
    function callback(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        alert(answer);
        top.window.location = answer;
    }
}
 -------------------------------------------------------

 

View solution in original post

7 REPLIES 7

Sandeep Rajput
Tera Patron
Tera Patron

@soumyadaggula This is client side code, you will not be able to convert this to a Script include (Server side code). Instead, you can plan to convert this to a UI Script(client side code) and call the same UI script from different client scripts.

 

Please refer to this documentation https://docs.servicenow.com/bundle/washingtondc-application-development/page/script/client-scripts/c... to know more about UI Scripts. 

@Sandeep Rajput 
thank you Sandeep , I will try this 

@soumyadaggula Please mark the response helpful and accepted solution if it managed to answer your question.

Anurag Tripathi
Mega Patron
Mega Patron

Hi,

Redirection can happen on client side also. Any reason why you need to get to server side and do it?

 

On Client side it can be as simple as this

var redirectURL = 'https://www.google.com/'; //url to redirect; this approach works in platform and Service Portal view
top.window.location = redirectURL ;
-Anurag