Redirect user to another record producer based on the reference field

Sangeetha7996
Tera Contributor

If the user selects a particular Business service (reference field) in the incident record producer, it should redirect to it's specific record producer or catalog form.
Business service field refers to the cmdb table. In the cmdb table, we have an attribute that holds the sys id of its dedicated record producer/ catalog form.
We have used onchange client script to redirect and script include to retrieve the sys id of the record producer from the cmdb table.

 

client script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var busc = g_form.getValue('business_service');
    var retrieveUrl = new GlideAjax('recordProducerNavigationUrl');
    retrieveUrl.addParam('sysparm_name', 'getrecprodid');
    retrieveUrl.addParam('sysparm_busc', busc);
    retrieveUrl.getXMLAnswer(userResponse);
    function userResponse(response) {
        if (response) {
            var dataUn = JSON.parse(response);
            var retUrl = dataUn.url;
            if (retUrl) {
                window.location.href = 'https://dev221804.service-now.com/sp?id=sc_cat_item&sys_id=' + retUrl;
            }
        }
    }
}

Script include: client callable is enabled
var recordProducerNavigationUrl = Class.create();
recordProducerNavigationUrl.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getrecprodid: function() {
        var buscId = this.getParameter('sysparm_busc');
        var recordProdId = new GlideRecord('cmdb_ci_service');
        recordProdId.addQuery('sys_id', buscId);
        recordProdId.query();
        if (recordProdId.next()) {
            var result = recordProdId.u_incident_record_producer.toString();
            return JSON.stringify(result);

        } else {
            var resultE = '';
            return JSON.stringify(resultE);
        }
    },
    type: 'recordProducerNavigationUrl'
});
1 ACCEPTED SOLUTION

Bhavya11
Kilo Patron

Hi @Sangeetha7996 

 

i hope from server side your getting sys_id of item that you need to redirect whenever there is change in Reference variable.

 

instead of  "window.location.href " use this "top.window.location"

For reference   

 

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }


        var retrieveUrl = new GlideAjax('GetLocationsByRegion');
        retrieveUrl.addParam('sysparm_name', 'getrecprodid');
        retrieveUrl.addParam('sysparm_busc', newValue);
        retrieveUrl.getXMLAnswer(function(response) {
            if (response) {
                var dataUn = JSON.parse(response);
                var retUrl = dataUn.url;
			
                if(retUrl){
                    top.window.location = 'https://dev####.service-now.com/sp?id=sc_cat_item&sys_id=' + retUrl;
				}
				else {
                alert('No URL found for the given business service.');
            }
        } 
            
        });
    

}

 

 



Please mark helpful & correct answer if it's really worthy for you.

Thanks,

BK

View solution in original post

2 REPLIES 2

Bhavya11
Kilo Patron

Hi @Sangeetha7996 

 

i hope from server side your getting sys_id of item that you need to redirect whenever there is change in Reference variable.

 

instead of  "window.location.href " use this "top.window.location"

For reference   

 

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }


        var retrieveUrl = new GlideAjax('GetLocationsByRegion');
        retrieveUrl.addParam('sysparm_name', 'getrecprodid');
        retrieveUrl.addParam('sysparm_busc', newValue);
        retrieveUrl.getXMLAnswer(function(response) {
            if (response) {
                var dataUn = JSON.parse(response);
                var retUrl = dataUn.url;
			
                if(retUrl){
                    top.window.location = 'https://dev####.service-now.com/sp?id=sc_cat_item&sys_id=' + retUrl;
				}
				else {
                alert('No URL found for the given business service.');
            }
        } 
            
        });
    

}

 

 



Please mark helpful & correct answer if it's really worthy for you.

Thanks,

BK

Thank you! I have tried the above modified script it is working.