I am unable to use getXMLWait in scoped application

lucky24
Tera Contributor

Hi Team,

I have to restrict user to submit catalog item if has more then 3 asset .

I am calling Script include in onSubmit script. I am using getXML method with callback function(Asynchronous).

I have written return false in onSubmit client script. As scoped application doesn't support getXMLWait I am unable to make Synchronous

call and   stop user from submitting the form.

Client script -

var requested_for = g_form.getValue('requested_for');
    var user = '';
    if (requested_for != '') {
        user = requested_for;
    } else
        user = requester;
    //alert(user);
  
    var ga = new GlideAjax('Modeldetails');
    ga.addParam('sysparm_name', 'returnDetails');
    ga.addParam('sysparm_caller_id', user);
    ga.addParam('sysparm_staus', NeworOld);
    ga.getXML(function(response) {
        var count = response.responseXML.documentElement.getAttribute("answer");
        //  var country = ga.getAnswer().toString();
        //alert('country: ' + country);
    
        if (count == "No") {
            g_form.showFieldMsg("is_this_a_new_or_replacement", "Sorry, you have exceeded the limit of 3 model per user. Please contact your local IT Team for further instructions", 'error', true);
            return false;

        }
	
    });

Script include- 

   returnDetails: function() {
        var userId = this.getParameter('sysparm_caller_id');
        //var NeworOld = this.getParameter('sysparm_staus');
        var count = 0;
        var gr = new GlideRecord("alm_hardware");
        gr.addQuery("model", "41ce0cb2974abd584649b0d3f153af8b"); // sys id of  asset
        gr.addQuery("assigned_to", userId);
        gr.query();
        while (gr.next()) {
            count++;

        }
        
            if (count >= 3) {
                return "No";
            } else {
                return "Yes";
            }
        
        

    },

How can we achieve it please suggest me.

 

 

6 REPLIES 6

Boyan1
Kilo Sage

Hello ,

You can replace it with getXML(callback) or getXMLAnswer(callback).

 

For further reference. please check:

https://docs.servicenow.com/bundle/utah-platform-user-interface/page/build/service-portal/reference/...

 

Best regards,
Boyan

 

Tai Vu
Kilo Patron
Kilo Patron

Hi @lucky24 

getXMLWait() is not supported by the Service Portal.

Let's try this approach as an alternative. => KB0783579

TaiVu_0-1698996411956.png

 

Your script will look like below.

 

	//Put these lines to your scripts
    if (g_scratchpad.isFormValid) {
        return true;
    }
    var actionName = g_form.getActionName();

    var requested_for = g_form.getValue('requested_for');
    var user = '';
    if (requested_for != '') {
        user = requested_for;
    } else
        user = requester;

    var ga = new GlideAjax('Modeldetails');
    ga.addParam('sysparm_name', 'returnDetails');
    ga.addParam('sysparm_caller_id', user);
    ga.addParam('sysparm_staus', NeworOld);
    ga.getXML(function(response) {
        var count = response.responseXML.documentElement.getAttribute("answer");
        if (count == "No") {
            g_form.showFieldMsg("is_this_a_new_or_replacement", "Sorry, you have exceeded the limit of 3 model per user. Please contact your local IT Team for further instructions", 'error', true);
            return false;
        }
		//Put these lines to your scripts
        g_scratchpad.isFormValid = true;
        g_form.submit(actionName);

    });

 

 

 

Cheers,

Tai Vu

Ankur Bawiskar
Tera Patron
Tera Patron

@lucky24 

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

Vishal Birajdar
Giga Sage

Hi @lucky24 

 

getXMLWait() - will not work in scoped application.

 

VishalBirajdar_0-1698996790614.png

 

Instead of calling glideAjax in onSubmit client script, use onChange client script to get the result from script include using getXMLAnswer() method and use that result in onSubmit script to stop/allow the submission.

 

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates