Some PDIs are currently unavailable, and PDI actions are paused. View the latest updates here. Read More

テーブル変換マップ 変換スクリプトでのメールアドレスチェックについて

NaoyaT
Tera Expert

こんにちは。知識不足で恐縮ですが、アドバイスいただけますと幸いです。

 

テーブル変換マップの変換スクリプトにて、タイミング「onbefore」でスクリプトを作成しています。
「質問の正規表現(question_regex.list)」のOOTBである「Email」の正規表現を利用して、
ソーステーブルのメールアドレス(u_group_email)が正規表現の形となっているかどうかをチェックしたいです。
正規表現の形になっていれば処理は流れ、正規表現の形になっていなければ処理を無視するようにしたいです。
以下スクリプトは「Email」の正規表現を直接変数に入れて実行しようとしていますが、うまくチェックができません。
ソーステーブルのメールアドレスが指定の正規表現通りの形となっているかどうかチェックする方法があればご教示いただきたいです。

 

(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {
    // メールの形式が決まった正規表現の形式になっていない場合はテーブルの更新を行わない
    // グループのメールチェック
    gs.info('*********** グループメールチェック');
    var regexPattern = "^(?![\.])[a-zA-Z0-9!#$%*\/?|^{}`~&'\+\-=_.]+(?<![.]+)@(?![\.-])[a-zA-Z0-9-_\.]+(\.[a-zA-Z0-9_]+)$";

    // チェック対象のメールアドレスを取得。
    var emailToValidate = source.u_group_email;
    if (JSUtil.notNil(emailToValidate)) {
        // 文字列を正規表現として認識。
        var reg = new RegExp(regexPattern);
        // メールがパターンと一致するか確認、true/falseを返す。
        var isValid = reg.test(emailToValidate);

        if (!isValid) {
            log.warn('不正なメール形式のため無視。取込スキップ: ' + source.u_group);
            ignore = true;
        }
    }
})(source, map, log, target);

 

2件の返信2

Tanushree Maiti
Tera Patron

Hi @NaoyaT 

 

(in English)

 

Try This:

 

 

gs.info("********** Group email check");

var regexPattern = "^[a-zA-Z0-9!#$*?_]+(?:\.[a-zA-Z0-9!#$*?_]+)*@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*\.[a-zA-Z]{2,}$";

var emailToValidate = source.u_group_email;

 

if (JSUtil.notNil(emailToValidate)) {

    var reg = new RegExp(regexPattern);

    var isValid = reg.test(emailToValidate);

   

    if (!isValid) {

        log.warn("Ignored due to invalid email format. Import skipped: " + source.u_group);

        ignore = true;

    }

}

 

 

Feature of the mentioned Regex

 

What Passess

  • test.user@domain.com (Standard format)
  • u!#$*?@domain.org (Common permitted symbols)
  • 123_abc@sub-domain.net (Numbers and hyphens)

What Fails (Expectedly)

  • .user@domain.com (Fails because it starts with a dot)
  • user@domain (Fails because it lacks a pattern at the end)

 

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti

Ankur Bawiskar
Tera Patron

@NaoyaT 

try to update script as this in your onbefore and fetch the regex from the question_regex table for Email

pass the recordSysId in script

(function runTransformScript(source, map, log, target) {

    var email = (source.u_group_email || '').toString().trim();
    if (JSUtil.nil(email))
        return;

    var gr = new GlideRecord('question_regex');
    if (!gr.get('recordSysId')) {
        log.warn('OOTB Email regex record not found.');
        return;
    }

    var pattern = gr.getValue('regex') || gr.getValue('pattern');
    if (JSUtil.nil(pattern)) {
        log.warn('Email regex pattern is empty.');
        return;
    }

    var re = new RegExp(pattern);
    if (!re.test(email)) {
        log.warn('Invalid email format, skipping import: ' + source.u_group);
        ignore = true;
    }
})(source, map, log, target);

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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