Built something you're proud of? Tell the story. A quick G2 review of App Engine or Build Agent helps other developers see what's possible on ServiceNow. Share your experience.

Need a way to close g_modal.showFrame based on API results

Raksha5
Tera Contributor

This is my requirement.
There is a UI Action on HR Agent Workspace. When clicked, it invokes a UI Page (which has CSS/HTML for a custom loader) as a popup using g_modal.showFrame. The UI Action proceeds to do a GlideAjax and calls an API and gets the result. If API is successful, then the modal must be destroyed and form has to reload. If unsuccessful, the modal must be destroyed and an error message should be displayed.

Need help on below 2 points:
1. Close the modal automatically

2. Remove the white background in the modal.

 

Below is the code snippet:
UI Action Workspace script:

function agentAssist() {
    var caseSysId = g_form.getUniqueValue();
    var ui_page_id = <sys_id of UI Page>;
    g_modal.showFrame({
        url: '/ui_page.do?sys_id=' + ui_page_id + '&caseid=' + caseSysId,
        size: 'sm',
        autoCloseOn: 'EMPTY_BODY', //Tried URL_CHANGED as well. No luck
        showClose: false
    });

    var ga = new GlideAjax('<SI>');
    ga.addParam('sysparm_name', '<function>');
    ga.addParam('sysparm_sys_id', caseSysId);
    ga.getXML(function(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
       
        if (answer == 'Success') {
            location.reload();
            //window.top.g_modal.destroy();
        } else {
            //window.top.g_modal.destroy();
            g_form.addErrorMessage('API failed. Please contact Administrator');
        }
    });
}
3 REPLIES 3

Ankur Bawiskar
Tera Patron

@Raksha5 

try this

function agentAssist() {
    var caseSysId = g_form.getUniqueValue();
    var ui_page_id = '<sys_id>';

    var modal = g_modal.showFrame({
        url: '/ui_page.do?sys_id=' + ui_page_id + '&caseid=' + caseSysId,
        size: 'sm',
        showClose: false
    });

    var ga = new GlideAjax('<SI>');
    ga.addParam('sysparm_name', '<function>');
    ga.addParam('sysparm_sys_id', caseSysId);

    ga.getXML(function(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");

        //  Close modal
        if (modal) {
            modal.destroy();
        }

        if (answer == 'Success') {
            location.reload();
        } else {
            g_form.addErrorMessage('API failed. Please contact Administrator');
        }
    });
}

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

modal.destroy();

this did not work. any other methods available?

modal.destroy();

This did not work. Are there any other methods?