Adding conditions in a Custom Date Field

Therese Tolenti
Tera Contributor

I have a development task wherein a user can choose a date in custom date field, only if the conditions below are met

1. case status is equals to complete or completed
2. choice field A or choice field B is not empty

 

How can I achieve this?

2 ACCEPTED SOLUTIONS

@Therese Tolenti 

YOu can add the error message as well:

 

 var caseStatus = g_form.getValue('state');
    var choiceA = g_form.getValue('choice_field_a');
    var choiceB = g_form.getValue('choice_field_b');
    
    // Get the custom date field
    var customDateField = g_form.Value('custom_date_field');

    // Check if case status is complete or completed and either choice field A or B is not empty
    if ((caseStatus == 'complete' || caseStatus == 'completed') && (choiceA || choiceB)) {
        // Enable the custom date field
        g_form..setReadOnly('customDateField',false);
     
    } else {
        // Disable the custom date field if the conditions are not met
            g_form.addErrorMessage('Date can not be selected')
        // Clear the value to prevent submitting invalid data
          g_form.clearValue('custom_date_field');
    }

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

View solution in original post

@Therese Tolenti 

if (caseStatus != '1' || caseStatus != '2') - 

This condition states: "If caseStatus is not equal to '1', or caseStatus is not equal to '2', then...". The problem with this logic is that it will always evaluate to true because caseStatus cannot be both '1' and '2' at the same time.

 

To fix this, you should use the logical operator && (logical AND) instead of || (logical OR) in your condition.

if (caseStatus != '1' && caseStatus != '2')

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

View solution in original post

8 REPLIES 8

Maddysunil
Kilo Sage

@Therese Tolenti 

I think you can write onchange client script on custom date field, below is the sample script you can use:

 

    var caseStatus = g_form.getValue('state');
    var choiceA = g_form.getValue('choice_field_a');
    var choiceB = g_form.getValue('choice_field_b');
    
    // Get the custom date field
    var customDateField = g_form.Value('custom_date_field');

    // Check if case status is complete or completed and either choice field A or B is not empty
    if ((caseStatus == 'complete' || caseStatus == 'completed') && (choiceA || choiceB)) {
        // Enable the custom date field
        g_form..setReadOnly('customDateField',false);
     
    } else {
        // Disable the custom date field if the conditions are not met
                g_form..setReadOnly('customDateField',true);
        // Clear the value to prevent submitting invalid data
        g_form.setValue('custom_date_field', '');
    }
}

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

singharmeet
Tera Contributor

Hi Therese,

I would create a UI Policy on the date field, making it either readonly, or hide it till these conditions are met.

something like below for UI action:

singharmeet_0-1712558457964.png

 

and then create ui policy action for date field:

singharmeet_1-1712558594948.png

 

Please mark this answer correct if you find it usefull.

 

 

Therese Tolenti
Tera Contributor

The Custom Date field should always be editable and visible. The requirement is to add an error if conditions are not met. 

 

@Therese Tolenti 

YOu can add the error message as well:

 

 var caseStatus = g_form.getValue('state');
    var choiceA = g_form.getValue('choice_field_a');
    var choiceB = g_form.getValue('choice_field_b');
    
    // Get the custom date field
    var customDateField = g_form.Value('custom_date_field');

    // Check if case status is complete or completed and either choice field A or B is not empty
    if ((caseStatus == 'complete' || caseStatus == 'completed') && (choiceA || choiceB)) {
        // Enable the custom date field
        g_form..setReadOnly('customDateField',false);
     
    } else {
        // Disable the custom date field if the conditions are not met
            g_form.addErrorMessage('Date can not be selected')
        // Clear the value to prevent submitting invalid data
          g_form.clearValue('custom_date_field');
    }

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks