How to return value to getXML function in client script

Tomi Corigliano
Kilo Sage

Hello,

 

I'm calling a script include in my catalog client script through GlideAjax, which works fine. Nevertheless, I can't find a way to return the value to the main function.

 

How is this possible? Preferably with an example.

 

Thanks in advance for your reply.

1 ACCEPTED SOLUTION

@Tomi Corigliano 

you can show field message of type error so it won't allow submitting the form.

So unless and until the user selects correct date they cannot submit it

When the validation fails show field message of type error; if validation is fine clear the message

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

10 REPLIES 10

Tomi Corigliano
Kilo Sage

This is the script include:

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

    checkIfDateIsGreaterThanNowInDays: function() {
		
        var startDT = this.getParameter('sysparm_sdt');
		var addDays = this.getParameter('sysparm_ad');
        var amountSec = gs.dateDiff(gs.nowDateTime(), startDT, true);
		var amountDays = parseInt(amountSec) / (60 * 60 * 24);
		if (amountDays > parseInt(addDays)){
			return true;
		} else {
			return false;
		}
    },
	
    type: 'DWPCatalogClientUtils'
	
});

This is the catalog client script:

function onSubmit() {
   
	var ecDate = g_form.getValue('ec_last_day');
	
	var ga = new GlideAjax('DCITSMCatalogClientUtils');
	ga.addParam('sysparm_name', 'getNowDateTimeDiff');
	ga.addParam('sysparm_sdt', ecDate);
	ga.getXML(GetDate);
	
	function GetDate(response){
		var diffSec = response.responseXML.documentElement.getAttribute("answer");
		var diffDays = parseInt(diffSec) / (60 * 60 * 24);
		
		if (diffDays > 0){
			g_form.addErrorMessage('The inserted day can not be in the future!','error');
			return false;
		}
		if (diffDays < -35) {
			g_form.addErrorMessage('The inserted day can not be more than maximum 35 days ago!','error');
			return false;
		}
	}
	
}

 

@Tomi Corigliano 

you just want to check these things

1) date cannot be in future

AND

2) date cannot be more than 35 days older

You can validate this using UI policy and not script required

Did you try to use that approach?

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Ankur,


It's true that you can show the FieldMessage through a UI policy, but this will not prevent it from submitting.

@Tomi Corigliano 

you can show field message of type error so it won't allow submitting the form.

So unless and until the user selects correct date they cannot submit it

When the validation fails show field message of type error; if validation is fine clear the message

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

I have configured it as follows:

 

TomiCorigliano_0-1683708673801.png

TomiCorigliano_1-1683708707840.png

 

It shows the message but it doesn't prevent the submit