Auto populate attachment field on portal using script include
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-20-2025 11:35 AM - edited ‎01-20-2025 01:30 PM
I am task with adding three attachment fields to a record producer and having them populate referencing another record producer field (cp_record) on the portal view of the form.
The client script and the client script include I have so far is below, the script is populating the string fields but the three attachment fields I have added are not being populated with the attachment using the reference as it should please advise
table (x_g_dh5_ccht_ccht_task)
script include:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-20-2025 07:17 PM
so your current record producer has 3 variables of type file attachment
You have 1 variable which refers to existing record submitted via record producer on same table?
Based on the record selected you want to set the variable value for those 3 attachment variables?
Please share some screenshots.
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-20-2025 09:49 PM - edited ‎01-20-2025 09:50 PM
Yes correct,
the three file type fields below are used on two different record producers. I want to use another variable "cp_record" to reference a previously submitted request and populate the 3 attachments fields pm the current record producer once that field"cp_record" is filled.
victims_complaint_date_time
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-21-2025 07:31 PM
it's very difficult to identify which file was attached to which attachment variable type.
If your record producer has 2 attachment variables then both of them have prefix as ZZ_YY so when you query sys_attachment with this tableName then you won't find it
try with this but this doesn't guarantee the files will be set in correct sequence or with correct variable
attachmentGR.addQuery('table_name', 'ZZ_YYx_g_dh5_ccht_ccht_task'); // Replace with the appropriate table
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-20-2025 09:36 PM
Hi @BenjaminY
Attachment fields in ServiceNow are typically not directly populated by setting their value to Sys IDs. Instead, attachments are associated with a record through the sys_attachment table, and the UI handles displaying them based on the associated record.
Updated getAttachments
Method :-
getAttachments: function(fieldName, recordSysId, targetRecordSysId) {
var attachmentGR = new GlideRecord('sys_attachment');
attachmentGR.addQuery('table_name', 'x_g_dh5_ccht_ccht_task'); // Source table
attachmentGR.addQuery('table_sys_id', recordSysId); // Source record Sys ID
attachmentGR.query();
while (attachmentGR.next()) {
var newAttachment = new GlideRecord('sys_attachment');
newAttachment.initialize();
newAttachment.setValue('table_name', 'x_g_dh5_ccht_ccht_task'); // Target table
newAttachment.setValue('table_sys_id', targetRecordSysId); // Target record Sys ID
newAttachment.setValue('file_name', attachmentGR.getValue('file_name'));
newAttachment.setValue('content_type', attachmentGR.getValue('content_type'));
newAttachment.insert();
}
},
Call the Attachment Copy in the Client Script
ajax.addParam('sysparm_name', 'CpRecord');
ajax.addParam('sysparm_record_num', recordNum);
ajax.addParam('sysparm_target_record_sysid', g_form.getUniqueValue());
If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!
Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI
ï”— YouTube: https://www.youtube.com/@learnservicenowwithravi
ï”— LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/