Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Split string field by space and convert to email address in mail script

aaronbartosch
Tera Contributor

Hello all, 

I have a requirement to take a string field that is populated via our integration with Workday containing name data and split that field based on space as a delimiter and generate an email address to send out to a vendor.

find_real_file.png

the field on our SC_REQ_ITEM table is u_name. 

As an example, this field will contain:   Bob Smith

we need to convert that and email out what the email address will be:   bob.smith@email.com

I've never tried to split a field based on space before especially in a mail script, so not sure this will even work.  but, below is the mail script i'm attempting to run.  unfortunately, the notification just shows a blank line where this mail script is, so i'm thinking it's failing before the template.print().

any thoughts would be greatly appreciated.

(function runMailScript(current, template, email, email_action, event) {

var nameID = sc_req_item.u_name + '';
	var nameSplit = nameID.split(" ");
	template.print('<p><font size="5" color="#f47641" face="helvetica">');
	template.print("email: " + nameSplit[0] + "." + nameSplit[1] + "@maac.com");
	template.print('</font></p>');
})(current, template, email, email_action, event);
1 ACCEPTED SOLUTION

Jon Barnes
Kilo Sage

I think it should work, but sc_req_item won't be assigned to anything in your script, perhaps you are trying to reference the current record?

 

(function runMailScript(current, template, email, email_action, event) {
  var nameID = current.u_name + '';
  var nameSplit = nameID.split(" ");
  template.print('<p><font size="5" color="#f47641" face="helvetica">');
  template.print("email: " + nameSplit[0] + "." + nameSplit[1] + "@maac.com");
  template.print('</font></p>');
})(current, template, email, email_action, event);

 

Also, you may want to handle the case where there is no space in the name, in which case nameSplit[1] will be undefined and cause issues.

View solution in original post

3 REPLIES 3

Jon Barnes
Kilo Sage

I think it should work, but sc_req_item won't be assigned to anything in your script, perhaps you are trying to reference the current record?

 

(function runMailScript(current, template, email, email_action, event) {
  var nameID = current.u_name + '';
  var nameSplit = nameID.split(" ");
  template.print('<p><font size="5" color="#f47641" face="helvetica">');
  template.print("email: " + nameSplit[0] + "." + nameSplit[1] + "@maac.com");
  template.print('</font></p>');
})(current, template, email, email_action, event);

 

Also, you may want to handle the case where there is no space in the name, in which case nameSplit[1] will be undefined and cause issues.

aaronbartosch
Tera Contributor

That was it.  I got so close.  thanks for the second eyes!

sure thing!