How to use synchronous call on service portal.

Bosco Sahayaraj
Mega Contributor

Hi Team,

I want to do date validation. Here getXMLWait() is not working on service portal. Any other alternative solution for this. Because i can not use asynchronous glide ajax, it will not wait for the service response. I am keeping my code below.   I am sure that my script include is working good.

function onSubmit() {

var startDate = g_form.getValue("start");

var endDate = g_form.getValue("end");

if(startDate != undefined && startDate != '' && endDate != undefined && endDate != ''){

var validation = new GlideAjax('OLA_DongleDateValidation');

validation.addParam('sysparm_name','ola_Startvalidation');

validation.addParam('sysparm_Start',startDate );

validation.addParam('sysparm_End',endDate );

//validation.getXMLWait(); not working in portal

//var answer = validation.getAnswer();

validation.getXML(HelloWorldParse);

var valid = new GlideAjax('OLA_DongleDateValidation');

valid.addParam('sysparm_name','ola_currentdatevalidation');

valid.addParam('sysparm_Start',startDate);

valid.addParam('sysparm_End',endDate );

valid.getXMLWait();

var answer1 = valid.getAnswer();

alert(answer1);

valid.getXML(HelloWorld);

    }

function HelloWorldParse(response) {  

    var answer = response.responseXML.documentElement.getAttribute("answer");

    if(answer = false){

    alert("End date should not be before start date");

    return answer;

    }

}

    function HelloWorld(response) {  

    var answer = response.responseXML.documentElement.getAttribute("answer");

    if(answer = false)

    {

    alert("Start date should not be a past date");

    return answer;

    }

    }

synchronous

6 REPLIES 6

Ivano B
ServiceNow Employee
ServiceNow Employee

Hi Bosco



I would suggest to have a look to this documentation



documentation/client_scripting.md at master · service-portal/documentation · GitHub



Maybe you can find some more information about what it is allowed.



Cheers


R0b0


Ivano B
ServiceNow Employee
ServiceNow Employee

Bosco



You can also check the field type on your script form in case the script is created for a service catalog item.


It should be set to 'Both'



Catalog_Client_Scripts___ServiceNow.png



Cheers R0b0


andrewpilachows
Kilo Guru

The getXMLWait() function is not supported in scoped applications nor Service Portal.   Also, async calls are not possible in onChange scripts because the script runs to completion without waiting on the return.   I would recommend moving client validation to onChange scripts.


Client Script Best Practices - ServiceNow Wiki



Other way is to find a client side alternative to evaluate dates instead of relying on server side APIs.


Date Format Validation in the new Service Portal