Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Script include not returning value

tcorniels
Tera Contributor

I have a script include that I cam calling from a catalog client script.  Ultimately I need to script include to return the current date + 2 days and then compare it, in the client script, to a variable field and then reset the variable field if the user chooses an invalid date.  I understand you can't do current date/time checks in a client script.  Not sure why I'm not getting a return value from the script include.

 

SCRIPT INCLUDE

var DateNowPlusTwoDays = Class.create();
DateNowPlusTwoDays.prototype = Object.extendsObject(AbstractAjaxProcessor, {
nowTime:function(){

	var valid_date = gs.Date();
	valid_date.addDaysUTS(2);

	return valid_date;
},

    type: 'DateNowPlusTwoDays'
});

CLIENT SCRIPT

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

   var ajax = new GlideAjax('DateNowPlusTwoDays');
   ajax.addParam('sysparm_name','nowTime');
   ajax.getXML('getResponse');
   console.log(ajax);
   console.log(g_form.getValue('date_needed'));
   
	if (g_form.getValue('date_needed') < ajax) {
		alert('Two business day lead time required');
		g_form.setValue('date_needed', ajax);
		return;
	}
   
}

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@tcorniels 

you can do this without script include

use this

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

    var selectedDate = new Date(getDateFromFormat(g_form.getValue('date_needed'), g_user_date_format));
    var today = new Date();
    var two_days_from_now = new Date();
    two_days_from_now.setDate(today.getDate() + 2);

    if (selectedDate.getTime() > two_days_from_now.getTime()) {
        alert('Two business day lead time required');
        g_form.clearValue('date_needed');
        g_form.setMandatory('date_needed', true);
    }
}

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

View solution in original post

3 REPLIES 3

Bert_c1
Kilo Patron

For your script include, change:

valid_date.addDaysUTS(2);

to

valid_date.addDaysUTC(2);

And the client script is missing the 'getResponse()' function.  See examples in your instance. A snippit from one I did:

 

	// callback function for returning the result from the script include
	function getResponse(response) {
        var users = response;
//		alert('users = ' + users);
        g_form.setValue('u_members_list', users); 
    }

 

Ankur Bawiskar
Tera Patron
Tera Patron

@tcorniels 

you can do this without script include

use this

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

    var selectedDate = new Date(getDateFromFormat(g_form.getValue('date_needed'), g_user_date_format));
    var today = new Date();
    var two_days_from_now = new Date();
    two_days_from_now.setDate(today.getDate() + 2);

    if (selectedDate.getTime() > two_days_from_now.getTime()) {
        alert('Two business day lead time required');
        g_form.clearValue('date_needed');
        g_form.setMandatory('date_needed', true);
    }
}

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

Viraj Hudlikar
Tera Sage
Tera Sage

Hello @tcorniels 

Script Include

var DateNowPlusTwoDays = Class.create();
DateNowPlusTwoDays.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    nowTime: function() {
        var valid_date = new GlideDateTime();
        valid_date.addDaysUTC(2); // Correct method name
        return valid_date.getDate(); // Returns date portion as ISO string (yyyy-MM-dd)
    },
    type: 'DateNowPlusTwoDays'
});

 

Client Script

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

    var ajax = new GlideAjax('DateNowPlusTwoDays');
    ajax.addParam('sysparm_name', 'nowTime');
    
    // Add callback function as parameter
    ajax.getXML(function(response) {
        var minDateString = response.responseXML.documentElement.getAttribute("answer");
        var userDateString = g_form.getValue('date_needed');
        
        // Convert strings to Date objects
        var minDate = new Date(minDateString);
        var userDate = new Date(userDateString);
        
        // Compare dates
        if (userDate < minDate) {
            alert('Two business day lead time required');
            // Set back to server's minimum date
            g_form.setValue('date_needed', minDateString);
        }
    });
}

 

If my response has helped you hit helpful button and if your concern is solved do mark my response as correct.

 

Thanks & Regards
Viraj Hudlikar.