- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-05-2023 05:01 AM
Im using Onsubmit client script which will run on portal form widget.
if (g_form.getActionName() == "sysverb_stay") {
if (g_form.getValue("stage") == "12") {
getMessage("please confirm you are at 'communication' stage and you are updating some values", function(msg) {
answer = confirm(msg);
confirmresponse(answer);
}
});
} else {
getMessage("Do you confirm that this is on your queue?", function(msg) {
answer = confirm(msg);
confirmresponse(answer);
});
}
}
function confirmresponse(answer) {
if (answer == 'false' || answer == false) {
return false;
}
else if ((answer == "true" || answer == true) && g_form.getActionName() == "sysverb_stay") {
g_form.setValue("state", "4");
g_form.save();
}
}
If i use the above code the record gets saved if i click on false in the confirm message.
I doesnt want the record to be saved when i click on False..
Im i doing something wrong..
Im using this on portal form widget
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2023 10:34 AM
I suppose I meant confirmation.
The idea is that you need to differentiate between two types of submit:
- the one initiated by the user
- the one triggered by your confirmation mechanism.
When the onSubmit is triggered by the user, you should initiate the getMessage calls and return false in order to cancel the submit. If you don't return false to cancel the submit, the submit will continue as if nothing happened. Because the getMessage calls are asynchronous and the callback functions that receive the translations will be executed after the submit process will have finished.
In the callback functions for the getMessage calls you should re-trigger submit if the user so answers.
When the onSubmit is triggered as a result of re-triggering the submit in the getMessage callback functions, you should no longer interfere.
Of course in order to differentiate between the onSubmit being initiated by the user or as a result of the confirmation callback re-triggering, you need to use flags. If the flag is NOT present, it will mean that the onSubmit has been triggered by the user. If the flag is present, it means that the onSubmit has been triggered by the getMessage callback functions.
You should make sure to clear the flag always when it is decided that the onSubmit has been triggered by the callback function. So that the user will be able to execute the onSubmit command as many times as needed.
What I'm trying to say is that because of the asynchronous nature of getMessage, when one presses the submit button, what is executed is this:
// Other submit logic of the system
if (g_form.getActionName() == "sysverb_stay") {
if (g_form.getValue("stage") == "12") {
getMessage("please confirm you are at 'communication' stage and you are updating some values", );
}
else {
getMessage("Do you confirm that this is on your queue?", );
}
}
// Other submit logic of the system
// In the end the submit of the form
As you can see, there is not return false there.
The rest of your onSubmit code will not have a chance to execute.
That is why you need to return false here already.
// Other submit logic of the system
if (g_form.getActionName() == "sysverb_stay") {
if (g_form.getValue("stage") == "12") {
getMessage("please confirm you are at 'communication' stage and you are updating some values", );
}
else {
getMessage("Do you confirm that this is on your queue?", );
}
return false;
}
Once you do that, the submit will be halted.
When the getMessage Promose is fulfilled, only than does the code where you think you return false execute.
So it will look something like:
function (msg) {
answer = confirm(msg);
confirmresponse(answer);
});
function confirmresponse (answer) {
if (answer == 'false' || answer == false) {
return false;
}
else {
if ((answer == "true" || answer == true) && g_form.getActionName() == "sysverb_stay") {
g_form.setValue("state", "4");
g_form.save();
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-05-2023 05:01 PM - edited 02-05-2023 05:01 PM
onSubmit only interrupts the submit process if one return false from that function.
You do not do that.
Note that getMessage is used asynchronously here, so the callback function (confirmresponse) will never be executed, because submitting will have finished by the time it is called.
You need a way more complex strategy something like:
In onSubmit check that verification has been done
- if verification has been done
- if the verification was successful, return true
- if the verification was successful, clear verification flag and return false
- if verifiction has not been done
- ask for confirmation AND RETURN FALSE
In the message response function for the confirmation message
- if the user wants to continue
- set verification flag to successful
- set the state value
- re-submit the form
It is the same phenomenon as described in article Asynchronous onSubmit Catalog/Client Scripts in ServiceNow on ServiceNow Pro Tips .
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2023 03:35 AM
Thanks for responding..
When you say verification, What should i verify? can you help me out here
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2023 10:34 AM
I suppose I meant confirmation.
The idea is that you need to differentiate between two types of submit:
- the one initiated by the user
- the one triggered by your confirmation mechanism.
When the onSubmit is triggered by the user, you should initiate the getMessage calls and return false in order to cancel the submit. If you don't return false to cancel the submit, the submit will continue as if nothing happened. Because the getMessage calls are asynchronous and the callback functions that receive the translations will be executed after the submit process will have finished.
In the callback functions for the getMessage calls you should re-trigger submit if the user so answers.
When the onSubmit is triggered as a result of re-triggering the submit in the getMessage callback functions, you should no longer interfere.
Of course in order to differentiate between the onSubmit being initiated by the user or as a result of the confirmation callback re-triggering, you need to use flags. If the flag is NOT present, it will mean that the onSubmit has been triggered by the user. If the flag is present, it means that the onSubmit has been triggered by the getMessage callback functions.
You should make sure to clear the flag always when it is decided that the onSubmit has been triggered by the callback function. So that the user will be able to execute the onSubmit command as many times as needed.
What I'm trying to say is that because of the asynchronous nature of getMessage, when one presses the submit button, what is executed is this:
// Other submit logic of the system
if (g_form.getActionName() == "sysverb_stay") {
if (g_form.getValue("stage") == "12") {
getMessage("please confirm you are at 'communication' stage and you are updating some values", );
}
else {
getMessage("Do you confirm that this is on your queue?", );
}
}
// Other submit logic of the system
// In the end the submit of the form
As you can see, there is not return false there.
The rest of your onSubmit code will not have a chance to execute.
That is why you need to return false here already.
// Other submit logic of the system
if (g_form.getActionName() == "sysverb_stay") {
if (g_form.getValue("stage") == "12") {
getMessage("please confirm you are at 'communication' stage and you are updating some values", );
}
else {
getMessage("Do you confirm that this is on your queue?", );
}
return false;
}
Once you do that, the submit will be halted.
When the getMessage Promose is fulfilled, only than does the code where you think you return false execute.
So it will look something like:
function (msg) {
answer = confirm(msg);
confirmresponse(answer);
});
function confirmresponse (answer) {
if (answer == 'false' || answer == false) {
return false;
}
else {
if ((answer == "true" || answer == true) && g_form.getActionName() == "sysverb_stay") {
g_form.setValue("state", "4");
g_form.save();
}
}
}