Client Script onSubmit (Mandatory Attachment)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-07-2022 11:23 PM
Hi I'm currently working on restricting a form submission so attachment must be made mandatory on a particular workflow. As per attached screenshots, the alert message should ONLY display when chart of accounts -> natural account is selected, but it seems to be appearing across all the other drop down values for eg, accounts payable -> external interfaces. Can someone please help me with?
Note that Natural Account only populates after Chart of Accounts is selected, simultaneously.
Here is my catalog client script (onSubmit):
function onSubmit() {
//Type appropriate comment here, and begin script below
var catItem = g_form.getValue("Chart_of_Accounts");
try {
if (catItem == 'Natural Accounts') {
var cat_id = g_form.getParameter("sysparm_item_guid");
var gr = new GlideRecord("sys_attachment");
gr.addQuery("table_name", "sc_cart_item");
gr.addQuery("table_sys_id", cat_id);
gr.query();
if (!gr.hasNext()) {
alert("You must use the latest 'NEW and MODIFICATION NAT ACCOUNT TEMPLATE'");
return false;
}
}
} catch (e) {
g_form.addErrorMessage(e);
return false;
}
}
AND on SP view:
function onSubmit() {
//Type appropriate comment here, and begin script below
var condition = g_form.getValue('Chart_of_Accounts');
if(condition == 'Natural Accounts') {
if(this.document.getElementByClassName('get-attachment').length == 0) {
g_form.addErrorMessage('You must use the latest "NEW and MODIFICATION NAT ACCOUNT TEMPLATE"');
return false;
}
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-07-2022 11:53 PM
Hi,
Even though the "Chart of Accounts" variable is hidden for "Accounts payable" client script will return it. You can update your condition to check whether "what is your query about?" is "Chart of Accounts" and "Chart of Account" variable has value "Natural Accounts". Refer the updated code as below. Update SP code with similar condition
function onSubmit() {
//Type appropriate comment here, and begin script below
var varQuery = g_form.getValue("what_is_your_query_about"); // Replace this variable name with actual variable
var catItem = g_form.getValue("Chart_of_Accounts");
try {
Update Chart of Accounts in below code with actual value you expect in the variable
if (varQuery == "Chart of Accounts" && catItem == 'Natural Accounts') {
var cat_id = g_form.getParameter("sysparm_item_guid");
var gr = new GlideRecord("sys_attachment");
gr.addQuery("table_name", "sc_cart_item");
gr.addQuery("table_sys_id", cat_id);
gr.query();
if (!gr.hasNext()) {
alert("You must use the latest 'NEW and MODIFICATION NAT ACCOUNT TEMPLATE'");
return false;
}
}
} catch (e) {
g_form.addErrorMessage(e);
return false;
}
}
Palani