How to allow spaces before and after email IDs using regex expression?

Santhosh15
Tera Guru

Hello All,

 

I need some help from you.

I have a field called "u_email_addresses_for_targeted_email", I am adding some email id's with spaces and which is not sending emails whenever there is a space before or after the email id's.

 

Below is the script which is using in script include and calling on business rule.

 
parm4 = current.u_email_addresses_for_targeted_email.toString();
parm4 = parm4.replace( new RegExp( "[\r\n]", "gm" ), "," );
 
Please help me on this issue.
 
Thank you in Advance
 
 
10 REPLIES 10

@Santhosh15 

your script include function has issue

Use this and fix the bold part

getRecipients: function ( current ) {
var parm3 = '';
var parm4 = '';

if ( current.u_publish_to_outage_portal_email_ta_dl == true ) {
parm3 = current.u_users_for_targeted_email.toString();
parm4 = current.u_email_addresses_for_targeted_email.toString();
parm4 = parm4.replace(/\s/g, '');
}

// these lines have issue, you don't have parm1 and parm2 defined anywhere but you are using it

var final_parm1 = parm1.toString() + "," + parm3.toString();
var final_parm2 = parm2.toString() + "," + parm4.toString();

var obj = {};
obj.final_parm1 = final_parm1;
obj.final_parm2 = final_parm2;
return obj;
},

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

Hello @Ankur Bawiskar,

 

Full code is not copied in chat but i am using the parm1 and parm2 in my script include.

Total script is copied below.

 

 getRecipients: function ( current ) {
        var parm1 = [];
        var parm2 = [];
        var parm3 = '';
        var parm4 = '';

        if ( ( current.u_publish_to_outage_portal == true ) ) {
            var regionSelected = current.getValue( 'u_outage_regions' );
            var businessUnitSelected = current.getValue( 'u_affected_business_units' );

            var buArr = businessUnitSelected.split( ',' );
            var regArr = regionSelected.split( ',' );

            if ( ( regionSelected.indexOf( gs.getProperty( 'outage_global_region' ) ) != -1 ) && ( businessUnitSelected.indexOf( gs.getProperty( 'outage_global_bu' ) ) != -1 ) ) {
                // Global region & Global business unit
                var gr = new GlideRecord( 'u_outage_region_preference' );
                //gr.addEncodedQuery( 'u_preference.u_subscribed_by=region_bus^u_preference.u_user.active=true' );
                gr.addEncodedQuery( 'u_preferenceISNOTEMPTY^u_preference.u_subscribed_by=region_bus^u_preference.u_user.active=true' );
                gr.query();
                while ( gr.next() ) {
                    parm1.push( gr.u_preference.u_user.toString() );
                }
            } else if ( regionSelected.indexOf( gs.getProperty( 'outage_global_region' ) ) != -1 ) {
                // For global region, send to all users who are subscribed to the business units listed on the change
                for ( var i = 0; i < buArr.length; ++i ) {
                    var gr = new GlideRecord( 'u_outage_region_preference' );
                    //gr.addEncodedQuery( 'u_preference.u_subscribed_by=region_bus^u_preference.u_user.active=true^u_business_unitsLIKE' + buArr[i] );
                    gr.addEncodedQuery( 'u_preferenceISNOTEMPTY^u_preference.u_subscribed_by=region_bus^u_preference.u_user.active=true^u_business_unitsLIKE' + buArr[i] );                    
                    gr.query();
                    while ( gr.next() ) {
                        parm1.push( gr.u_preference.u_user.toString() );
                    }
                }
            } else if ( businessUnitSelected.indexOf( gs.getProperty( 'outage_global_bu' ) ) != -1 ) {
                // For global BU, send to all users who are subscribed to the regions listed on the change
                for ( var i = 0; i < regArr.length; ++i ) {
                    var gr = new GlideRecord( 'u_outage_region_preference' );
                    //gr.addEncodedQuery( 'u_preference.u_subscribed_by=region_bus^u_preference.u_user.active=true^u_region=' + regArr[i] );
                    gr.addEncodedQuery( 'u_preferenceISNOTEMPTY^u_preference.u_subscribed_by=region_bus^u_preference.u_user.active=true^u_region=' + regArr[i] );
                    gr.query();
                    while ( gr.next() ) {
                        parm1.push( gr.u_preference.u_user.toString() );
                    }
                }
            } else {
                // Not global BU or region
                for ( var j = 0; j < regArr.length; j++ ) {
                    for ( var k = 0; k < buArr.length; k++ ) {
                        var gr = new GlideRecord( 'u_outage_region_preference' );
                        gr.addQuery( 'u_region', regArr[j] );
                        //gr.addEncodedQuery( 'u_preference.u_subscribed_by=region_bus^u_preference.u_user.active=true^u_business_unitsLIKE' + buArr[k] );
                        gr.addEncodedQuery( 'u_preferenceISNOTEMPTY^u_preference.u_subscribed_by=region_bus^u_preference.u_user.active=true^u_business_unitsLIKE' + buArr[k] );
                        gr.query();
                        while ( gr.next() ) {
                            parm1.push( gr.u_preference.u_user.toString() );
                        }
                    }
                }
            }
            parm1 = new ArrayUtil().unique( parm1 );

            var subAppl = current.getValue( 'u_subscribable_applications_services' );
            if ( subAppl != null && subAppl != undefined ) {
                var subscribable = subAppl.length >= 0 ? subAppl.split( ',' ) : [];
                for ( var i = 0; i < subscribable.length; i++ ) {
                    var grp = new GlideRecord( 'u_outage_communication_preferences' );
                    //grp.addEncodedQuery( 'u_subscribed_by=applications^u_preference.u_user.active=true^u_outage_sub_app_servicesLIKE' + subscribable[i] );
                    grp.addEncodedQuery( 'u_subscribed_by=applications^u_user.active=true^u_outage_sub_app_servicesLIKE' + subscribable[i] );  
                    grp.query();
                    while ( grp.next() ) {
                        parm2.push( grp.u_user.toString() );
                    }
                }
            }
        }

        if ( current.u_publish_to_outage_portal_email_ta_dl == true ) {
            parm3 = current.u_users_for_targeted_email.toString();
            parm4 = current.u_email_addresses_for_targeted_email.toString();
            parm4 = parm4.replace( new RegExp( "[\r\n]", "gm" ), "," );
        }

        var final_parm1 = parm1.toString() + "," + parm3.toString();
        var final_parm2 = parm2.toString() + "," + parm4.toString();

        var obj = {};
        obj.final_parm1 = final_parm1;
        obj.final_parm2 = final_parm2;
        return obj;
    },

 

 

Thank you

@Santhosh15 

I already gave the line of code which will replace space between email

Did you try that?

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

LeenaMocey
Tera Contributor
parm4 = current.u_email_addresses_for_targeted_email.toString();

// Replace new lines with commas
parm4 = parm4.replace(/\r?\n/g, ',');

// Trim spaces before and after each email
parm4 = parm4.split(',')
    .map(email => email.trim())  // Remove spaces before and after
    .filter(email => email)      // Remove empty values
    .join(',');

Santhosh15
Tera Guru

@Ankur Bawiskar , Which lines i need to add in code?