Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Recipient email address and the mail script

tsoct
Tera Guru

Hello all,

I'm attempting to generate dynamic notification content based on the recipient's email domain.

tsoct_0-1690457361117.png

I wrote a mail script to meet the need, however it isn't working. Is there anything I've missed?

Thank you

 

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {

	var checkRecipient = email.recipients;
  
    if (checkRecipient.indexOf('@gmail.com') > -1) 
    {
        template.print('<div class=font><p><font size="2">content for gmail accounts</font></p></div>');
    } else {
        template.print('<div class=font><p><font size="2">content for internal users</font></p></div>');
    }

})(current, template, email, email_action, event);

 

3 REPLIES 3

sushantmalsure
Mega Sage

Hi @tsoct ,

I think its the issue with 'email.recipients' but i have tried something similar on my PDI and its working fine.

Just change your mail script to following and check if that works:

 

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {

    var arrUtil = new ArrayUtil();
    var emails = [];
    //Get sys_ids of reference fields
    var ids = [];
    if (email_action.recipient_fields) {
        var r = email_action.recipient_fields.split(',');
        for (var x = 0; x < r.length; x++)
            ids.push(current[r[x]]);
    }
    //Get user list
    ids.push(email_action.recipient_users);
    //Get group list
    ids.push(email_action.recipient_groups);
    //If parm1 contains recipient, get parm1 value
    if (email_action.event_parm_1)
        ids.push(event.parm1);
    //If parm2 contains recipient, get parm2 value
    if (email_action.event_parm_2)
        ids.push(event.parm2);
    //Re-split the array by commas
    ids = ids.toString().split(',');

    
    var checkRecipient = ids.join();

    if (checkRecipient.indexOf('@gmail.com') > -1) {
        template.print('<div class=font><p><font size="2">content for gmail accounts</font></p></div>');
    } else {
        template.print('<div class=font><p><font size="2">content for internal users</font></p></div>');
    }

})(current, template, email, email_action, event);

 

 

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Regards,Sushant Malsure

Hello, thanks for the script. ids seems to return sys_id of the users. How should i convert them to email? Since some of the users in our system registered with gmail too.

Hi @tsoct : Please ignore my previous comment.

Here is updated script to convert IDs to email and then check the gmail account.

 

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {

    var arrUtil = new ArrayUtil();
    var emails = [];
    //Get sys_ids of reference fields
    var ids = [];
    if (email_action.recipient_fields) {
        var r = email_action.recipient_fields.split(',');
        for (var x = 0; x < r.length; x++)
            ids.push(current[r[x]]);
    }
    //Get user list
    ids.push(email_action.recipient_users);
    //Get group list
    ids.push(email_action.recipient_groups);
    //If parm1 contains recipient, get parm1 value
    if (email_action.event_parm_1)
        ids.push(event.parm1);
    //If parm2 contains recipient, get parm2 value
    if (email_action.event_parm_2)
        ids.push(event.parm2);
    //Re-split the array by commas
    ids = ids.toString().split(',');
	
	for(var y =0; y<ids.length; y++){
		var getEmail = new GlideRecord('sys_user');
		getEmail.get(ids[y]);
		
		emails.push(getEmail.email);
	}
    var checkRecipient = emails.join();
    if (checkRecipient.indexOf('@gmail.com') > -1) {
        template.print('<div class=font><p><font size="2">content for gmail accounts</font></p></div>');
    } else {
        template.print('<div class=font><p><font size="2">content for internal users</font></p></div>');
    }

})(current, template, email, email_action, event);

 

Try and let me know how it goes.

 

 

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Regards,Sushant Malsure