Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

UI action redirect to a new window/tab using server-side script

TzachB
Giga Contributor

Hey everyone,

I'm trying to open an image with a button click using UI action (server-side script).

there is a way to redirect to a new window/tab?

Most of the solutions I found refer to the client-side.

this is my script: (the script work ok, but the image open in the same page)

var UrlImageSysId='';//image sysId for url param
var url='';//url for redirect
var currentImage = current.sys_id;//current image sysId from 'u_ts_image' table


var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_sys_id',currentImage);
gr.query();
if(gr.next()){
UrlImageSysId=gr.sys_id;
}

url = 'https://hpindigodev.service-now.com/'+UrlImageSysId+'.iix';
action.setRedirectURL(url);

1 ACCEPTED SOLUTION

The SN Nerd
Giga Sage
Giga Sage

It is not possible to open a new tab or window in a browser by server-side code alone. You can only choose the next page shown.

When you think about it - the server cannot control the browser on a client's computer. If it could, that would be a massive security flaw!

You need to do it with both Client and Server Scripts.

This is how:

  1. Write a Client callable Script include called AttachmentAjaxUtil with your attachment fetch script that takes currentImage as a parameter and returns UrlImageSysId
    var AttachmentAjaxUtil = Class.create();
    AttachmentAjaxUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    	
    	getAttachmentSysId: function() {
    		var UrlImageSysId='';//image sysId for url param
    		var url='';//url for redirect
    		var currentImage = this.getParameter('sysparm_currentImage');//current image sysId from 'u_ts_image' table
    
    
    		var gr = new GlideRecord('sys_attachment');
    		gr.addQuery('table_sys_id',currentImage);
    		gr.query();
    		if(gr.next()){
    			UrlImageSysId=gr.sys_id;
    		}
    		
    		return UrlImageSysId;
    	},
    	
        type: 'AttachmentAjaxUtil'
    });​
  2. Create a Client Script using GlideAjax to fetch that sys_id
    var ga = new GlideAjax('AttachmentAjaxUtil'); 
    ga.addParam('sysparm_name','getAttachmentSysId');  
    ga.addParam('sysparm_currentImage',g_list.getChecked()); 
    ga.getXML(showPopup);  
    
    function showPopup(response) {  
       var UrlImageSysId= response.responseXML.documentElement.getAttribute("answer"); 
       url = 'https://hpindigodev.service-now.com/'+UrlImageSysId+'.iix';
       g_navigation.openPopup(url); //this is preferable to window.open - window is not supported API, will probably be flagged in health checks
    
    }​

    Let me know if you have issues with your GlideAjax.

    Please note the code provided has not been tested. Use at your own risk!


ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

View solution in original post

11 REPLIES 11

Allen Andreas
Tera Patron

Hi,

If this UI action is client side, then you'll need to use a different method for opening URL in new tab.

See: https://community.servicenow.com/community?id=community_question&sys_id=5516976bdb5c57847b337a9e0f96...

If this is server side, you can try:

window.open(url, '_blank');
Please mark reply as Helpful/Correct, if applicable. Thanks!

Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

thanks for your comment,

I am using server-side script, I tried that one and it's doesn't work (it's open the same page again..)

Hi, can you try:

g_navigation.openPopup(url);

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

The SN Nerd
Giga Sage
Giga Sage

It is not possible to open a new tab or window in a browser by server-side code alone. You can only choose the next page shown.

When you think about it - the server cannot control the browser on a client's computer. If it could, that would be a massive security flaw!

You need to do it with both Client and Server Scripts.

This is how:

  1. Write a Client callable Script include called AttachmentAjaxUtil with your attachment fetch script that takes currentImage as a parameter and returns UrlImageSysId
    var AttachmentAjaxUtil = Class.create();
    AttachmentAjaxUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    	
    	getAttachmentSysId: function() {
    		var UrlImageSysId='';//image sysId for url param
    		var url='';//url for redirect
    		var currentImage = this.getParameter('sysparm_currentImage');//current image sysId from 'u_ts_image' table
    
    
    		var gr = new GlideRecord('sys_attachment');
    		gr.addQuery('table_sys_id',currentImage);
    		gr.query();
    		if(gr.next()){
    			UrlImageSysId=gr.sys_id;
    		}
    		
    		return UrlImageSysId;
    	},
    	
        type: 'AttachmentAjaxUtil'
    });​
  2. Create a Client Script using GlideAjax to fetch that sys_id
    var ga = new GlideAjax('AttachmentAjaxUtil'); 
    ga.addParam('sysparm_name','getAttachmentSysId');  
    ga.addParam('sysparm_currentImage',g_list.getChecked()); 
    ga.getXML(showPopup);  
    
    function showPopup(response) {  
       var UrlImageSysId= response.responseXML.documentElement.getAttribute("answer"); 
       url = 'https://hpindigodev.service-now.com/'+UrlImageSysId+'.iix';
       g_navigation.openPopup(url); //this is preferable to window.open - window is not supported API, will probably be flagged in health checks
    
    }​

    Let me know if you have issues with your GlideAjax.

    Please note the code provided has not been tested. Use at your own risk!


ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022