
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-11-2023 11:20 PM
Hi,
While trying to set a value for the variable using "onsubmit" client script. The alert is getting displayed but after the form is getting submitted. The value gets cleared.
The value has been set via GlideAjax function. But I noticed that when the value is been set outside the function, its getting stored for the variable.
Below is the client script on submit code:-
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-12-2023 12:13 AM
Hello @Community Alums ,
I have modified a script that I was using for a similar case, it involves some logic that I have found in the community, but I can not remember the name of the author, basically what you want to do is execute your onSubmit Client Script, execute your async logic, call submit again and return false.
That is done because the changes from the GlideAjax are going to be available to you after the form is submitted, so what you are seeing in the pop-up is the information you want, but the submission is done. So that is why in the callback function we call again the submission of the form, after we have set the data from the GlideAjax call and we need to cancel the first submission, because in the current iteration of the submit we don't have the data set.
function onSubmit() {
if (g_scratchpad._ajaxChecked) {
// We have run our Ajax Checks, so we can continue on
// and let our form submission continue
g_scratchpad._ajaxChecked = null;
return g_scratchpad._returnVal;
}
var currentchoice = g_form.getValue('application_service');
g_scratchpad._action = g_form.getActionName();
g_scratchpad._ajaxChecked = false;
var userdetails = new GlideAjax('Applicationservicechoices');
userdetails .addParam('sysparm_name', 'choicelistapp');
userdetails .addParam('sysparm_choice', currentchoice);
userdetails .getXMLAnswer(callback);
function callback(response) {
g_scratchpad._returnVal = true;
g_form.setValue('variable_details',response)
g_scratchpad._ajaxChecked = true;
if (typeof g_form.orderNow != 'undefined') {
// this is a catalog item
g_form.orderNow();
} else {
// this will resubmit the form using the saved
// ui action that was originally clicked
g_form.submit(g_scratchpad._action);
}
}
// always return false if we get to this point
return false;
}
If this helped you you can Accept the Solution and mark my answer as Helpful.
All the Best,
Stefan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-11-2023 11:33 PM
Hi @Community Alums ,
It is an asynchronous call so script ends before receiving the value, so field is submitted without being updated.
Try to use onchange client script or try to achieve or with before Business rule.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-11-2023 11:48 PM
Hi @Pavankumar_1 , The values can be set via only onsubmit. As we are declaring the variables randomly. So, On change will not work for us.
You suggested to use before business rule, how this can be used? Do I need to use a business rule rather than client script? or its different.
Can't we return the output outside the function and store the value in variable? Will this be not possible?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-11-2023 11:52 PM
Hi @Community Alums ,
is this is catalog item or normal form?
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-11-2023 11:54 PM
Its a catalog item