- 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-12-2023 12:08 AM
Hi @Community Alums ,
you can use before create or update business rule on sc_req_item table and give condition as Item as your catalog item(Item is reference field available on RITM table).
use
current.variables.start_date //you can get catalog variable values like this and set values using before update or create Business rule
ServiceNow Community MVP 2024.
Thanks,
Pavankumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-12-2023 12:05 AM
Your code requires value from only one variable 'application_service'. So if you add it as an onChange client script on 'application_service' then it should work as expected.
- 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-14-2023 02:16 AM
Hi @Stefan Georgiev ,
The script which you shared it worked after small changes 🎉. Just I want to know why "_ajaxChecked " did not checked for other conditions that is after entered into the callback function and can we call the g_scratchpad in the client side scripting? and is there any negatives when we call at client side?
Thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-14-2023 04:43 AM
Hello @Community Alums ,
the script that I provided was a bit sketchy because I was in a hurry, I just modified some things that I was using and there were some code leftovers that forgot to remove, my case was different.
"_ajaxChecked ", is just a name that is used for the scratchpad, you can change it, I might have just used the name that I found in the article that I used for my implementation.
g_scratchpad is just an object that you can pass values with, you can try login it in your catalog client script
You might try passing the data with the g_form, but i need to test that, not sure if it is going to work.
There should not be any negative effects for using the g_scratchpad, chances for errors even if you dont clean your data from it are really small, and something to work with the scratchpad and to have the same name. Clear your data and there are not going to be any problems. And if you are talking for performance this is just one transaction, it is not going to impact the performance.
Hope that this helps you!
If the provided information answers your question, please consider marking it as Helpful and Accepting the Solution so other community users can find it faster.
All the Best,
Stefan