why confirm() is not working in ui action button

Jithender123
Tera Contributor

I am trying to create a button on incident table named password reset, for this I need a confirm box saying "are you sure you want to reset password" (yes/no). but unfortunately when I use confirm(), its not saving.

 

Please assist.

16 REPLIES 16

Neeraj31
Mega Sage

@Jithender123 

To save on server side, you have to write server side code in UI action. For that you need to add below "if block" of code in your UI action.

 

 

function reset_password() {
    var con = confirm('Are you sure');
    if(con){
        gsftSubmit(null, g_form.getFormElement(), 'reset_password');
    }
}

if(typeof window == 'undefined') {
   // Add Server side logic Eg: I have changed the short_description and then update
    current.short_description = 'Test';
    current.update();
}

 

 

 

Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

sunil maddheshi
Tera Guru

@Jithender123 

Write client side UI action and call script include from it, below is the sample code:

UI action: 

function confirmResetPassword() {
    var answer = confirm("Are you sure you want to reset the password?");
    if (answer) {
        // Call a Script Include or trigger server-side logic
        var ga = new GlideAjax('PasswordResetScriptInclude');
        ga.addParam('sysparm_name', 'resetPassword');
        ga.addParam('sysparm_sys_id', g_form.getUniqueValue());
        ga.getXMLAnswer(function(response) {
            alert(response); // Show success or error message
        });
    }
}
confirmResetPassword();

Script inlcude:

var PasswordResetScriptInclude = Class.create();
PasswordResetScriptInclude.prototype = {
    initialize: function() {},
    
    resetPassword: function() {
        var sys_id = gs.getParameter('sysparm_sys_id');
        if (!sys_id) return "Invalid record";

        var inc = new GlideRecord('incident');
        if (inc.get(sys_id)) {
            return "Password reset request submitted successfully.";
        }
        return "Incident not found.";
    },
    
    type: 'PasswordResetScriptInclude'
};

 

Please mark correct/helpful if this helps you!