Default value to be show as 2 years from current date in DD/MM/YYYY format ;while creating new record in table

Abhradipa Baner
Kilo Contributor

I want to set Default value of "Expiry Date " field is +2 years from current date in DD-MM-YYYY format. 

I have given Type of field as DATE for "Expiry Date " and written the below OnLoad() Client Script -

function onLoad() {
//Type appropriate comment here, and begin script below
if(g_form.isNewRecord()){
var todayDate = new Date(new Date().setFullYear(new Date().getFullYear() + 2)); // Now
g_form.setValue('expiry_date', todayDate.toISOString().slice(0,10));
}
}

// Output : Invalid Date while submitting new request.

Then I got to know "Date" type ideally took YYYY-MM-DD format ;So it is causing error as an Invalid Date. 

But I want to set it by default in DD-MM-YYYY format as this is our company's default format . 

How I can fix this issue? Please guide .

Regards,

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

the date format would be shown as per logged in user's date format

You need not worry on that.

Let the system set the date in system format yyyy-mm-DD and system will take care of other formats

you can use default value in that date field

javascript: var dt; var gdt = new GlideDateTime(); gdt.addYearsLocalTime(2); dt = gdt.getLocalDate(); dt;

Regards
Ankur

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

View solution in original post

8 REPLIES 8

suvro
Mega Sage
Mega Sage

You can use calculated Value ....

Go to the dictionary field--> Click on advanced view --> under Calculated Value tab set the field with below script

var gdt =GlideDateTime(gs.nowDateTime());

gdt.addYears(2);

gdt.getDate();

return gdt

I am getting below error - Error Message

Function nowDateTime is not allowed in scope x_gmss_academyreq. Use GlideDateTime instead: new GlideDateTime().getDisplayValue(). 
 
How can I fix this?

Use this

 

var gdt =new GlideDateTime();

gdt.addYears(2);

gdt.getDate();

return gdt

Kalyani Jangam1
Mega Sage
Mega Sage

Hi,

In the default value, you can add below logic

javascript:var gdt = new GlideDateTime(gs.nowDateTime());gdt.addYears(2);gdt.getDate();