The CreatorCon Call for Content is officially open! Get started here.

Please rectify the error coming in logs for the date field validation

rishabh31
Mega Sage
Hello Team,
 
Can anyone please help me to get this error resolved and make these scripts work?
 
Requirement- 'Date to Disable' (Date type variable) should not be the Past date, it can be either today's date or any future date.
 
To achieve this I wrote below the client callable script Include:

 

 

validateDatetoDisable.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    nowDateTime: function() {
        var disableDate = this.getParameter('sysparm_disable_dt');
        gs.log('Rishi Disable Dt ' + disableDate);
        var cDate = new GlideDate();
        gs.log('Rishi Current Dt ' + cDate);
        //return gs.dateDiff(gs.nowDateTime(), disableDate, true) / 86400;
        //return gs.dateDiff(cDate, disableDate, true) / 86400;
        return gs.dateDiff(cDate, disableDate, true);
    },
    type: 'validateDatetoDisable'
});​

 

 

Then I wrote onChange Client Script on change of Date to Disable and called the above script include as below:
 

 

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var disDate = new GlideAjax('validateDatetoDisable');
    disDate.addParam('sysparm_name', 'nowDateTime');
    disDate.addParam('sysparm_disable_dt', g_form.getValue('date_to_disable_account'));
    disDate.getXML(compareDisableDate);

    function compareDisableDate(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        //if (answer <= 0) {To neither allow current date nor past date
        if (answer < 0) { //To allow current date but not past date
            g_form.addErrorMessage("Date to Disable should not be the Past Date");
            g_form.clearValue('date_to_disable_account');
        }
    }
}

 

 

But I am getting the below error (shown in the screenshot/attached) while checking through the logs,
rishabh31_0-1691129870051.jpeg

I know this can be achieved through UI Policy Script, but I want this fulfilled through Client Callable Script Include for learning purposes.

 

Thank You in advance!

2 ACCEPTED SOLUTIONS

OlaN
Giga Sage
Giga Sage

Oh, sorry, I completely missed the error from the log that you posted.

That error indicates something else, maybe a scope identifier will help?

Here's how I would do it, if going for the client-script/script include way.

 

Script include:

script-include-date-validation.png

 

Client script:

client-script-date-validation.png

View solution in original post

Okay, that will make it a bit more complicated.

Add these lines to the script to allow for the same day also.

 

And on another note, make it a habit to use gs.info() instead of gs.log(), since gs.log() does not work in scoped applications.

isBeforeToday: function() {
        var compareDate = this.getParameter('sysparm_date_compare');
        if (!compareDate)
            return 0;

        var gdt = new GlideDateTime();
        var compareGDT = new GlideDateTime(compareDate);
	
	var systemDay = gdt.getDate().toString();
	var compareDay = compareGDT.getDate().toString();

	if (systemDay == compareDay) {
		return 1;
	else {
	        if (compareGDT.before(gdt)) {
        	    return 0;
        	} else {
        	    return 1;
        	}
	}
    },

View solution in original post

10 REPLIES 10

OlaN
Giga Sage
Giga Sage

Hi,

I believe the error here is that the function is returning a value as a String, but you are trying to compare it as an Integer.

That will not work.

 

Also, if you are just looking at date comparison, you can probably do this with a UI policy, no coding would be required.

Have a look at this article by Mark Roethof to help you get started.

OlaN
Giga Sage
Giga Sage

Oh, sorry, I completely missed the error from the log that you posted.

That error indicates something else, maybe a scope identifier will help?

Here's how I would do it, if going for the client-script/script include way.

 

Script include:

script-include-date-validation.png

 

Client script:

client-script-date-validation.png

Thank You @OlaN Sir, its working fine now

Hello @OlaN Sir,

 

Sorry was not tested your provided script with Current (Today's) date selection. When I select Today's date it's displaying 'Invalid Date' (see screenshot) which is not desired. It should take today's date or any future date. Please help so that it will take Today's date as well.

 

rishabh31_0-1691327164702.png

 

I tried to verify this through applying logs (see the below screenshot)

rishabh31_0-1691327985940.png

Provided Script Include with Logs:

rishabh31_1-1691328114680.png

 

 

var DisdtValidation = Class.create();
DisdtValidation.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    isBeforeToday: function() {
        var compareDate = this.getParameter('sysparm_date_compare');
		gs.log('Rishi Compare Dt ' + compareDate);
        if (!compareDate)
            return 0;

        var gdt = new GlideDateTime();
		gs.log('Rishi Now Dt ' + gdt);
        var compareGDT = new GlideDateTime(compareDate);
		gs.log('Rishi Compare GDT ' + compareGDT);
        if (compareGDT.before(gdt)) {
            return 0;
        } else {
            return 1;
        }
    },
    type: 'DisdtValidation'
});

 

 

Okay, that will make it a bit more complicated.

Add these lines to the script to allow for the same day also.

 

And on another note, make it a habit to use gs.info() instead of gs.log(), since gs.log() does not work in scoped applications.

isBeforeToday: function() {
        var compareDate = this.getParameter('sysparm_date_compare');
        if (!compareDate)
            return 0;

        var gdt = new GlideDateTime();
        var compareGDT = new GlideDateTime(compareDate);
	
	var systemDay = gdt.getDate().toString();
	var compareDay = compareGDT.getDate().toString();

	if (systemDay == compareDay) {
		return 1;
	else {
	        if (compareGDT.before(gdt)) {
        	    return 0;
        	} else {
        	    return 1;
        	}
	}
    },