Attachment Count for Scoped App
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-16-2018 10:00 AM
Ok, I know this has been asked numerous times, but I'm still struggling. I have a scoped app that depending on the value of True/False fields, may require attachments before the record can be saved. In my onSubmit client script, I need to find a way to count the number of attachments.
/*
The following script give me the error: onSubmit script error: TypeError: Cannot read property 'getElementsByClassName' of null: function () { [native code] } - possibly because I need something more meaningful than 'attachment_list_items' as the parameter?
var attach = document.getElementsByClassName('attachment_list_items');
for (var count = 0; count < attach.length; count++) {
var child = attach[count].childNodes;
if((child.length) == 1) {
expectedDocCount += 1;
}
}
The following script give me the error: onSubmit script error: TypeError: gel is not a function: function () { [native code] }
var cat_id = gel('sysparm_item_guid').value;
var attachment = new GlideRecord("sys_attachment");
attachment.addQuery("table_name", "x_cee_regulatory_a_x_cee_regulatory_applications_table");
attachment.addQuery("table_sys_id", cat_id);
attachment.query();
while (attachment.next()) {
expectedDocCount++;
}
The following script give me the error: onSubmit script error: ReferenceError: GlideAggregate is not defined:
function () { [native code] }
var rec = new GlideAggregate('sys_attachment');
var sysID = g_form.getValue('sys_id');
rec.addQuery('table', "x_cee_regulatory_a_x_cee_regulatory_applications_table");
rec.addQuery('table_sys_id', sysID);
rec.addAggregate('COUNT');
rec.query();
if (rec.next()) {
expectedDocCount = rec.getAggregate('COUNT');
}
Hoping someone can point me in the right direction.
Thanks
- Labels:
-
Scoped App Development

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-16-2018 12:05 PM
Hello Kevin,
GlideRecord/GlideAggregate is not supported at client side code in Scoped application. You will have to create Script Include and then validate the logic and return back the response to client side. This can be done via GlideAjax if you have to do at client side or you can create a Business rule to validate the check.
https://docs.servicenow.com/bundle/geneva-servicenow-platform/page/script/server_scripting/reference/r_ExamplesOfAsynchronousGlideAjax.html
Thanks,
Pradeep Sharma
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-16-2018 12:36 PM
Thanks Pradeep,
My understanding is that I need to query the sys_attachment table, but I when I look at that table, the only column that has anything that is useful for query purposes is "Table Sys ID". How does the rows in sys_attachment related to the actual record that is being saved?
If I am missing something, please include a sample of the query I would use in the script include and what I would pass from the client script. Keep in mind that my record may not have been saved yet, so not sure there is a sys_id at this point.
Thanks,
Kevin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-17-2018 08:03 AM
Ok, I found the solution, which was to pass in g_form.getUniqueValue() to the script include.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2018 09:12 AM
Hi Kevin,
Could you share your solutions. I'm getting Submit script error: ReferenceError: GlideAggregate is not defined:
function () { [native code] } that you mentioned above when I'm running my client script checking for an attachment in a scoped app. Works fine in Global.