Issue with Attaching Files to Specific Fields from Mobile Using actionResult.addAttachment()
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-20-2025 04:25 AM
Context:
I'm working on a mobile Action Item in ServiceNow that involves attaching files to specific fields on a record.
Setup Details:
I have created an input field on the screen for the Action Item, of type Attachment, named: submitted_letter.
The Action Item is of type Update, but the default update behavior is not working, so I am using a Scripted Action Item to manually handle the update logic.
I'm using actionResult.addAttachment() to attempt to attach files to a given field and then associate it with the corresponding record.
Problems Faced:
Attachments are not available immediately in the sys_attachment table when actionResult.addAttachment() is called possibly only after the Action Item execution completes.
actionResult.addAttachment() does not return a sys_id or reference to the inserted attachment, making it hard to associate the attachment to a specific field.
I need to attach multiple files to multiple fields, but since attachments are delayed or invisible during the script runtime, I can’t reliably map them.
Even when I try to query sys_attachment manually, the attachment doesn't show up in time to associate it with the target field (e.g., submitted_letter).
My Requirement:
Map each file to its specific field on the record (e.g., submitted_letter, supporting_documents, etc.).
Optionally rename each attachment.
Ensure that all attachments are present and mapped correctly with the fields.
(function WriteBackAction(parm_input, parm_variable, actionResult) {
try {
var sys_id = parm_variable['sys_id'];
if (!sys_id) return;
var tableName = "xxxx";
var gr = new GlideRecord(tableName);
if (!gr.get(sys_id)) {
gs.error("[BOND] Failed to find the Record");
return;
}
var test = actionResult.addAttachment("submitted_letter", tableName, sys_id);
var attachmentGr = new GlideRecord("sys_attachment");
attachmentGr.addEncodedQuery("table_name=" + tableName + "^table_sys_id=" + sys_id);
attachmentGr.orderByDesc("sys_created_on");
attachmentGr.setLimit(1);
attachmentGr.query();
if (attachmentGr.next()) {
gr.setValue("submitted_letter", attachmentGr.getValue('sys_id'));
} else {
gs.warn("[BOND] No attachment found for field: " + "submitted_letter");
}
gr.update();
} catch (e) {
gs.error("[BOND] Error in WriteBackAction: " + (e.message || e));
}
})(parm_input, parm_variable, actionResult);
I tried Putting the code outside of WriteBackAction but still the attachment is not yet there
(function WriteBackAction(parm_input, parm_variable, actionResult) {
var sys_id = parm_variable['sys_id'];
if (!sys_id) return;
var tableName = "xxxx";
actionResult.addAttachment("submitted_letter", tableName, sys_id);
})(parm_input, parm_variable, actionResult);
var sys_id = "xxxxx";
var tableName = "xxxx";
var attachmentGr = new GlideRecord("sys_attachment");
attachmentGr.addEncodedQuery("table_name=" + tableName + "^table_sys_id=" + sys_id);
attachmentGr.orderByDesc("sys_created_on");
attachmentGr.setLimit(1);
attachmentGr.query();
if (attachmentGr.next()) {
gr.setValue("submitted_letter", attachmentGr.getValue('sys_id'));
} else {
gs.warn("[BOND] No attachment found for field: " + "submitted_letter");
}Thanks in advance for any guidance or suggestions!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 weeks ago
Any update here? I am facing the exact same issue...