- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-25-2017 07:51 AM
Hello SNC -I am designing a catalog item where when a specific dropdown choice is chosen - lets call it choice_1, then I want to make it mandatory to attach a PDF, and only a PDF. I am new to catalog items in general, and tying this to a UI policy/action as well.
Here is a catalog client script Im working with - but it is far from what I need in this use case. Any help would be appreciated:
function onSubmit() {
if (g_form.getValue('check_attachment') == 'Yes') {
var ord_id = gel('sysparm_cart_edit').value;
var gr = new GlideRecord("sys_attachment");
gr.addQuery("table_name", "sc_cart_item");
gr.addQuery("table_sys_id", ord_id);
gr.query();
if (!gr.next()) {
alert("You must attach a file to submit.");
return false;
}
}
else if (g_form.getValue('check_attachment') == 'No') {
var cat_id = gel('sysparm_item_guid').value;
var gf = new GlideRecord("sys_attachment");
gf.addQuery("table_name", "sc_cart_item");
gf.addQuery("table_sys_id", cat_id);
gf.query();
if (!gf.next()) {
alert("You must attach the required form to submit.");
return false;
}
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-25-2017 09:50 AM
Content type for pdf will be 'application/pdf'
You can use GlideAjax sync because onSubmit should wait for decision. It will have better performance on checking table with GlideRecord.
On GlideAjax you can check if attachment exist and if so, does any of existing attachments have content type as 'application/pdf', then you can return true/false to Ajax call.
OnSubmit is essential as trigger to check attachments. You can also do it on ui action but onSubmit is better considering its catalog item so only right items will be affected.
Summarizing:
OnSubmit with GlideAjax
GlideAjax with GlideRecord and logic to determine right file
GlideAjax returning true/false
OnSubmit checking return from GlideAjax and deciding to return false or continue
For application/pdf content, its better to double check by attaching any file on your instance and check what is in column Content Type
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-25-2017 09:22 AM
Hi Jeremy,
Best pratice is to avoid GlideRecord on client side scripts, please consider changing GlideRecord to GlideAjax and check attachment on server side, when attachment table is huge, client side script can cause performance issue.
Going back to script you need to check content type of file, thats a column in sys_attachment.
It will be like:
if (gr.getRowCount() > 0){
// Attachment exist
while(gr.next()){
// check Content Type for each attachment to see if pdf is attached
}
} else {
// No attachment
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-25-2017 09:40 AM
Hi Dawid - thank you for the reply. That is a good point about the GlideAjax as it runs asynchronously as opposed to GlideRecords.
If I understand correctly your script above you are saying that content type of attachments is an extended field of sys_attachment ? Also in the script below you are treating the var gr as it was defined in my script, correct?
Does there need to be a ui policy/action in addition to this in your opinion?
Thank you in advance.
//glideajax or gliderecord call
if (gr.getRowCount() > 0){
// Attachment exist
while(gr.next()){
// check Content Type for each attachment to see if pdf is attached
}
} else {
// No attachment
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-25-2017 09:50 AM
Content type for pdf will be 'application/pdf'
You can use GlideAjax sync because onSubmit should wait for decision. It will have better performance on checking table with GlideRecord.
On GlideAjax you can check if attachment exist and if so, does any of existing attachments have content type as 'application/pdf', then you can return true/false to Ajax call.
OnSubmit is essential as trigger to check attachments. You can also do it on ui action but onSubmit is better considering its catalog item so only right items will be affected.
Summarizing:
OnSubmit with GlideAjax
GlideAjax with GlideRecord and logic to determine right file
GlideAjax returning true/false
OnSubmit checking return from GlideAjax and deciding to return false or continue
For application/pdf content, its better to double check by attaching any file on your instance and check what is in column Content Type
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-25-2017 10:40 AM
Thank you for the help Dawid.