Alternative way -> How To: Async GlideAjax in an onSubmit script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-19-2024 12:13 AM
The Aim of this post is to provide some kind of marker or a flag which can reveal us when a async operation is Completed.
The marker can be 1. g_scratchpad 2. A checkbox variable
In Context of The Solution from @Jon Barnes https://www.servicenow.com/community/developer-blog/how-to-async-glideajax-in-an-onsubmit-script/ba-... for the solution of How To: Async GlideAjax in an onSubmit script
THE PROBLEM STATEMENT :-
As per @Jon Barnes :-
As you probably already know, the only way to use GlideAjax in an onSubmit client script is to use getXMLWait and make a synchronous call to the server.
As you may also know, getXMLWait is flat-out not supported in Service Portal.
And so we have a problem. Many of us have use cases for checking something on the server when the user submits a form (or catalog request). But the above mutually exclusive facts have made it very difficult to create onSubmit scripts that do this and work for both native UI and Service Portal.
Solution --> The idea is to hold a property in a persistent object on the client side that tells our submit script if the GlideAjax function has completed and is successful. So here is a simple script that you can use to accomplish this using GlideAjax and g_scratchpad
This Solution is Awesome and it works well in service Portal !!
However an issue occurs using g_scratchpad in the native view , its not able to find the g_scratchpad and even if you decide to write a display business rule we know that display Business Rules on the catalog item view do not exist - https://www.servicenow.com/community/service-management-forum/g-scratchpad-not-working-on-native-ui-...
Here's my Alternate solution for the same inspired from the solution of @Jon Barnes , without using g_scratchpad as it throws error in the native view.
Below is my approach for above scenario which can work for both portal as well as native view.
so to tackle that we need another kind of marker that can run on both service portal as well as on the native view , here is the alternate solution without using g_scratchpad and without using display business rule :-
Use a Checkbox Field Instead of g_scratchpad
You can replace use of g_scratchpad with a checkbox field on the form. This will work in both the native UI and Service Portal.
Step 1: Create a Checkbox Field
Create a checkbox field on your catalog item form, for example, u_ajax_checked or any other name.
Step 2: Update Your onSubmit Script
Modify your script to use the checkbox field instead of g_scratchpad.
My original plan was to hide this field but the script wont work if this field is hidden.
Although in my case If you uncheck all the options the to make it visible the script will not work correctly , the from submission will not proceed , however when i again checked all the above option and renamed my variable's display name the field is now hidden and the script also works perfectly for me , maybe its a glitch , however even if you don't want to do it from variables properties you can still hide the field from the script also and that will work too.
function onSubmit() { // If we've already run the GlideAjax check, allow the form to submit if (g_form.getValue('u_ajax_checked') === 'true') { g_form.setValue('u_ajax_checked', 'false'); // Reset the flag for future submissions return true; } // Set the action name for later use and initialize the check flag var action = g_form.getActionName(); g_form.setValue('u_ajax_checked', 'false'); var requestedFor = g_form.getValue('u_requestedfor'); var ga = new GlideAjax('GetAssetDetails'); ga.addParam('sysparm_name', 'getAssetInfo'); ga.addParam('sysparm_sys_id', requestedFor); // Make the asynchronous GlideAjax call ga.getXMLAnswer(function(response) { if (response === 'true') { g_form.setVisible('u_justification', true); g_form.setMandatory('u_justification', true); var justification = g_form.getValue('u_justification'); if (!justification) { g_form.addErrorMessage('Please provide a reason for requesting a new laptop during the warranty period.'); return; // Stop here, do not proceed with submission } } // Set the flag indicating the check passed, and re-trigger form submission g_form.setValue('u_ajax_checked', 'true'); if (typeof g_form.orderNow != 'undefined') { // For catalog items g_form.orderNow(); } else { // For standard forms, re-submit with the saved action g_form.submit(action); } }); // Prevent immediate form submission, as we are handling it asynchronously return false; }
The Above approach worked for me and i hope it works for your use case as well !!
This way in my opinion the above solution will work both for Service portal as well as for the native view.
Let me know if it works for you !!
Also checkout, my solution on How to Redirect a user from a catalog Item form to servicePortal homepage from using a confirmation box in a catalog client script. --> https://www.servicenow.com/community/community-central-forum/a-way-to-redirect-to-serviceportal-home...