UI Action to trigger confirm pop up under condition

SNAdmin47
Kilo Sage

I'm looking to use a confirm pop up box on a UI Action, but would prefer it if the confirm pop up box only appeared on condition of a dot-walked field value not being empty, and otherwise execute a basic save. The UI Action currently stands like the below, and works fine.... the confirm pop up box is triggered and if Yes is clicked then the later if statement is executed. If no is clicked then an alert pops up to say the form hasn't been saved. 

 

This works fine but is triggered every time the UI Action is clicked:

    function confirmChangeSubmission() {
            var answer = confirm('This will save the change, are you sure?');
            if (answer) {
                gsftSubmit(null, g_form.getFormElement(), 'sysverb_insert_and_stay');
            } else {
                alert('The form has not been saved');
            }
        } 

    if (typeof window == 'undefined') {
        current.insert();
        action.setRedirectURL(current);
        gs.include('ActionUtils');
        var au = new ActionUtils();
        au.postInsert(current);
    }

I've tried variations on the following with an if statement in the initial function called by the onClick but when clicked the form does nothing... understandably because it's a client script being asked to do a server query:

    function confirmChangeSubmission() {
        if (current.company.u_change_leadtime != '') {
            var answer = confirm('This will save the change, are you sure?');
            if (answer) {
                gsftSubmit(null, g_form.getFormElement(), 'sysverb_insert_and_stay');
            } else {
                alert('The form has not been saved');
            }
        } else {
            current.insert();
            action.setRedirectURL(current);
            gs.include('ActionUtils');
            var au = new ActionUtils();
            au.postInsert(current);
        }
    }

    if (typeof window == 'undefined') {
        current.insert();
        action.setRedirectURL(current);
        gs.include('ActionUtils');
        var au = new ActionUtils();
        au.postInsert(current);
    }

 

However, I'd prefer it if the popup box only appeared if the dot-walked field (company.u_change_leadtime) on the form was not empty. Is that possible, and if so, how? 

 

Thanks in advance for any insight on this. The only other option I can think of is to use different UI Actions to only show according to the value as the form is saved, but that would be far less graceful and far less reliable. 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@SNAdmin47 

you cannot dot walk like that in client side of UI action

You can use getReference with callback and check the value of dot walked field

    function confirmChangeSubmission() {

        var ref = g_form.getReference('company', callBackMethod);

        function callBackMethod(ref) {
            if (ref.u_change_leadtime != '') {
                var answer = confirm('This will save the change, are you sure?');
                if (answer) {
                    gsftSubmit(null, g_form.getFormElement(), 'sysverb_insert_and_stay');
                } else {
                    alert('The form has not been saved');
                }
            } else {
                // field is empty so no confirm directly server side
                gsftSubmit(null, g_form.getFormElement(), 'sysverb_insert_and_stay');
            }
        }
    }

    if (typeof window == 'undefined') {
        current.insert();
        action.setRedirectURL(current);
        gs.include('ActionUtils');
        var au = new ActionUtils();
        au.postInsert(current);
    }

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

@SNAdmin47 

you cannot dot walk like that in client side of UI action

You can use getReference with callback and check the value of dot walked field

    function confirmChangeSubmission() {

        var ref = g_form.getReference('company', callBackMethod);

        function callBackMethod(ref) {
            if (ref.u_change_leadtime != '') {
                var answer = confirm('This will save the change, are you sure?');
                if (answer) {
                    gsftSubmit(null, g_form.getFormElement(), 'sysverb_insert_and_stay');
                } else {
                    alert('The form has not been saved');
                }
            } else {
                // field is empty so no confirm directly server side
                gsftSubmit(null, g_form.getFormElement(), 'sysverb_insert_and_stay');
            }
        }
    }

    if (typeof window == 'undefined') {
        current.insert();
        action.setRedirectURL(current);
        gs.include('ActionUtils');
        var au = new ActionUtils();
        au.postInsert(current);
    }

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

SNAdmin47
Kilo Sage

Thanks @Ankur Bawiskar , that's awesome... just tested it and it works.

 

I was starting to wonder if I'd have to create a new UI page or some other more complicated and complex solution, but that's really graceful and one I'll be adding to my personal 'cheat sheet' for future. Thanks again, hope you have a great week. 😁

@SNAdmin47 

Glad to help

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader