Download attachment from the UI action

Nag9
Tera Expert

Hi All,

I have a requirement to create a link in ritm form to download a file. When the user clicks on the link, it should download a file, and the form should stay on the same page.

For this, I created a UI action for the ritm table with the below code.

 

onclick -confirmDownload()

script:

function confirmDownload() 
var URL = 'nav_to.do?uri=sys_attachment.do?sys_id=aec12b1b970321100408bde3f153affb';
var down = confirm("Do you want to download the file?');
     alert(down);
if (down) 
g_navigation.openPopup(URL);
action.setRedirectURL(url);
    }
}
 
 
It's working as expected, but the issue is that it's redirecting to some other page. If you remove the if condition, the file will not be downloaded when the user clicks on the UI action.
 
On clicking on the UI action, a file should be downloaded with specific fields data as in excel format(like export excel) and it should stay on the same page.
 
Anyone help me on this?
 
@asifnoor - can you look this thread once
 
Thanks
Nag

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Nag9 

your UI action should be client side and update script as this

No need to use action object

function confirmDownload() {
	var URL = '/sys_attachment.do?sys_id=aec12b1b970321100408bde3f153affb';
	var down = confirm("Do you want to download the file?");
	if (down == true) 
		g_navigation.open(URL, '_blank');
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

16 REPLIES 16

Mohit Kaushik
Mega Sage
Mega Sage

Hi @Nag9 ,

 

Please add the below line at end of your code, that should do the work.

 

// add this after redirectURL line
action.setReturnURL(current);

 

 

Please mark this correct and helpful if it solved your query and helpful alone if it lead you in right direction.

 

 

 

Thanks,
Mohit Kaushik
ServiceNow MVP (2023-2025)

Thanks for quick reply,

 

 Still form is redirecting to other page

 

function confirmDownload() {
 
var URL = 'nav_to.do?uri=sys_attachment.do?sys_id=aec12b1b970321100408bde3f153affb';
    var down = confirm('Do you want to download the file?');
     alert(down);
    if (down) {
g_navigation.openPopup(URL);
action.setRedirectURL(url);
  action.setReturnURL(current);
    }
 
}
 
g_navigation.openPopup(URL); - seems this line is causing issue - but if remove this line file is not downloading

Nag9
Tera Expert

@Ankur Bawiskar  Could you please help me on this

 

Ankur Bawiskar
Tera Patron
Tera Patron

@Nag9 

your UI action should be client side and update script as this

No need to use action object

function confirmDownload() {
	var URL = '/sys_attachment.do?sys_id=aec12b1b970321100408bde3f153affb';
	var down = confirm("Do you want to download the file?");
	if (down == true) 
		g_navigation.open(URL, '_blank');
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader