Get a first look at what's coming. The Developer Passport Australia Release Preview kicks off March 12. Dive in! 

Return date field should be have maximum 15days duration from date required field.

girija04
Tera Contributor

In Catalog form "Return Date" field on form to have a maximum limit of 15 business days (3 weeks) from the "Date Required" so that the form is used properly.

2 REPLIES 2

Mark Manders
Giga Patron

Create an onChange catalog client script on the 'return date' field (check your field names on the next script):

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue == '') {
        return;
    }
    
    // Retrieve values from form fields
    var returnDate = g_form.getValue('return_date');
    var dateRequired = g_form.getValue('date_required');

    // Ensure both dates are set
    if (!returnDate || !dateRequired) {
        return;
    }

    // Convert date strings to GlideDateTime objects for comparison
    var gdReturnDate = new GlideDateTime(returnDate);
    var gdDateRequired = new GlideDateTime(dateRequired);

    // Get the current date/time
    var now = new GlideDateTime();

    // Calculate the difference in days
    var diffInDays = gs.dateDiff(dateRequired, returnDate, true) / (1000 * 60 * 60 * 24);

    // Calculate the max allowed date by adding 15 business days
    var maxReturnDate = new GlideDateTime(dateRequired);
    var businessDays = 0;
    
    // Loop to add business days (excluding weekends)
    while (businessDays < 15) {
        maxReturnDate.addDaysUTC(1);
        if (maxReturnDate.getDayOfWeek() != 0 && maxReturnDate.getDayOfWeek() != 6) { // 0 = Sunday, 6 = Saturday
            businessDays++;
        }
    }

    // Perform the validation
    if (gdReturnDate.before(now) || gdReturnDate.after(maxReturnDate)) {
        // Invalid date, clear the field and show error message
        g_form.clearValue('return_date');
        g_form.showErrorBox('return_date', 'The selected Return Date must be within 15 business days from the Date Required and cannot be in the past.');
    } else {
        // Hide any previous error message
        g_form.hideErrorBox('return_date');
    }
}

 


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

Najmuddin Mohd
Mega Sage

Hi @girija04 ,
Create an on-change client script with variable name selected return_date.
Pass the values to the script include to check the differnce between both. 
If greater than 15 days, show a message and clear the value.

Catalog on - change client script:

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


    var requiredDate = g_form.getValue('date_required');
    var returnDate = g_form.getValue('date_return');

   

    var ga = new GlideAjax('global.getDays');
    ga.addParam('sysparm_name', 'getDate');
    ga.addParam('requiredDate', requiredDate);
    ga.addParam('returnDate', returnDate);
    ga.getXML(callback);

    function callback(response) {

        var answer = response.responseXML.documentElement.getAttribute("answer");
        
        var days = parseInt(answer);
		g_form.addInfoMessage(days);
		
        if (days > 15) {
            g_form.addErrorMessage('Return Days is more than 15 days');
			g_form.clearValue('date_return');
        }
    }

}

 

 

Script include:

Name: getDays

Client callable: selected

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

	getDate: function() {
		
        
        var requiredDate = this.getParameter('requiredDate');
        var returnDate = this.getParameter('returnDate');

        var sgd1 = new GlideDate();
        sgd1.setDisplayValue(requiredDate);
        var sgd2 = new GlideDate();
        sgd2.setDisplayValue(returnDate);

        var duration = GlideDate.subtract(sgd1, sgd2);
        //gs.addInfoMessage(duration.getDisplayValue());

		return duration.getDisplayValue();

    },

    type: 'getDays'
});

 

NajmuddinMohd_0-1724923186351.png

 

NajmuddinMohd_2-1724923250269.png


If this information helps you, kindly mark it as Helpful and Accept the solution.

Regards,
Najmuddin.