'Planned start date' check

Thomas G
Tera Guru

Hi,

I have created a custom choice type column on the change_request table called 'u_lead_time_duration'. On that I have defined three choices:
1_day_lead
3_days_lead
5_days_lead

The plan now is that if 1_day_lead is selected, it should not be possible to set a 'Planned start date' (start_date) less than 1 day (or 24 hours) ahead in time. 3_days_lead means not possible to select a 'Planned start date' less than 3 days ahead in time and so forth.

I have then created an onChange Client Script also on the change_request table looking at the field name 'Planned start date'. The script is this:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}

var startDate = g_form.getValue('start_date');
var leadTimeDuration = g_form.getValue('u_lead_time_duration');

if (startDate && leadTimeDuration) {
var minDate = new Date();
minDate.setHours(0, 0, 0, 0); // Set time to midnight

switch (leadTimeDuration) {
case '1_day_lead':
minDate.setDate(minDate.getDate() + 1);
break;
case '3_days_lead':
minDate.setDate(minDate.getDate() + 3);
break;
case '5_days_lead':
minDate.setDate(minDate.getDate() + 5);
break;
}

var selectedDate = new Date(startDate);

if (selectedDate < minDate) {
g_form.addErrorMessage("Start date cannot precede the specified lead time of " + leadTimeDuration + ".");
g_form.clearValue('start_date');
}
}
}

For some reason it does not work. I can still select a 'Planned start date' on, say, today, even if 5_days_lead is selected. 

Can you help me make this script work?

Best regards
Thomas
1 ACCEPTED SOLUTION

Hi @Thomas G 

Yes it would be easier. Before that can you please try this client script?

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var format = g_user_date_time_format;
    var startDate = g_form.getValue('start_date');
    var leadTimeDuration = g_form.getValue('u_lead_time_duration');

    if (startDate && leadTimeDuration) {
        var minDate = new Date();
        minDate.setHours(0, 0, 0, 0); // Set time to midnight

        switch (leadTimeDuration) {
            case '1_day_lead':
                minDate.setDate(minDate.getDate() + 1);
                break;
            case '3_days_lead':
                minDate.setDate(minDate.getDate() + 3);
                break;
            case '5_days_lead':
                minDate.setDate(minDate.getDate() + 5);
                break;
        }
        // get date strings into a number of milliseconds since 1970-01-01
        var startDateMs = getDateFromFormat(startDate, format);
        var leadDateMS = minDate.getTime();

        if (startDateMs < leadDateMS) {
            g_form.addErrorMessage("Start date cannot precede the specified lead time of " + leadTimeDuration + ".");
            g_form.clearValue('start_date');
        }
    }
}
Thanks,
Anvesh

View solution in original post

18 REPLIES 18

Hi Anvesh,

So it would be easier/better to handle with a Business Rule in my case, you think?

Regards
Thomas

Hi @Thomas G 

Yes it would be easier. Before that can you please try this client script?

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var format = g_user_date_time_format;
    var startDate = g_form.getValue('start_date');
    var leadTimeDuration = g_form.getValue('u_lead_time_duration');

    if (startDate && leadTimeDuration) {
        var minDate = new Date();
        minDate.setHours(0, 0, 0, 0); // Set time to midnight

        switch (leadTimeDuration) {
            case '1_day_lead':
                minDate.setDate(minDate.getDate() + 1);
                break;
            case '3_days_lead':
                minDate.setDate(minDate.getDate() + 3);
                break;
            case '5_days_lead':
                minDate.setDate(minDate.getDate() + 5);
                break;
        }
        // get date strings into a number of milliseconds since 1970-01-01
        var startDateMs = getDateFromFormat(startDate, format);
        var leadDateMS = minDate.getTime();

        if (startDateMs < leadDateMS) {
            g_form.addErrorMessage("Start date cannot precede the specified lead time of " + leadTimeDuration + ".");
            g_form.clearValue('start_date');
        }
    }
}
Thanks,
Anvesh

Hi Anvesh,


Thank you such much!


It is not perfectly precise when it comes to the calculation of 1, 3 and 5 days ahead but it is defenitely the best result yet! 🙂

Regards
Thomas

Thanks @Thomas G  for trying it out.

 

Please let me know your exact requirement for calculating the days ahead, what issue it is causing.

 

Please mark my answer helpful and accept as solution if it helped you 👍✔️

Thanks,
Anvesh

Hi again,

I have added some minor changes to the Client Script and now it works as I want. Thank you so much for leading me in the right direction, Anvesh!

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var format = g_user_date_time_format;
    var startDate = g_form.getValue('start_date');
    var leadTimeDuration = g_form.getValue('u_lead_time_duration');

    if (startDate && leadTimeDuration) {
        var minDate = new Date();
        var leadTimeText = ''// Initialize the lead time text

        switch (leadTimeDuration) {
            case '1_day_lead':
                minDate.setHours(minDate.getHours() + 24);
                leadTimeText = '1 day';
                break;
            case '3_days_lead':
                minDate.setHours(minDate.getHours() + 72);
                leadTimeText = '3 days';
                break;
            case '5_days_lead':
                minDate.setHours(minDate.getHours() + 120);
                leadTimeText = '5 days';
                break;
        }

        // Calculate the minimum date in milliseconds
        var startDateMs = getDateFromFormat(startDate, format);
        var leadDateMS = minDate.getTime();

        // Allow a small time difference (e.g., 1 minute)
        var timeDifference = Math.abs(startDateMs - leadDateMS);
        var allowableDifference = 60000// 1 minute in milliseconds

        if (timeDifference > allowableDifference) {
            g_form.addErrorMessage("Start date cannot precede the specified lead time of " + leadTimeText + ".");
            g_form.clearValue('start_date');
        }
    }
}