Document Template Script for Current Date (MMMM D, YYYY)

ErinF
Tera Expert

I am creating a document script to insert current date in "January 23, 2023" format. I have created the following document script and called within the HTML template ${template_script:CurrentDate}. However, I am receiving null when previewing the document. Any advice on where I have gone wrong? 

 

(function runTemplateScript(target /*GlideRecord for target task*/ ) {

	//Add your code here to return the dynamic content for template
	var cd = new GlideDate();
		cd.getByFormat("MMMM D, YYYY");
	
		template.print(cd);
	
})(target);

 

Note: I am working in a Human Resources: Core scoped app  

1 ACCEPTED SOLUTION

ErinF
Tera Expert

I ended up unable to use template.print in the document template script and used this successfully: 

(function runTemplateScript(target /*GlideRecord for target task*/ ) {
	//Add your code here to return the dynamic content for template	
	var cd = new GlideDate();
	
	return (cd.getByFormat('MMMM dd, YYYY'));
})(target); 

View solution in original post

4 REPLIES 4

Sai Shravan
Mega Sage

Hi @ErinF , 

Can you check with the below code.

(function runTemplateScript(target /*GlideRecord for target task*/ ) {
	var cd = new GlideDate();
	cd.setDisplayValue(gs.nowDateTime());
	template.print(cd.getByFormat("MMMM D, YYYY"));
})(target);

 

Just make sure that the scoped app has access to the "GlideDate" class and the "gs" global object, which are both provided by the ServiceNow platform. If these are not available in the scoped app, the script will not work.

Regards,
Shravan
Please mark it as helpful if this helps you.

Regards,
Shravan
Please mark this as helpful and correct answer, if this helps you

Hi Sai. Thank you for your comment. I appreciate it. I am getting the error that gs.nowDateTime cannot be used within my scoped app (Human Resources:Core). Any other ideas? Thanks. 

Hi @ErinF ,

Can you give it a try by using GlideDateTime.

-> This will give you the current date and time in the same manner as gs.nowDateTime().

Regards,
Shravan
Please mark this as helpful and correct answer, if this helps you

ErinF
Tera Expert

I ended up unable to use template.print in the document template script and used this successfully: 

(function runTemplateScript(target /*GlideRecord for target task*/ ) {
	//Add your code here to return the dynamic content for template	
	var cd = new GlideDate();
	
	return (cd.getByFormat('MMMM dd, YYYY'));
})(target);