Make Justification field mandatory when change request is out of planned window

Saranya K
Tera Contributor

Hi all,

 

When Change Request moving to implement and Review State,

if Actual Start Date < Planned Start Date ,
Actual Start Date < Planned End Date,

any one of this  true then abort and throw error and make justification field mandatory and set state value to old value .

 

I have wrote the below on change of state client script . which is not working

 

SaranyaK_0-1753167261608.png

 

8 REPLIES 8

Ankur Bawiskar
Tera Patron

@Saranya K 

please share the script here and not the image.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Hi @Ankur Bawiskar ,

Thanks for replying

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

   //Type appropriate comment here, and begin script below
   var plannedStart = g_form.getValue('start_date');
    var plannedEnd = g_form.getValue('end_date');
    var actualStart = g_form.getValue('work_start');
    var actualEnd = g_form.getValue('work_end');

    if (newValue == '-1' || newValue == '0') {  
        var outsideWindow = false;

        if (actualStart !== '') {
            if (actualStart < plannedStart || actualStart > plannedEnd) {
                outsideWindow = true;
            }
        }


        if (outsideWindow) {
            g_form.addErrorMessage('Please update "Justification for implementation outside planned window" under the Schedule tab.');
            g_form.setMandatory('u_justification_end', true);
        } else {
            g_form.setMandatory('u_justification_end', false);
           
        }
    }
}

@Saranya K 

try this

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

    // Define the state values for 'Implement' and 'Review'.
    // (replace 'implementValue' and 'reviewValue' with your actual state values)
    var implementValue = 'implement_state_number';
    var reviewValue = 'review_state_number';

    // Only check at implement or review states
    if (newValue != implementValue && newValue != reviewValue) return;

    var plannedStart = g_form.getValue('start_date');
    var plannedEnd = g_form.getValue('end_date');
    var actualStart = g_form.getValue('work_start');

    // Helper function to convert GlideDateTime string to JS Date
    function parseDateTime(str) {
        // Handles empty string
        if (!str) return null;
        // 'yyyy-MM-dd HH:mm:ss' => 'yyyy-MM-ddTHH:mm:ss'
        return new Date(str.replace(' ', 'T'));
    }

    var dPlannedStart = parseDateTime(plannedStart);
    var dPlannedEnd = parseDateTime(plannedEnd);
    var dActualStart = parseDateTime(actualStart);

    var error = false;

    // Actual Start Date < Planned Start Date
    if (dActualStart && dPlannedStart && dActualStart < dPlannedStart) {
        error = true;
    }
    // Actual Start Date < Planned End Date
    if (dActualStart && dPlannedEnd && dActualStart < dPlannedEnd) {
        error = true;
    }

    if (error) {
        // Message to user
        g_form.addErrorMessage('Actual Start Date is outside the Planned Window. Please provide justification.');
        // Make justification field mandatory (update 'u_justification_end' to your field name if needed)
        g_form.setMandatory('u_justification_end', true);
        // Optionally, display/focus the justification field:
        g_form.setDisplay('u_justification_end', true);
        g_form.setFieldMessages('u_justification_end', 'Please provide justification before proceeding.', 'error');
        // Reset state to previous value to block transition
        setTimeout(function() { g_form.setValue('state', oldValue); }, 100);
        // Optional: Focus on justification field
        setTimeout(function() { g_form.setFocus('u_justification_end'); }, 300);
    } else {
        g_form.setMandatory('u_justification_end', false);
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

@Saranya K 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.