Issue with Attaching Files to Specific Fields from Mobile Using actionResult.addAttachment()

SBharathK
Tera Contributor

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:

  1. Attachments are not available immediately in the sys_attachment table when actionResult.addAttachment() is called possibly only after the Action Item execution completes.

  2. 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.

  3. 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.

  4. 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!

4 REPLIES 4

RaghavSh
Kilo Patron

can you make the below change and try:

 

var sys_id = parm_variable.sys_id;

 


Raghav
MVP 2023

@RaghavSh The issue is not with the sys_id, it is the correct sys_id, regardless of how I access it:

var sys_id = parm_variable['sys_id'];
// or
var sys_id = parm_variable.sys_id;

Both lines return the same value when logged.

Yeah i believe thats how SN has configured this. 
i believe what you need to do now is play with the system to get your requirement done.

 

schedule an event from your script and do the action on your table through script action, its just a suggestion.


Raghav
MVP 2023

Thanks for the suggestion, @RaghavSh  I appreciate it!

Just to clarify, the attachment is working fine and is being linked to the record as expected. However, my requirement is a bit more specific: I’m trying to attach the file at the field level, not the record level.

Let me know if you’ve come across a way to achieve that, or if you think there’s a workaround.

Thanks again!