The CreatorCon Call for Content is officially open! Get started here.

How can I use Glide Ajax in Client callable UI function

Prathamesh Cha1
Tera Contributor

after clicking the button called Request Extension I am trying to fetch assigned_to manager in approver user field, but showing me error called take ur function outside of the root function.

 

function onButtonClick() {

    //Set the Requested New Due Date and the Business Justification fields mandatory and editable, onclick of the button
    g_form.setReadOnly("expected_start", false);
    g_form.setReadOnly("u_sla_breach_reason", false);
    g_form.setReadOnly("assignment_group", false);
    g_form.setMandatory('expected_start', true);
    g_form.setMandatory('u_sla_breach_reason', true);
    g_form.setMandatory("assignment_group", true);

    //set approver based on extension count
    if(g_form.getValue("u_extension_count") == 0){

        g_form.setValue("approver_type", "1", "User");
        var regUser = new GlideAjax('PP_RegChangeUtils');
            regUser.addParam('sysparm_name', 'getAssignedToManager');
            regUser.addParam('sysparm_assignTo', g_form.getValue("assigned_to"));
            regUser.getXML(gotManager);

        function gotManager(response) {
            var answer = response.responseXML.documentElement.getAttribute("answer");
            g_form.setValue("approver_user", answer)
             }

    }else if(g_form.getValue("u_extension_count") == 1 || g_form.getValue("u_extension_count") == 2){

        g_form.setValue("approver_type", "2", "Group");
        g_form.setValue("approver_group", "86a498f81bab4a108430ca26604bcbe2", "PayPal IRM - Regulatory Change Extension Approver L1");

    }else if(g_form.getValue("u_extension_count") > 2){

        g_form.setValue("approver_type", "2", "Group");
        g_form.setValue("approver_group", "256550301beb4a108430ca26604bcb7b", "PayPal IRM - Regulatory Change Extension Approver L2");
    }

    //The Requested New Due Date and the Business Justification fields should not be empty
    if ((g_form.getValue('expected_start') == "") || (g_form.getValue('u_sla_breach_reason') == "") || (g_form.getValue('assignment_group') == "")) {
        g_form.addErrorMessage('Assignment Group, Extended Regulatory Change Task Due Date and Extension Justification are required');
        return false;
    }

    gsftSubmit(null, g_form.getFormElement(), 'rct_extension');
}

if (typeof window == 'undefined') {
    updateFields();
}

//Update the Regulatory Change Task form
function updateFields() {
    current.update();
    action.setRedirectURL(current);
}


3 REPLIES 3

AnveshKumar M
Tera Sage
Tera Sage

Hi @Prathamesh Cha1 

 

It's might be because of the callback function. And moreover getXMLAnswer makes an Async call, this might cause form to be submitted even before the response is back. You can try using the getXMLWait which makes synchronus call. Try the below code.

 

function onButtonClick() {

    //Set the Requested New Due Date and the Business Justification fields mandatory and editable, onclick of the button
    g_form.setReadOnly("expected_start", false);
    g_form.setReadOnly("u_sla_breach_reason", false);
    g_form.setReadOnly("assignment_group", false);
    g_form.setMandatory('expected_start', true);
    g_form.setMandatory('u_sla_breach_reason', true);
    g_form.setMandatory("assignment_group", true);

    //set approver based on extension count
    if(g_form.getValue("u_extension_count") == 0){

        g_form.setValue("approver_type", "1", "User");
        var regUser = new GlideAjax('PP_RegChangeUtils');
            regUser.addParam('sysparm_name', 'getAssignedToManager');
            regUser.addParam('sysparm_assignTo', g_form.getValue("assigned_to"));
            regUser.getXMLWait();
			var answer_ga = ga.getAnswer()
			g_form.setValue("approver_user", answer_ga)

    }else if(g_form.getValue("u_extension_count") == 1 || g_form.getValue("u_extension_count") == 2){

        g_form.setValue("approver_type", "2", "Group");
        g_form.setValue("approver_group", "86a498f81bab4a108430ca26604bcbe2", "PayPal IRM - Regulatory Change Extension Approver L1");

    }else if(g_form.getValue("u_extension_count") > 2){

        g_form.setValue("approver_type", "2", "Group");
        g_form.setValue("approver_group", "256550301beb4a108430ca26604bcb7b", "PayPal IRM - Regulatory Change Extension Approver L2");
    }

    //The Requested New Due Date and the Business Justification fields should not be empty
    if ((g_form.getValue('expected_start') == "") || (g_form.getValue('u_sla_breach_reason') == "") || (g_form.getValue('assignment_group') == "")) {
        g_form.addErrorMessage('Assignment Group, Extended Regulatory Change Task Due Date and Extension Justification are required');
        return false;
    }

    gsftSubmit(null, g_form.getFormElement(), 'rct_extension');
}

if (typeof window == 'undefined') {
    updateFields();
}

//Update the Regulatory Change Task form
function updateFields() {
    current.update();
    action.setRedirectURL(current);
}

 

Let me know, if you need any more help on this.

 

Thanks,
Anvesh

Hi @AnveshKumar M ,

 

thanks but its not working, there is error message "Error Message Access to getXMLWait is not available in scoped applications." also tried with line "

var answer_ga = ga.getAnswer()

"

 

also tried with "

var answer_ga = regUser.getAnswer()

"

Bert_c1
Kilo Patron

Here's a simple example of using GlideAjax in a UI action. Should work in a scoped app:

 

UI Action script:

 

function onClick() {
	g_form.getValue("assigned_to");

	var ga = new GlideAjax('GetUserManager');
	ga.addParam('sysparm_name', 'getAssignedToManager');
	ga.addParam('sysparm_assignTo', g_form.getValue("assigned_to"));
	ga.getXMLAnswer(getResponse);

	// callback function for returning the result from the script include
	function getResponse(response) {
		alert('Manager: ' + response);
	}
}

Script include:

var GetUserManager = Class.create();
GetUserManager.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	getAssignedToManager: function() {
        var userID = this.getParameter("sysparm_assignTo");
		var user = new GlideRecord('sys_user');
		user.addQuery('sys_id', userID);
		user.query();
		gs.info("GetUserManager: Found " + user.getRowCount() + " user records.");
		if (user.next()) {
			result = user.manager.toString();
		}
		else {
			result = "Unknown";
		}
		gs.info("GetUserManager: Returning string: " + result);
		return JSON.stringify(result);
    },

    type: 'XUserDetailsAjax'
});

Add your additional UI action logic as you show.