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.

How to get Users email from Group to "To" address of Email Client Template from the Incident Form.

Neelavathi M
Tera Contributor

Hi All,

 

I have a  requirement to create a Email client template which must have Emails from "XYZ" group , assume "XYZ" group has 10 users in it so in "TO" address of Template , these 10 users Email ID has to be populated.

 

NeelavathiM_0-1691155438837.png

 

To Achieve this ,i tried configuring "Script Include" and calling that script include in email client template.

 

Script Include:

var GetMIMCommunicationrecipients = Class.create();
GetMIMCommunicationrecipients.prototype = {
    initialize: function() {},

    getMIMRecipients: function() {

        var emailList = [];
        var gr = new GlideRecord('sys_user_grmember');
        gr.addEncodedQuery('group.sys_idSTARTSWITH994098798713291066d4766acebb35bc');
        gr.query();
        if(gr.next()) {
			gs.log('count'+gr.getRowCount());
            emailList.push(gr.user.email);
        }
		return emailList;
    },

    type: 'GetMIMCommunicationrecipients'
};
 
Email client template:
NeelavathiM_1-1691155635183.png

 

Can any one please suggest how to achieve this.

Appreciated your response Thanks. 

1 ACCEPTED SOLUTION

Elijah Aromola
Mega Sage

Can you share your template record? You must put the call in there.

javascript: new GetMIMCommunicationrecipients().getMIMRecipients()

 

Also the script must return a comma separated string of email addresses, so you would update your method to the below.

 

var GetMIMCommunicationrecipients = Class.create();
GetMIMCommunicationrecipients.prototype = {
    initialize: function() {},

    getMIMRecipients: function() {

        var emailList = [];
        var gr = new GlideRecord('sys_user_grmember');
        gr.addEncodedQuery('group=994098798713291066d4766acebb35bc');
        gr.query();
        while(gr.next()) {
            emailList.push(gr.user.email);
        }
		return emailList.join(",");
    },

    type: 'GetMIMCommunicationrecipients'
};

 

View solution in original post

4 REPLIES 4

Elijah Aromola
Mega Sage

Can you share your template record? You must put the call in there.

javascript: new GetMIMCommunicationrecipients().getMIMRecipients()

 

Also the script must return a comma separated string of email addresses, so you would update your method to the below.

 

var GetMIMCommunicationrecipients = Class.create();
GetMIMCommunicationrecipients.prototype = {
    initialize: function() {},

    getMIMRecipients: function() {

        var emailList = [];
        var gr = new GlideRecord('sys_user_grmember');
        gr.addEncodedQuery('group=994098798713291066d4766acebb35bc');
        gr.query();
        while(gr.next()) {
            emailList.push(gr.user.email);
        }
		return emailList.join(",");
    },

    type: 'GetMIMCommunicationrecipients'
};

 

Hi @Elijah Aromola ,

 

Thanks for your Reply,

 

This is the Email Client Template.

 

NeelavathiM_1-1691165026654.png

 

 

And after adding below line, i am able to get only one email on Template, please let me know where the changes has to be made.

"return emailList.join(",");"

 

NeelavathiM_0-1691165002807.png

 

Thanks.

Hi @Elijah Aromola ,

 

Yes , it worked , thanks for your timely help.

Elijah Aromola
Mega Sage

You need to change your code from an "if" to a "while". See my code from above.