MaxMixali
Mega Sage

✅ Requirement: Restrict Date Field and Map to Due Date

Users should not be able to add:
- The current day or next 2 days
- Weekends (Saturday, Sunday)
They should only be able to select business days, and the selected date should map to the Due Date field.

--------------------------------------------------------------------
🧩 Implementation Steps
--------------------------------------------------------------------

Step 1: Create the Date Variable / Field
----------------------------------------
- Variable name: u_due_date
- Type: Date
- Mandatory: true

Step 2: Add Catalog Client Script (onChange)
--------------------------------------------
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

var selectedDate = new Date(newValue);
var today = new Date();

// Remove time
today.setHours(0, 0, 0, 0);
selectedDate.setHours(0, 0, 0, 0);

// Calculate difference in days
var diffDays = Math.floor((selectedDate - today) / (1000 * 60 * 60 * 24));

// Restrict current and next 2 days
if (diffDays <= 2) {
alert("Please select a date at least 3 business days from today.");
g_form.clearValue('u_due_date');
return;
}

// Restrict weekends
var day = selectedDate.getDay();
if (day == 0 || day == 6) {
alert("Weekends are not allowed. Please select a business day (Monday–Friday).");
g_form.clearValue('u_due_date');
return;
}

// Map to Due Date field
g_form.setValue('due_date', newValue);
}

--------------------------------------------------------------------
Step 3: Use for Standard Forms (Optional)
----------------------------------------
Create a Client Script on the table (e.g., Task, Incident) with same logic.

--------------------------------------------------------------------
Step 4: Optional Enhancement
----------------------------------------
You can extend validation to check official holidays via a Script Include referencing cmn_schedule_span.

--------------------------------------------------------------------
✅ Expected Behavior
| Scenario | Result |
|-----------|---------|
| Today | ❌ Alert |
| Tomorrow / +2 days | ❌ Alert |
| Weekend | ❌ Alert |
| Valid business day (+3) | ✅ Accepted, auto-mapped to Due Date |