gcatalog item date time field validation
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-09-2024 07:42 AM
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.
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
1 REPLY 1
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-10-2024 12:15 AM
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
Thank you!!
Dnyaneshwaree Satpute
Tera Guru