- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-13-2022 03:43 AM
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,
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-13-2022 08:43 PM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-13-2022 04:07 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-13-2022 06:46 AM
I am getting below error - Error Message
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-13-2022 08:33 PM
Use this
var gdt =new GlideDateTime();
gdt.addYears(2);
gdt.getDate();
return gdt
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-13-2022 04:16 AM
Hi,
In the default value, you can add below logic
javascript:var gdt = new GlideDateTime(gs.nowDateTime());gdt.addYears(2);gdt.getDate();