Business Rule to extract first email address from a form field.

Carlos52
Tera Expert

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!

1 ACCEPTED SOLUTION

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 :

find_real_file.png

Kindly mark correct and helpful if applicable

View solution in original post

6 REPLIES 6

Chetan Mahajan
Kilo Sage
Kilo Sage

Hi Carlos,

                whatever you explained, a bit tricky, could you please share a snap of the form, and what operation you want on that form ? So we can help you accordingly. I think you have multiple emails in short description, and you want to populate the first email from them to email_origin field, is it correct ?

Hi Chetan,

 

Thanks for the quick reply!

 

Yes, and ideally afterwards (or inside the same script) I'll also send an email to that address.

In the following example there are 2 emails mentioned, but there could be more.

find_real_file.png

find_real_file.png

Additionally I assume I can just pass the variable when sending the email, but I'm trying to do it this way so I can use a similar function in future scripts.

Okay , let me check it on this and know you ASAP

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 :

find_real_file.png

Kindly mark correct and helpful if applicable