Mail Script - How to get the first name of the variables reference type

GwynethPhoS
Tera Contributor

I need my notification email body to show the first name based on the email of a supplier contact. I have two reference field, i need to get the first name of both variables to be shown on my email notification. anyone could help me with this? thank you in advance. and its not reference to a sys_user.

5 REPLIES 5

Astrid Sapphire
ServiceNow Employee

Hi @GwynethPhoS ,

 

Could you please provide additional context around what table you are running this from, and where you are trying to pull this data from? It is hard to provide a comprehensive answer with the current level of detail.

 

If your reference field is pointing to Supplier Contact (sn_slm_contact_m2m_supplier), you will need to access the Supplier contact (supplier_contact) field on this table as it refers to the Vendor contact (vm_vdr_contact). You will want to dot-walk to the First name field (first_name).

 

If these references are declared as variables on your record, you will need to access them through the appropriate variable field. For case management tables, that is the Variables field (variables). All in all, an email script using a variable and grabbing this data would look something like the below script. Note that current is assumed to be the record your variable is on.

 

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
          /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
          /* Optional GlideRecord */ event) {

			// Replace with the name of your variable from the record producer
			var fieldName = 'supplier_contact';

			if(!gs.nil(current.variables[supplier_contact])){
				if(!gs.nil(current.variables[fieldName].supplier_contact.first_name)){
					template.print(current.variables[fieldName].supplier_contact.first_name.toString());
				} else {
					// Handle for an empty first name value, as appropriate
				}
			}

})(current, template, email, email_action, event);

 

Please let me know if you have further questions.

 


Kind regards,




Astrid Sapphire


Product Excellence Manager, Finance and Supply Chain BU

the reference tables are correct, so what should i put in my email body? tried to make mail script but not this one you provided and this is the preview of my email: "Dear, Email script render error: email script [ getOffboardedFirstName ] does not exist , Email script render error: email script [ getDelegatedFirstName ] does not exist."  need to get the first name of offboarded user (reference to sn_slm_contact_m2m_supplier) and delegated task to first name (also reference to sn_slm_contact_m2m_supplier)

Hi @GwynethPhoS,

 

There are two components to making a mail script operate as expected.

 

  1. You need to define a record in the Email Script table (sys_script_email) with a unique name that does not contain any whitespace. This record contains the code of your email script. The unofficial standard is to use snake case for names.
  2. You need to call your email script in the notification with the following notation: ${mail_script:unique_script_name}

In summary, your email body should contain "Dear ${mail_script:unique_script_name},", with the mail script in place of where the name(s) would be used.

 

The error message you are sharing indicates that the Email Script record doesn't exist, or is not accessible to the notification. The latter is unlikely, but without screenshots of the records (obscuring customer information) it is hard to say for sure.

 


Kind regards,




Astrid Sapphire


Product Excellence Manager, Finance and Supply Chain BU

Ankur Bawiskar
Tera Patron

@GwynethPhoS 

can you share screenshots of how that email looks currently and how you want?

what you tried so far and what didn't work?

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