email script in email template

Vedavalli
Tera Contributor

When I'm trying to use my email script in email template
I'm getting 

Following variables are not valid or defined: ${mail_script:missing_fields_data}
Error Message
Invalid update

can I know the reason for this 

Thank you
1 ACCEPTED SOLUTION

@Vedavalli 

Thank you for marking my response as helpful.

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

View solution in original post

12 REPLIES 12

Shree_G
Kilo Sage

Hello @Vedavalli ,

 

This typically means that either the mail script does not exist or there is a script error in it.

 

Try to locate the script :

> System Notification > Email > Notification Email Scripts -> search for the Email script "missing_fields_data".

 

If the script exist, open the script and resolve any errors. Try Background script if needed. OR

share the script to know more about the errors.


If this solution helped resolve your issue, please consider marking it as helpful or correct.
This will assist others in finding the solution faster and close the thread.

Hi @Shree_G 

(function run(current, template, email, email_action, event) {
   var missingFields = [];
   // 3 is typically the value for 'On Hold' in Incident state (confirm in your instance)
   if (current.getValue('state') == '3') {
       if (!current.getValue('hold_reason')) {
           missingFields.push("On Hold Reason");
       }
       
       if (!current.getValue('close_notes')) { // Adjust if your resolution field name is different
           missingFields.push("Resolution Notes");
       }
       if (missingFields.length > 0) {
           template.print("The following mandatory fields are missing:<ul>");
           for (var i = 0; i < missingFields.length; i++) {
               template.print("<li>" + missingFields[i] + "</li>");
           }
           template.print("</ul>");
       } else {
           template.print("All mandatory fields are filled.");
       }
   } else {
       template.print("Incident is not in 'On Hold' state.");
   }
})(current, template, email, email_action, event);

this is the script

@Vedavalli 

try to debug if you are configuring it correctly

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

Shree_G
Kilo Sage

Hello @Vedavalli ,

 

The script is fine and without error. I tried on "incident" table and it worked as expected:

 

Shree_G_0-1746690787553.png

 

Shree_G_1-1746690852315.png

Re-check your configurations and on which table are you using it.

 

Also, why are you sending a notification to a user when the fields are blank. You can use Client scripts to do such validations instead of Email script.

 


If this solution helped resolve your issue, please consider marking it as helpful or correct.
This will assist others in finding the solution faster and close the thread.