How to replace the attachment attached before sending the item after sending the items
- 新着としてマーク
- ブックマーク
- 購読
- ミュート
- RSS フィードを購読する
- ハイライト
- 印刷
- 不適切なコンテンツを報告
05-24-2023 05:25 AM
Hi,
I am developing with catalog items.
A certain service has a requirement to attach multiple attachments to the variables prepared on the application screen.
I would like to replace the attached file that was attached before sending after sending the application.
It is possible to add attachments from the ticket page after submitting the application, but I would like to replace the target attachment with the corresponding variable's attachment item. (I want the file after replacement to be the latest version)
Is there any way to implement it?
Regards,
- 新着としてマーク
- ブックマーク
- 購読
- ミュート
- RSS フィードを購読する
- ハイライト
- 印刷
- 不適切なコンテンツを報告
05-27-2023 12:53 AM
You can use the GlideSysAttachment from Serverside Script in ServiceNow to delete or create attachments.
Refer to the GlideSysAttachment Global API documentation at the following link:
GlideSysAttachment - Global (servicenow.com)
- 新着としてマーク
- ブックマーク
- 購読
- ミュート
- RSS フィードを購読する
- ハイライト
- 印刷
- 不適切なコンテンツを報告
05-27-2023 01:20 AM
Hi @Mi1 ,
I trust you are doing great.
Here's an outline of the solution:
Create a custom UI action or a button on the application screen to trigger the attachment replacement process.
When the UI action or button is clicked, invoke a server-side script or a business rule to handle the attachment replacement logic.
In the server-side script, retrieve the existing attachments related to the corresponding variable's attachment item. You can use the getAttachments() method from the GlideSysAttachment API to fetch the attachments associated with a record.
Prompt the user to select the file they want to replace the existing attachment with.
Once the user selects the replacement file, delete the existing attachment using the deleteAttachment() method from the GlideSysAttachment API. This method requires the sys_id of the attachment record as a parameter.
After deleting the existing attachment, create a new attachment record using the write() method from the GlideSysAttachment API. Provide the file content, file name, and related record information to create the new attachment.
Save the record with the updated attachment.
// Get the existing attachments related to the variable's attachment item
var attachmentGR = new GlideSysAttachment().getAttachments(table, recordSysId, 'variable_attachment_field_name');
// Delete the existing attachment
if (attachmentGR.next()) {
var attachmentSysId = attachmentGR.getValue('sys_id');
new GlideSysAttachment().deleteAttachment(attachmentSysId);
}
// Create a new attachment with the replacement file
var attachment = new GlideSysAttachment();
attachment.write(table, recordSysId, 'variable_attachment_field_name', 'Replacement File Name', 'Replacement File Content');
// Save the record with the updated attachment
current.update();
// Redirect or perform further actions as needed
Was this answer helpful?
Please consider marking it correct or helpful.
Your feedback helps us improve!
Thank you!
Regards,
Amit Gujrathi