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

How to add an attachment to a record using a file input on a UI Page (custom development).

nick0989
Tera Contributor

I have a form on a UI page and all I need to do is take an input field with type "file", and have the user upload a file. Simple enough.

The file then needs to be added to a record as an attachment. I have the record in the form.

I can't figure out how to add the file as an attachment, I have tried GlideSysAttachment().

var fileName = this.getParameter('sysparm_file_name'); //file name from input
var fileType = this.getParameter('sysparm_file_type'); //file type
var tableName = this.getParameter('sysparm_table_name'); //the table of the record
var task = this.getParameter('sysparm_task'); //the record

var attachment = new GlideSysAttachment();

var rec = new GlideRecord(tableName);
rec.get(task);
var file = fileName;
var contentType = fileType;
var content = ""; //I am unsure if I can put anything here?
var agr = attachment.write(rec, file, contentType, content);
return agr;

I am unsure as to if this will work with file types.

I have tried the api/now/attachment/file end point to POST.

/api/now/attachment/file?table_name=" + tableName + "&table_sys_id=" + task + "&file_name=" + fileName

But this doesn't seem to work either.

I have to use a input element with type file in the UI page.

13 REPLIES 13

Hello @ChrisBurks 

I appreciate your prompt response.

Let me check if there are any ACL restriction. I have a question, If there is restriction  I should not be able to add attachment on the table as well?

Because I am able to add attachment using the Manage Attachments.

 

Hello @ChrisBurks 

 

I would also request you to please share your code and images of it uploading to your table in your instance

Hi,

 

As I noted before I used the same code you posted with the exception of pointing to the table/record within my instance and commenting out fdata.

ui_page.png

 

and here is the file attached to the record.

table_with_attachment.png

 

If it's not ACLs or security, it could be application scope settings on the table and or the UI Page. If the table is set to only allow from the scope or if it has restricted caller access and the UI page is in a different scope. You might have to create restricted caller access records.

 

How do I send a certificate? Is there a sample code? Because I am using 401 status now