How can i send multiple e-mails inside a Workflow Script, getting a form field as parameter?

Leonardo Lago
Tera Contributor

Hello! I am trying to send multiple e-mails getting a single String as a parameter from a form field.

I could make the code to get a single e-mail and send it through notifications and events, like this:

------------------------------------------------------------------------------------------

(function() {
    
    gs.eventQueue('send_email', current, current.variables.email);

})();

------------------------------------------------------------------------------------------

I am making this through a Workflow inside ServiceNow, and the code above worked very well.

My current code (see below) is trying to split the incoming String from the field in a Array, trying to run this array inside a loop to deliver all e-mails, but this solution is not working:

------------------------------------------------------------------------------------------

var emailID = g_form.getValue('email');
var spl = emailID.split(',');
var len = spl.length;

for(var i=0; i<len;i++){

gs.eventQueue('send_email', current, spl[i]);

}

------------------------------------------------------------------------------------------

I don't know where is the problem of my code, if is in the g_form.getValue, or in the split function, or even in the For Loop.

I really appreciate some help, thank you.

 

 

1 ACCEPTED SOLUTION

Markus Kraus
Kilo Sage

You are mixing client side (g_form) and server side (gs.eventQueue) code. 

Rather use current.variables.email.toString() instead of g_form.getValue('email').

Note: Email notifications will take a comma separated list of emails anyways, the split is not really required.

View solution in original post

3 REPLIES 3

Markus Kraus
Kilo Sage

You are mixing client side (g_form) and server side (gs.eventQueue) code. 

Rather use current.variables.email.toString() instead of g_form.getValue('email').

Note: Email notifications will take a comma separated list of emails anyways, the split is not really required.

Thank you very much, your solution worked great for me and solved my problem.
Just to clarify a doubt, every Script present in Workflow is classified as a Server-side Script?

Yes that’s correct. Everything that runs in any workflow script is a server side script.