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

Thanks @Maddysunil, i will try this 

Hi @Maddysunil, the caseStatus rule is not working properly. I tried using label and then tried using value as well. Values are Completed = 1 and cancelled = 2. I used != so that i don't have to build an else logic. Would you know whats the error here?

 

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

@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

Thanks @Maddysunil, this worked! 

 

I tried a scenario when i reversed or reverted back the status to NOT equal  to 1 and 2 during testing, I was hoping that the script will still work since its .getvalue but what happen is the form still is getting saved instead of getting an error. It only works if I tried to edit again the custom date field. What should be added in the script for it to be automatically be cleared when the status is reverted back?