Issue with Saving Record Using g_modal.confirm in onSubmit Client Script

ao7
Tera Contributor

Hello everyone,

I am trying to show a modal using g_modal.confirm in an OnSubmit Client Script when the Save button is clicked. I want the record to be saved only if the user clicks the OK button in the modal.
However, when I press OK, nothing happens and the record is not saved.

Below is the script I am using. I referred to other scripts, but honestly, I do not fully understand the function(confirm) part and so on.

function onSubmit() {
    var dlgMessage = "XXXXXXXXXX";
    g_modal.confirm(getMessage("YYYYYYYYYYY"), dlgMessage, function(confirm) {
        if (confirm) {
            g_form.save();
        }
    });
    return false;
}

If there are any mistakes or improvements in my implementation, please let me know.

4 REPLIES 4

Shruti
Mega Sage
Mega Sage

Hi,

Write a onSubmit client script 

UI type - should be Mobile/Service Portal for workspace

 

function onSubmit() {

    var msg = getMessage("Are you sure you want to take this action?");
    g_modal.confirm(getMessage("Confirmation"), msg, function(confirmed) {
        if (!confirmed) {
            return false;
        }
    });


}

ChiragA11796440
Tera Contributor

function onSubmit() {
var dlgMessage = "XXXXXXXXXX";
g_modal.confirm(getMessage("YYYYYYYYYYY"), dlgMessage, function(confirm) {
if (confirm) {
return true;
}
});
return false;
}

AbinC
Tera Contributor

Hi @ao7 ,

 

var confirmed = false;

function onSubmit() {
if (confirmed) {
// If already confirmed, allow the save
return true;
}
// Show the confirmation dialog
g_modal.confirm(
getMessage("YYYYYYYYYYY"), // Title
"XXXXXXXXXX", // Message
function(result) {
if (result) {
confirmed = true;
g_form.save(); // This triggers onSubmit again, but now confirmed is true
}
}
);
// Prevent the form from saving until the user confirms
return false;
}

if you found above helpful please mark it helpful

 

Thanks,

Abin

ao7
Tera Contributor

Dear All,

Thank you for your advice!
The issue has been resolved using the script below, so I’d like to share it.

function onSubmit() {
    if (g_form.isNewRecord() || typeof g_scratchpad === 'undefined')
        return true;

    if (g_scratchpad.already_submitted)
        return true;
    
    var dlgMessage = "XXXXXXXXXX";
    g_modal.confirm(getMessage("YYYYYYYYYYY"), dlgMessage, function(confirm) {
        if (confirm) {
            g_scratchpad.already_submitted = true;
            g_form.save();
        }
    });
    return false;
}