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

paulmorris
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!

View solution in original post

11 REPLIES 11

Hi Paul,

I have a similar requirement but need to build URL by calling API. Can you please take a look on my post?

 

https://community.servicenow.com/community?id=community_question&sys_id=142a86dddb553b88190dfb243996192b&anchor=answer_73e1cf9ddbdd37480be6a345ca961911

 

Not applicable

Hello!

I was having the same issue but instead of .openPopup I used the .open method and it worked!

 

Thank you so much!

 

Regards,

Pedro