Not getting the answer from Script Include in onSubmit Catalog Client Script by using GlideAjax

Ankit Balapure
Tera Contributor

Hi, 

Can anyone tell me that where am I going wrong. I am getting the null answer. Please refer the below scripts and screenshots.

 

Script Include: 

AnkitBalapure_1-1747054808480.png

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

    checkAuthorization : function(){

		var userId = this.getParameter('sysparm_user_id');

		
		return "userId";

	},


    type: 'UserAuthorizationToFormSubmission'
});

 

 

Client Script:

AnkitBalapure_2-1747054926454.png

function onSubmit() {
    var current_user = g_user.userID;

    var auth_stat = new GlideAjax('UserAuthorizationToFormSubmission');
    auth_stat.addParam('sysparm_user_id', current_user);
    auth_stat.addParam('sysparm_name', 'checkAuthorization');
    auth_stat.getXMLWait();
    alert(auth_stat.getAnswer());

}

 

Output: 

AnkitBalapure_0-1747054736188.png

 

Please suggest why this is happening if I am trying to get the value from the script include which is client callable and called from the above mentioned client script by using the 'getXMLWait()' method.

 

Thanks,

Ankit

 

 

 

10 REPLIES 10

OlaN
Giga Sage
Giga Sage

Hi,

The issue is that you are doing this in an onSubmit client script.

The GlideAjax function is running async, so by the time when the script has done the processing, the onSubmit script is already completed.

That's why you get a Null answer, the data is still not returned, and the value not defined.

 

You should switch to using an onChange client script to have the processing be completed before the submit happens.

Also, don't use "getXMLWait()" you should explore the getXMLAnswer() instead.
Mark Roethof has written a great article on it, you can find it here.

SumanthDosapati
Mega Sage
Mega Sage

@Ankit Balapure 

This article might help you.

 

Regards,

Sumanth

Ankur Bawiskar
Tera Patron
Tera Patron

@Ankit Balapure 

what's your business requirement here?

If you want to do some validation then please use onChange rather than onSubmit

remember these points:

1) Asynchronous GlideAjax won't work as by the time ajax function returns the value your form will get submitted

2) Synchronous GlideAjax is not allowed in portal and scoped application

Refer these links for workaround/solution on how to use Synchronous GlideAjax in onSubmit

How To: Async GlideAjax in an onSubmit script

Asynchronous onSubmit Catalog/Client Scripts in ServiceNow

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

Ankit Balapure
Tera Contributor

Hi,

Here's the requirement:

I have a Catalog Item form that includes auto-populating and read-only fields like Name, Manager, etc. The Manager field gets auto-filled based on the Name whenever the form loads.

Now, I want this Catalog Item to be Submitted by :

  1. The Manager of the user auto populated in the form.

  2. Two static users defined in a system property.

Please let me know how we can implement this logic or if you need any further details.

 

Thanks,

Ankit