Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

Restrict users to select more than/less than the current time based on other field selection

ss123
Tera Contributor

Hello All,

 

We have a requirement wherein the "Requested due date and time" should accept date/time based on the selected Request type.

 

For example: When Users choose Request type: “Update” or “Archive.” user is able to set a due date between 3 and 8 hours from the current date/time.

But when they select Request Type: “New”, they must choose a due date that is at least 8 hours from the current date/time.

 

Thanks!

1 REPLY 1

Mark Manders
Mega Patron

With a catalog client script, you should be able to do this.

Can you check if below script works for you?

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return; 
    }
    
    var requestType = g_form.getValue('request_type');
    var dueDateField = 'requested_due_date_time';
    
    // Get the current date/time
    var currentDate = new Date();

    // Define the minimum hours for each type
    var minHours = 0; // Default to 0, but will change based on the request type

    if (requestType == 'New') {
        minHours = 8; // New requests must be at least 8 hours from now
    } else if (requestType == 'Update' || requestType == 'Archive') {
        minHours = 3; // Updates and Archives must be between 3 and 8 hours from now
    }
    
    // Calculate the minimum valid due date
    var minDate = new Date(currentDate.getTime() + (minHours * 60 * 60 * 1000));
    
    // Perform validation
    var requestedDueDate = new Date(g_form.getValue(dueDateField));
    
    if (requestType == 'New' && requestedDueDate < minDate) {
        g_form.showFieldMsg(dueDateField, 'Due date must be at least 8 hours from now for New requests.', 'error');
        g_form.setValue(dueDateField, ''); // Clear invalid date
    } else if ((requestType == 'Update' || requestType == 'Archive') && 
               (requestedDueDate < minDate || requestedDueDate > new Date(currentDate.getTime() + (8 * 60 * 60 * 1000)))) {
        g_form.showFieldMsg(dueDateField, 'Due date must be between 3 and 8 hours from now for Update or Archive requests.', 'error');
        g_form.setValue(dueDateField, ''); // Clear invalid date
    } else {
        g_form.hideFieldMsg(dueDateField);
    }
}

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