Auto-populate Expiration date based on the Start date and Duration of months selected

Nagashree5
Tera Contributor

Hi All,

 

Below are the fields on the form level.

Nagashree5_0-1697640605501.png

Based on the start date and duration of months selected (it is a dropdown of 1 to 12 numbers) - Expiration date should be auto populated.

suppose if the start date is selected as 10-18-2023 and duration of months is selected as 3 months, the Expiration date needs to be auto populated as 10-18-2023 + 90 days.

Can anyone guide me on this.

 

TIA.

15 REPLIES 15

Alka_Chaudhary
Mega Sage
Mega Sage

Hello @Nagashree5 ,

You can write on change client script for 'PO Duration(in Month)' field.

You can use the below script to make it work.

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var d = g_form.getValue('start_date'); // replcae the 'start_date' with the variable name of start date
    var startDate = new GlideDateTime(d);
    var endDate = new GlideDateTime(startDate);
    if (newValue == 1) {
        endDate.addDays(30);

    } else if (newValue == 2) {
        endDate.addDays(60);

    } else if (newValue == 3) {
        endDate.addDays(90);

    } else if (newValue == 4) {
        endDate.addDays(120);

    } else if (newValue == 5) {
        endDate.addDays(150);

    } else if (newValue == 6) {
        endDate.addDays(180);

    } else if (newValue == 7) {
        endDate.addDays(210);

    } else if (newValue ==8)  {
        endDate.addDays(240);

    } else if (newValue == 9) {
        endDate.addDays(270);

    } else if (newValue == 10) {
        endDate.addDays(300);

    } else if (newValue == 11) {
        endDate.addDays(330);

    } else if (newValue == 12) {
        endDate.addDays(360);

    }
    g_form.setValue('end_date', endDate.getDate()); // replace 'end_date' with the variable name of end date





}

 

 

Also, check the value of choices for 'PO Duration(in month)' is 1,2,3....12 if not the change the if and else condition value.

 

If this response clears up your doubt, kindly flag it as both helpful and correct.

Thanks,

Alka

Hi @Alka_Chaudhary,

 

Thanks for the response. I have tried the mentioned code and it I'm getting "JavaScript error in your browser console" on portal and the error is ReferenceError: GlideDateTime is not defined.

Please suggest.

Hello @Nagashree5 ,

We cannot use GlideDateTime() in client script, sorry for inconvenience. To use GlideDateTime(), we can use it in server side script that's why we have to use AjaxCall.

 

You can use the below script in onChange client script for 'PO Duration(in Month)' field.(replace the variable names):-

 

 

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

    var d = g_form.getValue('start_date'); // replace the 'start_date' with the variable name of start date
    var ga = new GlideAjax('getAddMonthToEndDate');
    ga.addParam('sysparm_name', 'getEndDate');
    ga.addParam('sysparm_month', newValue);
    ga.addParam('sysparm_start_date', d);
    ga.getXML(callback);

    function callback(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        g_form.setValue('end_date', answer); //replace the 'end_date' with the variable name of start date

    }

}

 

 

Then, create a script include with the name 'getAddMonthToEndDate' and check the client callable checkbox. Add the below code in the script:-

 

 

var getAddMonthToEndDate = Class.create();
getAddMonthToEndDate.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getEndDate: function() {
        var newValue = this.getParameter('sysparm_month');
        var d = this.getParameter('sysparm_start_date');
        var startDate = new GlideDateTime(d);
        var endDate = new GlideDateTime(startDate);
        if (newValue == 1) {
            endDate.addDays(30);

        } else if (newValue == 2) {
            endDate.addDays(60);

        } else if (newValue == 3) {
            endDate.addDays(90);

        } else if (newValue == 4) {
            endDate.addDays(120);

        } else if (newValue == 5) {
            endDate.addDays(150);

        } else if (newValue == 6) {
            endDate.addDays(180);

        } else if (newValue == 7) {
            endDate.addDays(210);

        } else if (newValue ==8)  {
            endDate.addDays(240);

        } else if (newValue == 9) {
            endDate.addDays(270);

        } else if (newValue == 10) {
            endDate.addDays(300);

        } else if (newValue == 11) {
            endDate.addDays(330);

        } else if (newValue == 12) {
            endDate.addDays(360);

        }
        return endDate.getDate();
    },

    type: 'getAddMonthToEndDate'
});

 

 

Also, check the value of choices for 'PO Duration(in month)' is 1,2,3....12 if not the change the if and else condition value.

 

If this response clears up your doubt, kindly flag it as both helpful and correct.

Thanks,

Alka