The Zurich release has arrived! Interested in new features and functionalities? Click here for more

MAIL SCRIPT!!

SandeepKSingh
Kilo Sage

Why is template.print() required for the notification to send? Shouldn’t email.setSubject() and email.setBody() be enough to trigger it?

3 ACCEPTED SOLUTIONS

Ravi Gaurav
Giga Sage
Giga Sage

Hi @SandeepKSingh 

In ServiceNow mail scripts, there are two distinct pieces at play:

  1. email object methods (setSubject(), setBody(), addAddress(), etc.)

    • These modify the email metadata (subject, recipients, body text).

    • However, this by itself does not produce any output unless the notification engine has something to render into the email body.

  2. template.print()

    • This is what actually writes content into the email body when the mail script is executed.

    • Without at least one template.print(), the script doesn’t produce any output stream for the notification engine.

    • Result: ServiceNow thinks the email body is empty -> no email is generated/sent.

That’s why when you added even a dummy template.print('Hello …'), the notification suddenly worked - because now there was tangible body content.

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


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

 YouTube: https://www.youtube.com/@learnservicenowwithravi
 LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/

View solution in original post

 

Shouldn’t email.setSubject() / email.setBody() be enough?

Intuitively, yes — but ServiceNow’s notification engine treats the mail script slightly differently:

  • email.setSubject() → changes the subject, but doesn’t mark the notification as having “content”.

  • email.setBody() → sets body text, but still requires some rendered output (like from template.print()) to make the notification engine commit the message.

  • template.print() → signals “this script has produced output”, which triggers ServiceNow to build and send the email.

Best practice

If you’re writing a pure mail script (not mixing with notification templates):

  • Always include at least one template.print() — even if all you want is to manipulate email object.

  • If you don’t want visible output, you can do:

 

template.print('');

This ensures the notification is triggered while keeping the body fully controlled via email.setBody().

So in short:
template.print() is the mechanism that tells ServiceNow “I’ve generated output”. Without it, the notification pipeline considers the message empty and skips sending.

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


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

 YouTube: https://www.youtube.com/@learnservicenowwithravi
 LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/

View solution in original post

Bhuvan
Kilo Patron

@SandeepKSingh 

 

If you are using email.setBody() to pass the email body, you need not use template.print() and sending email should work. If you do not have email body, it would expect TemplatePrinter API to set the email body

 

https://www.servicenow.com/docs/bundle/zurich-api-reference/page/app-store/dev_portal/API_reference/...

 

You can refer below threads for more information

 

https://www.servicenow.com/community/incident-management-forum/question-related-to-mail-script/m-p/3...

 

https://www.servicenow.com/community/itsm-forum/template-print-font-setting-for-comments-work-notes/...

 

If this helped to answer your query, please mark it helpful & accept the solution.

 

Thanks,

Bhuvan

View solution in original post

3 REPLIES 3

Ravi Gaurav
Giga Sage
Giga Sage

Hi @SandeepKSingh 

In ServiceNow mail scripts, there are two distinct pieces at play:

  1. email object methods (setSubject(), setBody(), addAddress(), etc.)

    • These modify the email metadata (subject, recipients, body text).

    • However, this by itself does not produce any output unless the notification engine has something to render into the email body.

  2. template.print()

    • This is what actually writes content into the email body when the mail script is executed.

    • Without at least one template.print(), the script doesn’t produce any output stream for the notification engine.

    • Result: ServiceNow thinks the email body is empty -> no email is generated/sent.

That’s why when you added even a dummy template.print('Hello …'), the notification suddenly worked - because now there was tangible body content.

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


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

 YouTube: https://www.youtube.com/@learnservicenowwithravi
 LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/

 

Shouldn’t email.setSubject() / email.setBody() be enough?

Intuitively, yes — but ServiceNow’s notification engine treats the mail script slightly differently:

  • email.setSubject() → changes the subject, but doesn’t mark the notification as having “content”.

  • email.setBody() → sets body text, but still requires some rendered output (like from template.print()) to make the notification engine commit the message.

  • template.print() → signals “this script has produced output”, which triggers ServiceNow to build and send the email.

Best practice

If you’re writing a pure mail script (not mixing with notification templates):

  • Always include at least one template.print() — even if all you want is to manipulate email object.

  • If you don’t want visible output, you can do:

 

template.print('');

This ensures the notification is triggered while keeping the body fully controlled via email.setBody().

So in short:
template.print() is the mechanism that tells ServiceNow “I’ve generated output”. Without it, the notification pipeline considers the message empty and skips sending.

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


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

 YouTube: https://www.youtube.com/@learnservicenowwithravi
 LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/

Bhuvan
Kilo Patron

@SandeepKSingh 

 

If you are using email.setBody() to pass the email body, you need not use template.print() and sending email should work. If you do not have email body, it would expect TemplatePrinter API to set the email body

 

https://www.servicenow.com/docs/bundle/zurich-api-reference/page/app-store/dev_portal/API_reference/...

 

You can refer below threads for more information

 

https://www.servicenow.com/community/incident-management-forum/question-related-to-mail-script/m-p/3...

 

https://www.servicenow.com/community/itsm-forum/template-print-font-setting-for-comments-work-notes/...

 

If this helped to answer your query, please mark it helpful & accept the solution.

 

Thanks,

Bhuvan