The CreatorCon Call for Content is officially open! Get started here.

Date should be Auto populate based on the value in other field

Brijmohan
Tera Contributor

Hi All,

There are two filed on the catalog form. One is date and another one is Project that have drop down value example ab and cd. I want to populate date field based on the Project drop down value.

If ab is selected in Project then date should be auto populate one year from now and if cd value is selected then date should be auto populate two years from now.

Right now I have below code.

 

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

    var getChoiceValue = g_form.getValue('test'); // variablename

    if (getChoiceValue == 'ab') {
        var todayDate = new Date(new Date().setFullYear(new Date().getFullYear() + 1)); // Now

        g_form.setValue('date', todayDate.toISOString().slice(0, 10));

    } else if (getChoiceValue == 'cd') {

        var todayDate1 = new Date(new Date().setFullYear(new Date().getFullYear() + 2)); // Now

        g_form.setValue('date', todayDate1.toISOString().slice(0, 10));
    }
}

 

For example today is 21/07/2022 and if I select ab then it is showing 21/07/2023 but it should be 20/07/2023. Also for two year it should be 20/07/2024. 

 Please suggest on this. 

Thanks in advance! 

1 ACCEPTED SOLUTION

suvro
Mega Sage

Refer below thread and checkout addDateTimeAmount part and use 365 days

https://community.servicenow.com/community?id=community_question&sys_id=38c40be9dbd8dbc01dcaf3231f9619e3

View solution in original post

3 REPLIES 3

suvro
Mega Sage

Refer below thread and checkout addDateTimeAmount part and use 365 days

https://community.servicenow.com/community?id=community_question&sys_id=38c40be9dbd8dbc01dcaf3231f9619e3

Srinivasulu Lag
ServiceNow Employee
ServiceNow Employee

Hi Brijmohan,

Please change your code with following code to add 1 year from now

new GlideDateTime(gs.nowDateTime());

gdt.addYears(1);

 g_form.setValue('date', gdt.getDate());

Please Mark ✅ Correct/helpful, if applicable.

Thanks,

Srinivasulu Laggala

Thanks for you response but as I know we can not use "gs" in client script. can we??