Warranty expiration date can't be selected past date and simultaneously update the date

Community Alums
Not applicable

Hi All, 

Having a requirement that Warranty expiration date can't be selected past date and simultaneously need to update the date based on the purchased date. 

we need to set the date range limit to 3years . 

tried with the below script to set the date range to 3 years but also need to update it based on the change of the purchased date. and its a editable field so we can't select the past date.

 

function onChange(control, oldValue, newValue, isLoading) {
if ( newValue == '') {
return;
}
var cdt = new Date();
var addtime = 1095; //The amount of time to add
var addtype = 'day'; //The time type. Can be day, week, month, year.
var ajax = new GlideAjax('ClientDateTimeUtils');
ajax.addParam('sysparm_name', 'addDateAmount');
ajax.addParam('sysparm_fdt', cdt);
ajax.addParam('sysparm_addtime', addtime);
ajax.addParam('sysparm_addtype', addtype);
ajax.getXML(SetReqDate);
function SetReqDate(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
//alert(answer);
g_form.setValue('warranty_expiration_1', answer);
//g_form.setReadOnly('warranty_expiration_1',true);
}
//Type appropriate comment here, and begin script below
}

 

 

Any suggestion to get the requirement.

2 REPLIES 2

Danish Bhairag2
Tera Sage
Tera Sage

Hi @Community Alums,

 

To achieve your requirement of setting the Warranty expiration date to a maximum of 3 years from the purchased date and updating it based on the change of the purchased date, you can use the following approach in your onChange client script:

 

function onChange(control, oldValue, newValue, isLoading) {

    if (isLoading || newValue === '') {

        return;

    }
    // Parse the selected purchased date

    var purchasedDate = new Date(newValue);

    // Calculate the maximum allowed warranty expiration date (3 years from purchased date)

    var maxWarrantyExpirationDate = new Date(purchasedDate);

    maxWarrantyExpirationDate.setFullYear(purchasedDate.getFullYear() + 3);

    // Get the current warranty expiration date from the form

    var currentWarrantyExpirationDate = new Date(g_form.getValue('warranty_expiration_1'));

    // If the current warranty expiration date is later than the maximum allowed date, reset it

    if (currentWarrantyExpirationDate > maxWarrantyExpirationDate) {

        g_form.setValue('warranty_expiration_1', '');

    }

    // Update the maximum allowable date for warranty expiration

    g_form.setDisplay('warranty_expiration_1', true); // Make sure the field is visible

    g_form.setLimit('warranty_expiration_1', 'sys_date', '<=' + maxWarrantyExpirationDate.toString());

}

 

In this script:

 

1. When the purchased date changes, it calculates the maximum allowable warranty expiration date by adding 3 years to the purchased date.

2. It then compares the current warranty expiration date (if any) with the calculated maximum date. If the current date is beyond the allowed range, it clears the field. Otherwise, it allows the field value to remain unchanged.

3. The `g_form.setLimit()` function is used to set the maximum allowed date for the `warranty_expiration_1` field. This ensures that users cannot select a date beyond the 3-year limit from the purchased date.

 

Please ensure that the field names and date formats in the script match your actual ServiceNow instance configuration. Additionally, thoroughly test the script to confirm it behaves as expected in different scenarios.

 

Mark my answer helpful & accepted if it helps you resolve your query

 

Thanks,

Danish

Community Alums
Not applicable

Hi @Danish Bhairag2 

 

tried as suggested but its not working as expected 

chanti1_0-1697137718904.png

if we modify the purchased date simultaneously its not getting updated on the warranty expiration field with 3years time interval. 

 

Any other suggestions.