- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-26-2016 09:31 AM
Hello experts,
How do I have a mandatory check for attachment via ui action? I want the user to submit an attachment before clicking 'submit'. If no 'attachment' then, alert user to add an attachment.
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-26-2016 11:23 AM
You can either do this in the client script or via a business rule. With a business rule you can have it fire on a certain state change or condition and query for the attachment. So create a before, advanced business rule and the following example script should work for you:
var attachRec = new GlideRecord("sys_attachment");
attachRec.addQuery("table_name", current.getTableName());
attachRec.addQuery("table_sys_id", current.sys_id);
attachRec.query();
if (!attachRec.next()) {
gs.addErrorMessage("You must attach a complete import template before submitting.");
current.setAbortAction(true);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-26-2016 12:24 PM
I modified the code a little, and used it in the UI action. Works as expected. Thanks for your help!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-26-2016 09:37 AM
Hi James,
Please refer below link and adjust it as per your req.
Mandatory Attachment Before State Change
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-26-2016 11:22 AM
Hi Pradeep,
Is this possible to do via UI Action and not Client Script? I was hoping the functionality can be contained within the UI Action button, so it can be used at a different state, rather than onSubmit. the workflow is also driven through UI Action, so current.state = 5 needs to go to 10.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-26-2016 09:42 AM
Hi James,
I would follow below code I have used before. The two options above are using gliderecord via client which is not best practice. Below code is using client script to run ajax call to server side to see if catalog item the user is submitting has an attachment.
Is this check for Catalog item or just a form record?
If check is for a form record then just update the query in line 14-15 to search for table vs cat item.
/* create script include with the following info:
Name == AJAXcheckAttachment
Client Calliable = true
Description == ajax class to be called from catalog item client side to check and see if an attachment has been added to item before user submits order.
Code below
*/
var AJAXcheckAttachment = Class.create();
AJAXcheckAttachment.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkAttachment: function() {
var cat_id = this.getParameter('cat_id');
var gr = new GlideRecord("sys_attachment");
//gr.addQuery("table_name", "sc_cart_item");
gr.addQuery("table_sys_id", cat_id);
gr.query();
if (!gr.next()) {
return false;
}
},
type: 'AJAXcheckAttachment'
});
/* Client side code with the following info:
Name == checkAttachment
Type == onSubmit
code below
*/
function onSubmit() {
//Type appropriate comment here, and begin script below
if(g_form.getValue('floor_tiles') == 'Yes'){ //<-- Custom checker for customer needs Change for new
var cat_id = gel('sysparm_item_guid').value;
var ga = new GlideAjax('AJAXcheckAttachment');
ga.addParam('sysparm_name','checkAttachment');
ga.addParam('cat_id', cat_id);
ga.getXMLWait();
var check = ga.getAnswer();
if (check == 'false'){
g_form.hideFieldMsg('floor_tiles_attachment_info');
g_form.showFieldMsg('floor_tiles_attachment_info','A Cut Sheet will need to be attached to submit this order.','error');
return false;
}
}
}