- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on ‎06-26-2017 01:02 AM
So a little background. I have a Scope Application which has a catalog item. I wanted the ability to verify a file was attached prior to submission. The problem is, most all solutions proposed do not work in scoped applications...
Here are the current solutions you could use if you were not in a scoped application:
1) You could use GlideAjax and do a getXMLWait(), then check the answer and return false, example:
var ga = new GlideAjax('MyAttachmentUtils');
ga.addParam('sysparm_name', 'hasAttachment');
ga.addParam('sysparm_data', g_form.getValue('sysparm_item_guid')); // you can't use getUniqueValue, it doesn't map to what is in the attachment table
ga.getXMLWait();
if (ga.getAnswer() == 'false') {
g_form.addErrorMessage('You must attach something');
return false;
}
All fine and dandy (if you are ok with using getXMLWait)...but, of course, getXMLWait is NOT available in scoped applications (makes sense, they don't want apps locking up the browser...
So...on to option 2
2) You could move your code from your script include directly into the client script...Then use GlideRecord and do the search in the client script!
var gr = new GlideRecord('sys_attachment');
gr.addQuery('table', 'my_table');
gr.addQuery('table_sys_id', g_form.getValue('sysparm_item_guid'));
gr.query();
if (!gr.next) {
g_form.addErrorMessage('You must attach something');
return false;
}
All fine and dandy (if you are fine with calling server side code in a client...yuck!)...But, of course, GlideRecord is NOT available in a scoped application client script! Adding global. doesn't fix it either. But wait, there is a workaround for this.. option 2a...
2a) Take the above script and move it to the 'global' application, then it works. The major downside is that this is NOT included in your application and won't promote properly. You'll have to install a global update set with the scoped app...this kinda breaks the whole idea of having a scoped app....so not a good solution, but it technically works...there has to be something better....
So how do we do it?
I love async calls and I love using script includes to handle server code. So I decided to always return false on the onSubmit. Then, in the ajax callback function, I submit the form again if the validation is successful. However, this is easier said than done...
First, g_form.submit() doesn't work on catalog items (too easy, darn it!). It'll give you an error saying use g_form.orderNow()...which doesn't work either!
Second, use good old DOM manipulation (even though SN doesn't recommend it) document.getElementById('submit_button').submit() ...oops, Scoped Apps don't allow access to the DOM! Thwarted again!
Now what? Well, it can be done, here's how:
1) Create a variable on your form called 'submitted', I just made it a single line text with '' by default. This will prevent recursion.
2) Hide that variable using a ui policy
3) Create your onSubmit client script, but instead of the normal ways mentioned above, get the HTMLElement and use .click()...
function onSubmit() {
if (g_form.getValue('submitted') == 'yes') // this prevents recursion
return true;
var ga = new GlideAjax('MyAttachmentUtils');
ga.addParam('sysparm_name', 'hasAttachment');
ga.addParam('sysparm_table', 'x_your_scoped_table_name');
ga.addParam('sysparm_id', g_form.getValue('sysparm_item_guid')); //don't use getUniqueValue, it's bogus if there is an attachment and this hasn't been submitted
ga.getXMLAnswer(function(response) {
g_form.clearMessages();
if (response == 'false')
g_form.addErrorMessage('You must attach a Statement of Work'); // no need to do anything else, false was already returned
else {
g_form.setValue('submitted', 'yes'); // set the hidden value to prevent recursion
g_form.getControl('submit_button').click(); // use HTMLElement to grab the button, then use click()
}
});
return false;
}
So there you have it. Took me several hours of surfing the community and trial and error before I figured a way around all the roadblocks SN created in Scoped Apps. I'm hoping they don't 'fix' this solution...unless, of course, they give us a better solution to do data validation in scope apps onSubmit!
- 6,864 Views
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi @akaupisch
I am trying to achieve exactly the same thing but no luck.
alert('but : ' + g_form.getControl('form_action_button'));
alert('but : ' + g_form.getControl('submit_button'));
I am on kingston release but both above statements are returning 'undefined'. Any idea?

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Looks like this script was originally written while trying to solve the problem on a record producer and not a catalog item. Record producers have a Submit button. Catalog items have the multiple shopping cart buttons.
Since shopping carts have multiple ways you can 'submit the form', you'll need to account for all of them in your code.
The 'Order Now' button is named oi_order_now_button in the DOM.
The 'Add to Cart' button is named oi_add_to_cart_button in the DOM.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Great--thank you!
I made a slight tweak. Instead of making a variable to hold "submitted" i just added it to the g_form object to stop recursion without having to add a special variable, like this. (I like using "yes" to avoid false falsey responses).
if (g_form.submitted == 'yes') return true;
...
g_form.submitted = 'yes';

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Thank you for this 🙂
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Thank you this helps 🙂
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Thank you so much for this! so much pain to go through when using scoped app for something as simple as doing GlideRecord or XMLWait(). I went through the same journey as you explained!