How to upload and store attachments from Service Portal widget to custom table
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi Community,
I am building an application and have a Service Portal requirement and need guidance.
I have created a custom table and a Service Portal widget with a form that includes a file upload option. When the user submits the widget form from the portal, I want to:
Create record in table
Upload and store the attached file against that same record (visible in the attachment section of the custom table record)
Currently, the widget submits but the record and/or attachment is not getting saved in the custom table.
- Labels:
-
Service Portal
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
why not use after update BR on your table?
Condition: Attachment field Changes [AND] Attachment Field [IS NOT EMPTY]
Script:
-> create record in other table
-> use GlideSysAttachment API to copy the file to your record
what did you start and where are you stuck?
š” If my response helped, please mark it as correct ā and close the thread šā this helps future readers find the solution faster! š
Ankur
⨠Certified Technical Architect || ⨠9x ServiceNow MVP || ⨠ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
I started creating public service portal by adding widget to it which helps in uploading a file. So, when I upload a file in portal, it is not storing in custom table. Only record is being created there is not data stored for attachments in it. (other values from portal are being stored in custom table, only attachment part is not stored). If you have any solutions, please do provide.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @Sneha KH
This behavior is expected in Service Portal and usually happens because attachments are not handled automatically like platform forms.
In a Service Portal widget, you must explicitly control record creation and attachment upload. If either step is missed or done in the wrong order, the record or attachment will not be saved.
Below is the approach that works correctly.
Approach
Attachments can only be saved after a record exists.
So the flow must be:
Create the record in the widget Server Script
Capture the generated sys_id
Return the sys_id to the Client Script
Upload the attachment using table name and sys_id
Server Script
(Create the custom table record first)
(function () {
if (input && input.action === 'create') {
var gr = new GlideRecord('u_custom_table'); // custom table name
gr.initialize();
gr.u_short_description = input.short_description;
var sysId = gr.insert();
data.sys_id = sysId;
data.table = 'u_custom_table';
}
})();Client Script
(Upload attachment after record is created)
api.controller = function ($scope, spAttachmentManager, spUtil) {
var c = this;
c.attachments = [];
c.submit = function () {
c.data.action = 'create';
c.data.short_description = c.short_description;
c.server.update().then(function (response) {
var sysId = response.data.sys_id;
var table = response.data.table;
if (sysId && c.attachments.length > 0) {
spAttachmentManager.uploadAttachments(
table,
sysId,
c.attachments
).then(function () {
spUtil.addInfoMessage('Record and attachment saved successfully');
});
}
});
};
};HTML
(File upload field)
<input type="file" multiple ng-files="c.attachments" class="form-control">*************************************************************************************************************
If this response helps, please mark it as Accept as Solution and Helpful.
Doing so helps others in the community and encourages me to keep contributing.
Regards
Vaishali Singh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
We have tried this already but didn't work. Give me any other solutions if you have?
