- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2020 10:33 PM
Hi Team,
I am submitting a new record to a custom table on the portal, before submit I need to check if there are any attachments added to the form. I am using an onSubmit client script for this but when I try to query the sys_attachment table, I dont get any records. The g_form.getUniqueValue() returns -1.
Thanks,
Kannan
Solved! Go to Solution.
- Labels:
-
Incident Management
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2020 11:10 PM
Hi,
the above onSubmit client script did you try setting Isolate Script as false on the client script?
or else you can have before insert BR on your table; query sys_attachment table with the current.table_sys_id as current sys id and if record not found then show message and stop form submission
Note: g_form.getUniqueValue() won't give the record sys_id as sys_id is not generated on new record; it only generates once you submit; so BR can have the current.sys_id
Also in scoped app you cannot do GlideRecord in client script; also you cannot perform synchronous GlideAjax in onSubmit script
BR Script: you can enhance it to determine if specific file name is also attached or not
(function executeRule(current, previous /*null when async*/) {
// Add your code here
var attachmentRec = new GlideRecord('sys_attachment');
attachmentRec.addQuery('table_sys_id', current.sys_id);
attachmentRec.addQuery('table_name', <yourTableName>));
attachmentRec.query();
if(!attachmentRec.next()){
gs.addErrorMessage('Attachment required');
current.setAbortAction(true);
}
})(current, previous);
OR
BR Script;
if(!current.hasAttachments()){
gs.addErrorMessage('Attachment required');
current.setAbortAction(true);
}
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2020 10:45 PM
Hello Kannan,
Try checking with this method: alert(g_form.getValue('sysparm_item_guid'));
What is the requirement?
Why are you not using this Mandatory Attachment Checkbox to make attachment mandatory.
Please check out my article on this
Possible Ways for Making an Attachment Mandatory : Service Portal/Native UI
Please mark as Correct Answer and Helpful, if applicable.
Thank You!
Abhishek Gardade
ServiceNow MVP 2020
Abhishek Gardade
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2020 10:53 PM
Hello Abhishek,
I am not using a catalog item to submit this record, it is directly a new form.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2020 10:55 PM
Have you tried this method g_form.getValue('sysparm_item_guid')) ?
Abhishek Gardade
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2020 10:59 PM
Yes, it comes up empty.