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

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!

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.

JaganN971616731_0-1721034875932.png

JaganN971616731_3-1721035416148.png

JaganN971616731_2-1721034938388.png

 

Regards,

Jagan