catalog item

abhisek
Tera Contributor

I have a catalog variable 'Expiration Date' which is a date variable and have another variable 'Is the exemption related to' which has 3 values a, b and c. Now the requirement is, if the value of the variable 'Is the exemption related to' is a then the value of the variable 'Expiration Date' must be auto set to today + 5 days and will be set to read only AND a red bar under the Expiration Date will display with the following text "Exemption under an incident is limited to 7 days" and if the value of the variable 'Is the exemption related to' is b then the value of the variable 'Expiration Date' must be auto set to today +3 days and will be set to read only AND a red bar under the Expiration Date will display with the following text "Exemption under a change limited to 2 ." and if the value of the variable 'Is the exemption related to' is c then the value of the variable 'Expiration Date' must be auto set to the value of max expiration date based on the data in the custom table (if exists).  The field will remain editable but cannot be set to anything that is later than the max date.  IF there is no max duration then it is fully editable to any date in the future. Here we have another reference variable 'which policy' which refers to that custom table which has the the integer field 'default expiration date'.

 

Can you please help me out to implement this.

Thanks in advance.

 

Regards,

Abhisek Chattaraj.

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@abhisek  

so basically based on choice value you need to auto populate date with 5 or 4 or based on some data from table

it should be an easy onChange client script + GlideAjax

what did you start with?

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

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

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Ashish Parab
Mega Sage

Hello @abhisek  ,

 

Please try the below onChange Client script:

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

    var exemptionField = newValue;
    var expirationField = 'expiration_date';

    var today = new Date();
    var redMessage;

    switch (exemptionField) {
        case 'a':
            var dateA = new Date(today);
            dateA.setDate(dateA.getDate() + 5);
            g_form.setValue(expirationField, dateA);
            g_form.setReadonly(expirationField, true);
            redMessage = "Exemption under an incident is limited to 7 days";
            g_form.showFieldMsg(expirationField, redMessage, 'error');
            break;

        case 'b':
            var dateB = new Date(today);
            dateB.setDate(dateB.getDate() + 3);
            g_form.setValue(expirationField, dateB);
            g_form.setReadonly(expirationField, true);
            redMessage = "Exemption under a change limited to 2 days.";
            g_form.showFieldMsg(expirationField, redMessage, 'error');
            break;

        /*case 'c':
            Need to add logic for your option c
            break;*/

        default:
            g_form.setReadonly(expirationField, false);
            g_form.clearMessages();
    }
}

 

For option C, we need to create a Script Include and call it in this client script.

Please note that we won't be able to display the field message in red, as it is not OOB behavior.

 

Output - 

ashish_parab_0-1733919750308.png

 

Please mark this as "correct" and "helpful" if you feel this answer helped you in anyway.

 

Thanks and Regards,

Ashish