PDFGenerationAPI: How to place attachment ONLY in a file_attachment field instead of header

Joe Walters
Tera Expert

I'm using the PDFGenerationAPI in a UI Action to generate a PDF and attach it to a specific document and, as expected by default, it's visible the form header.

 

I also have a 'file attachment' field, however, and this is where I actually want it to appear (and only there).

 

Anyone know of a way to only show the attachment in the file attachment field and not also in the form header?

 

JoeWalters_4-1701975350427.png

 

 

 

 

6 REPLIES 6

AshishKM
Kilo Patron
Kilo Patron

Hi @Joe Walters

 

Please share the UI Action code.

 

The header ( attachments ) attached at record level, but attachment type variable is part of record itself , although both reference record stored in same table but point of reference are different.

 

-Thanks,

AshishKMishra


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

It's just a simple UI Action based on the code below from the PDFGenerationAPI documentation.

var v = new sn_pdfgeneratorutils.PDFGenerationAPI;

//  (Option) get HTML from the description field of an incident record
var gr = new GlideRecord("incident");
var html;

if (gr.get("<tableSysId>")) {
 html = gr.description.toString();
}

var result = v.convertToPDF(html, "incident", "<target_sys_id>", "myPDF");
gs.info(JSON.stringify(result));

 

After running this code, I have a couple of lines to populate the sys_id of the attachment I just created into my file attachment field. The result then is the attachment is displayed in both locations on the form.

 

But the result I'm trying to replicate with script is what happens when you use the UI to add a file into a file attachment field. When you do this in the UI, the file will only be visible in the field and not in the form's header.

 

Whether you attach at the record level or into the field, records are created on both the sys_attachment table and sys_attachment_doc tables, so there must be something else that I'm missing and can't find in any documentation.

The trick here with the File Attachment field type, as with the Image field type, is how the attachments are stored in the sys_attachment table. This is where the "ZZ_YY" prefix comes in. 

 

Files added to the File Attachment table retain their name (e.g., 'xyz.png') but are stored on the table using the ZZ_YY prefix (e.g., "ZZ_YYincident").

 

Similarly (but slightly different), if you add an Image field to a table and upload an image, that will be stored with a file name of the field name on the ZZ_YY prefixed table. For instance, if your field name was "u_image" and the table was incident, the sys_attachment would have a file name of just "u_image" and table would be ZZ_YYincident.

 

You could do this one of two ways

1) When creating the pdf set the target table to "ZZ_YYincident" rather than "incident" (but keep the same target sys_id).

2) After the attachment has been created, change the table from incident to ZZ_YYincident

 

Either way, you shouldn't have to set the field manually afterwards, ServiceNow takes care of that after the attachment is inserted.

 

SanjivMeher
Kilo Patron
Kilo Patron

This thread may help. But not best practice, since it is DOM manipulation.

https://www.servicenow.com/community/developer-forum/how-to-update-client-script-to-hide-attachment-...

 


Please mark this response as correct or helpful if it assisted you with your question.