I want to determine the return value of "getXMLAnswer" in the catalog client script! !

bonsai
Mega Sage

I'm developing a script that determines the value of a field in the catalog client script and makes the submission an error.

I would like to pass a parameter to the script include so that the existing record and the record to be created do not have the same value.

However, the client script "getXMLAnswer" did not return the return value to "test_response".

does anyone know how?

In the script include, judge based on the argument of "param_1" and return "true" or "false".

function onSubmit() {
    var ga = new GlideAjax('test_script');
    ga.addParam('sysparm_name', 'test');
    //Pass the status as an argument to see if it is duplicated
    ga.addParam('param_1', "status");

    //"true" if the status is duplicated
    //I expect it to return "false" if it's not a duplicate.
    var test_response = ga.getXMLAnswer(callback);

    //I want to determine and process the return value of "getXMLAnswer"! !
    if (test_response == false) {
        alert(response);
    }

}


function callback(response) {
    alert(response);
    return response;
}
1 ACCEPTED SOLUTION

@盆巣

As you are using getXMLWait() on portal which is no longer supported, you might have received an error message in your browser console. As for getXMLAnswer() or getXML() both are asynchronous in nature so they cannot validate/stop submission using onSubmit().

I found a knowledge article  How to do async validation in an onsubmit client script  , which might help in resolving your requirement. I tried to implement it, and it was working fine.

What you can do is create 2 onSubmit() Catalog Client Scripts one for Desktop and second for Mobile/Service Portal to tackle your requirement.

Desktop

function onSubmit() {
	
    var ga = new GlideAjax('test_script');
    ga.addParam('sysparm_name', 'test');

    ga.getXMLWait();
    var test_response = ga.getAnswer();

    if (test_response.toString() == 'false') {
		g_form.addInfoMessage('Response: ' + answer);
        g_form.addInfoMessage('Response Type: ' + typeof answer);
        return false;
    }

}

See the image for reference.

find_real_file.png

 

Mobile / Service Portal

function onSubmit() {

    if (g_scratchpad.isFormValid) {
        return true;
    }
	
    var actionName = g_form.getActionName();
	var ga = new GlideAjax('test_script');
    ga.addParam('sysparm_name', 'test');

    ga.getXML(function(response) {
		var answer = response.responseXML.documentElement.getAttribute('answer');
        g_form.addInfoMessage('Response: ' + answer);
        g_form.addInfoMessage('Response Type: ' + typeof answer);

        if (answer.toString() == 'false') {
            g_form.addErrorMessage('Cannot Submit');
			return false;
        } else {
            g_scratchpad.isFormValid = true;
            g_form.submit(actionName);
        }
    });
    return false;

}

See the image for reference.

find_real_file.png

 

Hopefully, this will help you resolve your query.

View solution in original post

15 REPLIES 15

@盆巣

As you are using getXMLWait() on portal which is no longer supported, you might have received an error message in your browser console. As for getXMLAnswer() or getXML() both are asynchronous in nature so they cannot validate/stop submission using onSubmit().

I found a knowledge article  How to do async validation in an onsubmit client script  , which might help in resolving your requirement. I tried to implement it, and it was working fine.

What you can do is create 2 onSubmit() Catalog Client Scripts one for Desktop and second for Mobile/Service Portal to tackle your requirement.

Desktop

function onSubmit() {
	
    var ga = new GlideAjax('test_script');
    ga.addParam('sysparm_name', 'test');

    ga.getXMLWait();
    var test_response = ga.getAnswer();

    if (test_response.toString() == 'false') {
		g_form.addInfoMessage('Response: ' + answer);
        g_form.addInfoMessage('Response Type: ' + typeof answer);
        return false;
    }

}

See the image for reference.

find_real_file.png

 

Mobile / Service Portal

function onSubmit() {

    if (g_scratchpad.isFormValid) {
        return true;
    }
	
    var actionName = g_form.getActionName();
	var ga = new GlideAjax('test_script');
    ga.addParam('sysparm_name', 'test');

    ga.getXML(function(response) {
		var answer = response.responseXML.documentElement.getAttribute('answer');
        g_form.addInfoMessage('Response: ' + answer);
        g_form.addInfoMessage('Response Type: ' + typeof answer);

        if (answer.toString() == 'false') {
            g_form.addErrorMessage('Cannot Submit');
			return false;
        } else {
            g_scratchpad.isFormValid = true;
            g_form.submit(actionName);
        }
    });
    return false;

}

See the image for reference.

find_real_file.png

 

Hopefully, this will help you resolve your query.

@盆巣 

Here are two more articles which might help you understand.

How to validate before onSubmit in Mobile/Service Portal

getXMLwait alternative for Service Portal

solved!
thank you!

Glad to know you solved it.

I have been banging my head against the wall for the better part of 2 days!!!!!! Works perfect! especially with MRVS variables!