How to create client script to auto populate the date field based on current date + 7days

nandhini muthu
Tera Contributor
 
1 ACCEPTED SOLUTION

Runjay Patel
Giga Sage

Hi @nandhini muthu ,

 

You can use calculated filed option like below.

 

RunjayPatel_0-1736344249066.png

 

Code:

var gdt = new GlideDateTime(); 
	gdt.addDaysUTC(7); 
	gdt.getDate();
	return gdt;  // return the calculated value

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

View solution in original post

8 REPLIES 8

Ashish Parab
Mega Sage

Hello @nandhini muthu ,

 

You can try the below onLoad client script:

function onLoad() {
    // Get the current date
    var currentDate = new Date();

    // Add 7 days to the current date
    currentDate.setDate(currentDate.getDate() + 7);

    // Format the date to match the ServiceNow date format (yyyy-MM-dd)
    var formattedDate = currentDate.toISOString().split('T')[0]; // Formats as yyyy-MM-dd

    // Set the value of the date field (replace 'date_field' with your field name)
    g_form.setValue('current_date_plus_7', formattedDate);
}

 

Output -

AshishParab_0-1736341610506.png

 

Please mark this as "correct" and "helpful" if you feel this answer helped you in anyway.

 

Thanks and Regards,

Ashish

 

Ankur Bawiskar
Tera Patron
Tera Patron

@nandhini muthu 

no client script required

you can use default value with this

javascript: var gdt = new GlideDateTime(); gdt.addDaysUTC(7); gdt.getDate();

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

@nandhini muthu 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Runjay Patel
Giga Sage

Hi @nandhini muthu ,

 

You can use calculated filed option like below.

 

RunjayPatel_0-1736344249066.png

 

Code:

var gdt = new GlideDateTime(); 
	gdt.addDaysUTC(7); 
	gdt.getDate();
	return gdt;  // return the calculated value

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------