- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-27-2022 04:30 AM
Hi everyone, I'm new to ServiceNow so apologies in advance if I'm asking something that should be obvious.
I'm trying to create a script on a Business Rule, that will search a specific form field for an email address and copy it to another field. The final objective will be to send an automatic email to that same address.
As the text and amount of emails on the original field may vary, I started by converting that field into a string, then searching for emails on it and adding them to an array.
Afterwards I request that the 1st object of that array is selected as being the correct one, but that does not work.
I also tried making the variable not an array and simply using a if statement in case it already has a value, but it does not work either.
However when I don't filter anything and just assign the value, it works, but obviously it gives the wrong email in case I have more than one.
Could you please check my code and tell me what I might be missing?
(function executeRule(current, previous /*null when async*/) {
var emailOrigin = '';
var emails = [];
var descriptionString = current.getValue('short_description');
var re = /[^< ]+(?=>)/g;
descriptionString.match(re).forEach(function(email) {
emails.push(email);
});
emailOrigin = emails[0];
//current.setValue('u_email_origin', emails[0]);
current.setValue('u_email_origin', emailOrigin);
})(current, previous);
Thanks in advance for your help!
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-27-2022 06:41 AM
Hi Carlos,
Create on before insert/update Business Rule as below, its setting first email id from short_description to origin email. then you can create notification on same table and use origin email in that
On Before Business Rule
(function executeRule(current, previous /*null when async*/ ) {
var short_description = current.u_short_description;
var result;
var mailsArr = [];
if (short_description) {
result = returnEmail(short_description); // Prints abc@demo.com,abc2@demo.com
}
// setting email in origin email
current.u_origin_email = result; //correct names this with your field
function returnEmail(text) {
return text.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)/gi)[0]; // returns first email id
}
})(current, previous);
Result :
Kindly mark correct and helpful if applicable
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-27-2022 07:32 AM
It works!
Plus your explanation helped me understand the whole process better, so I've already been able to add more functionalities.
Thank you so much Chetan!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-15-2024 02:17 AM - edited 07-15-2024 02:26 AM
Hi @Chetan Mahajan,
I configured the same business rule however I am unable get the email id from the description field to the origin email field. Could you please help me with the issue? PFA screenshots for reference.
Regards,
Jagan