g_form.setValue() fails inside getXML()

snowblind
Tera Expert

I'm forced to call global.AbstractAjaxProcessor to query some data from the client's onSubmit() handler.  The data returns okay, but I need to set a field value with the data to submit it.  setValue() isn't working inside getXML().  It sets the visible value in the field, but the original value gets sent with the submit.  There are no policies set.  Here's a synopsis of the code.  Any help will be appreciated.  

 

function onSubmit() {

	var ga = new GlideAjax('...');
	...
	ga.getXML(
		function (response) {
			var answer = response.responseXML.documentElement.getAttribute('answer');
			var stooges = JSON.parse(answer);	// Okay

			var payload = {
				'stooge1'		: stooges.moe,
				'stooge2'		: stooges.larry
			};

			var payload_string = JSON.stringify(payload);	// Okay
			
			g_form.clearValue('fieldname');
			g_form.setValue('fieldname',payload_string);	// Silent failure; original sent
		}
	);

	return true;
}

 

11 REPLIES 11

Bert_c1
Kilo Patron

In addition to Shivalika's response, how is 'fieldvalue' defined? add:

 

      alert('payload_string = ' + payload_string);
 
after setting that variable in the function. See what is returned from the script include.  Posting incomplete code rarely results in a solution, only suggestions on what to check as you have gotten here.

The field whose value I'm setting is 'Single Line Text' in the catalog form.

Medi C
Giga Sage

Hello @snowblind

 

The issue with your script is GlideAjax and getXML code likely stems from the fact that onSubmit() is executed synchronously, but getXML() is asynchronous. Let's break it down:

 

Key Issue: Asynchronous Execution of getXML()

  • onSubmit() executes when the form is submitted.

  • The ga.getXML() function is called, but it does not wait for the response before onSubmit() finishes.

  • The function inside getXML() is a callback that executes after the server responds, but the form submission does not wait for this to complete.

  • By the time g_form.setValue('fieldname', payload_string); executes inside the callback, the form has already submitted with the original value, causing a silent failure.

What Actually Happens

  1. onSubmit() runs.

  2. ga.getXML() sends an AJAX request.

  3. onSubmit() returns true, allowing the form to submit before getXML() completes.

  4. getXML() response arrives after the form is already submitted, so g_form.setValue() does not affect the submitted data.

To validate that your script is fine and returns the right value, please use "return false;" at the end to abort the submission and see if the value is set.

 

If the value is not returned and not set on your field, then you would need to double check your script include.

If the value is returned and set correctly and the form is not submitting, Then you can try validating if the value is returned by the time the form is about to be submitted using scratchpads.

function onSubmit() {
    if (g_scratchpad._ajaxChecked) {
        g_scratchpad._ajaxChecked = null;
        return true;
    }

    g_scratchpad._action = g_form.getActionName();
    g_scratchpad._ajaxChecked = false;

    var ga = new GlideAjax('...');
    ...
    ga.getXML(
        function(response) {
            var answer = response.responseXML.documentElement.getAttribute('answer');
            var stooges = JSON.parse(answer); // Okay

            var payload = {
                'stooge1': stooges.moe,
                'stooge2': stooges.larry
            };

            var payload_string = JSON.stringify(payload); // Okay

            g_form.clearValue('fieldname');
            g_form.setValue('fieldname', payload_string); // Silent failure; original sent

            g_scratchpad._ajaxChecked = true;
            if (typeof g_form.orderNow != 'undefined') {
                g_form.orderNow();
            } else {
                g_form.submit(g_scratchpad._action);
            }
        }
    );
    return false;
}

 

If it still doesn't work, please validate your script include function if it is returning the value as expected. Do gs.info to get more logs, and check the Browser Console if there are any errors thrown when submitting the form.

 

I hope this helps!

 

 


If you found this helpful, please hit the thumbs-up button and mark as correct. That helps others find their solutions.

That was a great explanation.  Thank you.  But, unfortunately, it landed me in an infinite loop, each iteration creating a request.  I assume g_form.submit() calls the onSubmit handler.

Juhi Poddar
Kilo Patron

Hello @snowblind 

Your issue is due to the asynchronous nature of getXML(). When you call ga.getXML(), the script continues executing and submits the form before the callback function finishes. Since the field value is updated inside the callback function, it happens after the form submission, causing the original value to be sent.

Solution:

  • You need to delay the form submission until after the getXML() call completes.
  • To do this, return false in onSubmit() and manually submit the form after updating the field.

Update Client Script: 

function onSubmit() {
    var ga = new GlideAjax('YourScriptIncludeName');
    ga.addParam('sysparm_name', 'YourFunctionName');
    ga.getXMLAnswer(function(response) {
        var stooges = JSON.parse(response);  // Ensure response is valid JSON

        var payload = {
            'stooge1': stooges.moe,
            'stooge2': stooges.larry
        };

        var payload_string = JSON.stringify(payload);

        g_form.setValue('fieldname', payload_string);

        // Manually submit after setting value
        g_form.submit();
    });

    // Prevent default submission
    return false;
}

Note:

  • getXMLAnswer() is used instead of getXML() to directly return the response value.
  • return false; prevents immediate form submission.
  • g_form.submit(); is called inside the callback after setting the value.

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"

 

Thank You
Juhi Poddar