The CreatorCon Call for Content is officially open! Get started here.

How can i save a file like attachment in UI Page?

EvilRojo
Tera Expert

How can i save a variable file type like attachment, i have my code

HTML

<input id="attachment" name="attachment" mandatory="true" type="file"/>
<g:dialog_buttons_ok_cancel ok_text="Actualizar" cancel_text="Cancelar" ok="updateCom();return true" cancel="closeWindow();"/>

 Client script

function updateCom() {
    gel('table_sys_id').value = 'sys_id_text';
    gel('fileName').value = 'NameofDocument.xlsx';
    GlideDialogWindow.get().destroy();
}
function closeWindow() {
    GlideDialogWindow.get().destroy();
}

Processing script

var attachmentSysid = '';
var gr = new GlideRecord('my_table');
gr.addEncodedQuery('query');
gr.query();
if (gr.next()) {
    var grAttachment = new GlideSysAttachment();
    attachmentSysid = grAttachment.write(gr, fileName, 'application/xlsx', attachment);
}

this code works, but the problem is that the variable 'attachment' just gave me the title of the original document(for example: documentTest.xlsx), I need the excel the i have in the file variable

14 REPLIES 14

Samaksh Wani
Giga Sage

Hello @EvilRojo 

 

Update your processing script :-

 

var attachmentGR = new GlideRecord('sys_attachment');
attachmentGR.addQuery('table_name', 'YOUR_TABLE_NAME'); 
attachmentGR.addQuery('table_sys_id', 'YOUR_RECORD_SYS_ID'); 
attachmentGR.query();

if (attachmentGR.next()) {
    var newFileName = 'CustomFileName'; // Replace 'CustomFileName' with your desired custom file name

    // Set the new file name for the attachment
    attachmentGR.file_name = newFileName;
    attachmentGR.update();
}

 

Plz mark my solution as Accept, If you find it helpful.

 

Regards,

Samaksh

It not works

Hi @EvilRojo,

 

Quick question: From your example code you posted it looks like the UI Page is being called by a UI Action. Is that correct?

As @-O-  suggested there is already an out-of-box way to add attachments and you don't have to even create a UI page. From a UI Action or even a Client Script you can call "saveAttachment(<table name>, <sys_id>)" and it will inherently bring up the things necessary to add an attachment.

 

Here are some examples.

As client script on an onchange

client_script_saveAttachment.png

 

Working example:

client_script_onchange_saveAttachment.gif

 

UI Action

ui_action_saveAttachment.png

 

Working example for UI Action

ui_action_saveAttachment.gif

VG3
Tera Contributor

Hello @ChrisBurks ,

 

save attachment(table name, Sysid) not working in workspace UI builder like I have used UI Page like below,

 

Html code --- on click() Handel attachment function

Client script : 

I used save attachment () but window is not opening

And in workspace UI builder glideDialoWindow.get() will work or not ?

 

Please check and correct me if I am wrong

Thanks in advance!