Why is g_scratchpad used in onSubmit Catalogue Client Script?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
I've just been looking at the onSubmit below Catalogue client Script:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
that's correct it doesn't explain
it's just a workaround many developers use to handle sync GlideAjax in portal and scoped app
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @Ankur Bawiskar So we don't know why scratchpad is used in a client script?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
@matthew_hughes , g_scratchpad isn't any catalog variable or a script variable to be declared in client scripts.
It's basically used to fetch the data from Server side especially from Display Business Rules.
I am sure in your script - g_scratchpad.isFormValid is already declared in your business rules to fetch some value and then pass it into your Onsubmit Client script.
To learn more about scratchpad , you may go through these docs, articles to get a better understanding -
https://www.servicenow.com/community/developer-forum/what-is-scratchpad/m-p/2118432
https://www.servicenow.com/community/developer-forum/explain-scratchpad/m-p/2256541
If my response has helped you, kindly mark it as helpful and accept the solution.
Regards,
Nayan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hello @matthew_hughes ,
Generally g_scratchpad is used to pass data from server side to client side using Display BR . Im not getting why you are using g_scratchpad as well as glideajax both at time .GlideAjax is also used to fetch server side data to client side using Script include . g_scratchpad is a built-in object provided by ServiceNow, so you don’t need to declare it in the catalog client script. As per my understanding ,it is automatically available on the client side and is commonly used to temporarily store values while a form is open but you need to declare it in display BR means you just need to initialized it in BR. Then you can call that g_scratchpad property in client script .
Im not sure but here based on you glideajax g_scratchpad property value is getting set . is it working ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @matthew_hughes ,
Hope you are doing well.
Instead you may use the below code snippet that may work for you .
function onSubmit() {
// 1. Check if we already validated in this session
if (window.isFormValid) {
window.isFormValid = false; // Reset for next time
return true;
}
// 2. Prevent submission immediately to wait for Ajax
var gaProperty = new GlideAjax('sn_apm_di.LBGDigitalInterfacesURLUtils');
gaProperty.addParam('sysparm_name', 'getSystemProperty');
gaProperty.addParam('sysparm_propertyName', 'sn_apm_manage_bus_app_fields_catalog_item_checkbox_names');
gaProperty.getXMLAnswer(function(answer) {
var aryCheckboxes = answer.split(",");
var blnSelectionMade = false;
for (var i = 0; i < aryCheckboxes.length; i++) {
if (g_form.getBooleanValue(aryCheckboxes[i].trim())) {
blnSelectionMade = true;
break;
}
}
if (blnSelectionMade) {
// 3. Mark as valid and trigger submission again
window.isFormValid = true;
// Use g_form.submit() for Native UI or orderNow for Portal
if (typeof g_form.orderNow === 'function') {
g_form.orderNow();
} else {
g_form.submit();
}
} else {
g_form.addErrorMessage('It looks like you have not changed any data. Please select a data point to update it.');
}
});
return false; // Stop the initial submission
}check that above code I added some flag in-between the code to make it easy for you to understand the execution.
If the solution is useful for you please mark it as helpful and accept the solution to close the thread.
Regards,
Sagnic
