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

gcatalog item date time field validation

NiloferS
Tera Contributor

in a catalog item we have a field "training date type". its a reference field getting its choice from a custom table. this field is in a variable set of the catalog item.

NiloferS_0-1720535804698.png

we need to validate the field so that user cant submit the request one week before from that dropdown date.

i have written below script include and called it in onsubmit catalog client script. it is not working

NiloferS_1-1720536000905.png

NiloferS_2-1720536113197.png

 

1 REPLY 1

Dnyaneshwaree
Mega Sage

Hello @NiloferS,

Try below code as an example and update it as per your req:
Script include:

var ValidateTrainingDate = Class.create();
ValidateTrainingDate.prototype = {
    initialize: function() {
    },

    validateDate: function(trainingDate) {
        var gr = new GlideRecord('your_custom_table');
        if (gr.get(trainingDate)) {
            var selectedDate = new GlideDateTime(gr.getValue('date_field')); // Assuming 'date_field' is the field name in your custom table
            var today = new GlideDateTime();
            today.addDays(7); // Add 7 days to the current date

            if (selectedDate < today) {
                return false; // Date is less than 7 days from today
            }
        }
        return true; // Date is valid
    },

    type: 'ValidateTrainingDate'
};

Onsubmit Catalog client script:

function onSubmit() {
    var trainingDate = g_form.getValue('your_reference_field'); // Replace with your reference field variable name
    var ga = new GlideAjax('ValidateTrainingDate');
    ga.addParam('sysparm_name', 'validateDate');
    ga.addParam('sysparm_trainingDate', trainingDate);
    ga.getXMLAnswer(function(response) {
        var isValid = response.responseXML.documentElement.getAttribute("answer");
        if (isValid == 'false') {
            g_form.addErrorMessage('You cannot submit the request less than one week before the selected date.');
            return false;
        }
    });
    return true; // Allow the form to be submitted
}

 

Please accept my solution if it works for you and thumps up to mark it as helpful.
Thank you!!

Dnyaneshwaree Satpute
Tera Guru