- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-05-2024 02:30 PM
Hi guys, how are you? Can you help me?
I have a request to create a client script to allow the user to select in my date field (due_date_tax_collection_guide) only Tuesdays and Thursdays, but he can only choose Tuesdays and Thursdays 4 days ahead of the current day. But I can't get it to work properly, could you help me?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-06-2024 08:46 AM
Hello @MariaVitorS
Those error message is of previously selected date. I have added a g_form.clearMessages() to clear any previous error message exist.
Here is the updated code:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || !newValue) {
return;
}
g_form.clearMessages();
validateDate();
}
function validateDate() {
var dateValue = g_form.getValue('date');
if (dateValue) {
var selectedDate = new Date(dateValue); // Convert selected date to a JavaScript Date object
var currentDate = new Date(); // Today's date
// Add 4 days to the current date
currentDate.setDate(currentDate.getDate() + 4);
// Get day of the week (0 = Sunday, 1 = Monday, ..., 6 = Saturday)
var dayOfWeek = selectedDate.getDay();
// Validate: must be >= 4 days from today and either Tuesday (2) or Thursday (4)
if (selectedDate < currentDate || (dayOfWeek !== 2 && dayOfWeek !== 4)) {
g_form.clearValue('date'); // Clear invalid date
g_form.addErrorMessage('Please select a valid date. The date must be at least 4 days from today and fall on a Tuesday or Thursday.');
}
}
}
This clears away any previously existing message in the form and satisfy the requirement. I hope this helps!
"If you found my answer helpful, please like and mark it as an "accepted solution". It helps others find the solution more easily and supports the community!"
Thank You
Juhi Poddar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-05-2024 11:02 PM
Hi @MariaVitorS ,
can you try something like below
client script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var ga = new GlideAjax('getCallersLocalTime');
ga.addParam('sysparm_name', 'getTTdays');
ga.addParam('sysparam_date', newValue);
ga.getXMLAnswer(function(response) {
var userDetails = JSON.parse(response);
if(!userDetails){
g_form.clearValue('u_select_date');
g_form.showFieldMsg('u_select_date','Please date in Tuesday and Thursday',true);
}
});
}
script include: client callable true
var getCallersLocalTime = Class.create();
getCallersLocalTime.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getTTdays:function(){
var dateid = this.getParameter('sysparam_date');
var gdt = new GlideDateTime(dateid);
var days=gdt.getDayOfWeek();
if(days ==2 || days ==4) // The day of the week value - Monday = 1, ... Sunday = 7.
{
return 'true';
}
else{
return 'false';
}
},
type: 'getCallersLocalTime'
});
Please mark my answer correct/helpful , If it helped you
Thanks,
BK
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-06-2024 04:55 AM
Hi @Bhavya11 I tested it here and it didn't work, no message was displayed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-06-2024 12:56 AM
Hello @MariaVitorS
- Write an onChange client script on date field.
onChange client script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || !newValue) {
return;
}
validateDate();
}
function validateDate() {
var dateValue = g_form.getValue('date');
if (dateValue) {
var selectedDate = new Date(dateValue); // Convert selected date to a JavaScript Date object
var currentDate = new Date(); // Today's date
// Add 4 days to the current date
currentDate.setDate(currentDate.getDate() + 4);
// Get day of the week (0 = Sunday, 1 = Monday, ..., 6 = Saturday)
var dayOfWeek = selectedDate.getDay();
// Validate: must be >= 4 days from today and either Tuesday (2) or Thursday (4)
if (selectedDate < currentDate || (dayOfWeek !== 2 && dayOfWeek !== 4)) {
g_form.clearValue('date'); // Clear invalid date
g_form.addErrorMessage('Please select a valid date. The date must be at least 4 days from today and fall on a Tuesday or Thursday.');
}
}
}
Result:
- Error message appear when an invalid date is entered
- Close the error message and enter valid date:
This is testing in my PDI and it works for me. Hope this helps!
"If you found my answer helpful, please like and mark it as an "accepted solution". It helps others find the solution more easily and supports the community!"
Thank You
Juhi Poddar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-06-2024 04:54 AM
Hi @Juhi Poddar
I've tested it here and even on dates that should work, the error message is being displayed. For example, if I put today (December 6) on Tuesday or Thursday (days 10 and 12 respectively) they should be allowed and the error is being displayed...