help with using confirm prompt in desktop interface for catalog item

Austin27
Tera Contributor

Hi Community. I'm trying to provide another confirm prompt to the end user to make sure that is who they want to remove. When using the catalog item in the desktop type interface (also called technician interface), I can click cancel and receive the error message code that I canceled. However, when I click OK the catalog item does order. I've tried using g_form.submit() but it says I should use g_form.orderNow(). I tried the orderNow() and that method doesn't proceed either. It acts like nothing happens. Not sure what I am missing.  Any thoughts or suggestions?

function onSubmit() {
    // on a remove
    if (g_form.getValue('add_or_remove') == 'remove') {

        // Prevent people from kicking themselves out of the group.
        if (g_form.getValue('requested_for') == g_user.userID) {
            g_form.addErrorMessage('You cannot use this form to remove your own access');
            return false;
        }

        try {
            var encodedQuery = 'sys_idIN' + g_form.getValue('requested_for');

            var ga = new GlideAjax('u_ClientUtil');
            ga.addParam('sysparm_name', 'getQueryResult');
            ga.addParam('sysparm_table', 'sys_user');
            ga.addParam('sysparm_columns', 'name');
            ga.addParam('sysparm_encodedQuery', encodedQuery);

            ga.getXMLAnswer(handleConfirmation);
            // Prevent form submission until confirmation is handled
            return false;

        } catch (error) {
            g_form.addErrorMessage('Error in GlideAjax call:', error);
            return false;
        }
    }

    // If not a removal request, allow form submission
    return true;
}

function handleConfirmation(response) {
    try {
        var answer = response;

        if (answer) {
            var aryAnswer = JSON.parse(answer);
            var name = aryAnswer[0].name;

            // Confirm action
            if (!confirm("Please confirm that you want to remove access from the following user: " + name + '.')) {
                // If user cancels, prevent the form from being submitted
                g_form.addErrorMessage('Action canceled by the user.');
				return false;
            }
            return true;

        } else {
            g_form.addErrorMessage('Failed to retrieve user information.');
        }

    } catch (error) {
        g_form.addErrorMessage('Error in handleConfirmation function:', error);
    }
}

 

Thank you.

Austin

 

 

1 ACCEPTED SOLUTION

Add alert lines to the client script to confirm the response from the SI, and see which if statements are satisfied with each test case - so you will see the path the script is taking. An alternate way of handling the confirm that I have used looks like this:

// Confirm action
var usrinput = confirm("Please confirm that you want to remove access from the following user: " + name + '.'))
// If user cancels, prevent the form from being submitted
if (usrinput == false) {
    g_form.addErrorMessage('Action canceled by the user.');
    return false;
}

You don't ever have to 'return true' in an onSubmit script as that is what will happen unless you return false.

View solution in original post

4 REPLIES 4

Brad Bowman
Kilo Patron
Kilo Patron

Hi Austin,

The return false after the ga.getXMLAnswer is preventing this request from being submitted.

If I remove the return false, the form is submitted either if the user selects ok or cancel from the prompt. It seems to only for canceling and not clicking ok, no matter which way I try. 

Add alert lines to the client script to confirm the response from the SI, and see which if statements are satisfied with each test case - so you will see the path the script is taking. An alternate way of handling the confirm that I have used looks like this:

// Confirm action
var usrinput = confirm("Please confirm that you want to remove access from the following user: " + name + '.'))
// If user cancels, prevent the form from being submitted
if (usrinput == false) {
    g_form.addErrorMessage('Action canceled by the user.');
    return false;
}

You don't ever have to 'return true' in an onSubmit script as that is what will happen unless you return false.

Najmuddin Mohd
Mega Sage

HI @Austin27 ,

I had seen similar issues when using GlideAjax calls in onSubmit() client scripts.

There are couple of solutions I have seen.

1. https://www.servicenow.com/community/itsm-forum/glide-ajax-not-working-on-submit-client-script/m-p/2...

2. https://www.youtube.com/watch?v=Sq5GA4BlXlc

I have used second solution and it worked for me.

If this information helps you, kindly mark this post as Helpful.

Regards,
Najmuddin.