The CreatorCon Call for Content is officially open! Get started here.

Transform map script to extract email ID from Description

JaganN971616731
Tera Contributor

Hello Community,

 

I have a requirement to extract Email ID from Description field. I have added a field called origin mail to store the extracted email id value(u_origin_mail). I have written a script however it is not working. Can anyone help me with the script to extract the email id from description using onAfter script? Your response would be appreciated.

Here is the code:

answer = (function transformRow(source,target,map,log,isUpdate) {
            var description = source.description;
            var result;
            var mailsArr = [];
            if (description) {
                result = returnEmail(description);
            }
            target.u_origin_email = result;

            function returnEmail(text) {
                return text.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-{2,}]+)/gi)[0];
            }

            })(source);
2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@JaganN971616731 

Did you debug if you are able to extract email or not? If this is working then check next points

2 options to achieve your requirement

1) you can use onBefore transform script for this instead of onAfter

OR

2) If you use onAfter then you need to use target.update() to actually update the record after setting the value

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Satishkumar B
Giga Sage
Giga Sage

 @JaganN971616731 The regex used in returnEmail has a slight mistake. The {2,} part in the regex should be replaced with \w{2,} to properly match domain parts of the email.

Make sure your script is used in an appropriate onAfter script context where source and target are correctly defined.

 

Please mark my response as helpful 👍and accept the solution if it helped you