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

Did this solve your issue?

hey,

thank you for your reply,

I tried that, I got a new tab with the url but I didn't get the parameter for the url.

https://hpindigodev.service-now.com/null.iix

in the console, I got url is undefined.

 

Sorry, made an error in the GlideAjax. getParameter call was missing.

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'
});​

As before, I have not tested this and some minor modifications may be required.

ok, I figured it out.

in the client, instead of using g_form.getuniquvalue(), I used g_list.getChecked() (because the images stored in a list).

thank you very much for your help!

Great to hear 🙂

Please mark correct if it has solved your issue.
I updated the code snippet too!