cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Learn all about the Community Ranking System, a fun gamification element of the PTC Community. X

Retrieve multiple email addresses from data table and insert as input into email service

JP_9655053
4-Participant

Retrieve multiple email addresses from data table and insert as input into email service

I have a service where I retrieve one to many email addresses from my data table based on certain inputs. How do insert these into the send message service? It currently gives me the following error,

 

"javascript service. Message ::Invalid or malformed email address entered."

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
nmutter
14-Alexandrite
(To:JP_9655053)

I'm not so clear what you want to do as the code you provided seems to be missing some comments and brackets.

 

But creating a string like "email1;email2;email3;" for the SendMessage service you can just do

var tableLength = result.rows.length;
var emailReceivers = ""; // empty mail string
for (var x = 0; x < tableLength; x++) {
    var row = result.rows[x];
    emailReceivers += row.Email + ";"; // append "email;" to receivers
}

if (emailReceivers.length > 0)
    emailReceivers = emailReceivers.slice(0, -1); // remove last ;

Hope this helps.

View solution in original post

7 REPLIES 7

Article - "How to send an email to multiple recipients via ThingWorx Mail Extension": https://www.ptc.com/en/support/article/cs310001 

JP_9655053
4-Participant
(To:VladimirN)

Thank you, but I need help on the for loop to go through my results from the data table and put the semicolon in between each result.
nmutter
14-Alexandrite
(To:JP_9655053)

can you share some code so we can help with it?

JP_9655053
4-Participant
(To:nmutter)

Bits and pieces of this service work. The first service to return the email address based on the inputs works, but it retrieves them as an infotable. Everything else in between works to find failures and create the email works. It just wont send my email since i'm not putting in the emails from the first service correctly. Hardcoding the emails into the address input will work as well but I need to select the emails based on area and shift.

 

//First gather email list based on area and shift of the audit
// result: INFOTABLE dataShape: "AuditEmailList.DS"
var EmailList = Things["AuditEmailList.DT"].GetEmailList({
Shift: Shift /* INTEGER */,
Area: Area /* STRING */
});

var params = {
t:EmailList /* INFOTABLE */,
columns: "Email" /* STRING */
};

var result = Resources["InfoTableFunctions"].Distinct(params);
var tableLength = result.rows.length;

for (var x=0; x < tableLength; x++) {
var row = result.rows[x];

//First check for and get failure information by AuditID
var failureinfo = Things["MSSQLConnector.AuditForms"].GetFailInfoPieceCheck({
AuditID: AuditID /* INTEGER */
});
var params = {
t:failureinfo /* INFOTABLE */,
columns: "UserID,TOE,Shift,LineID,Item,PassFailNa,Notes" /* STRING */
};

//Second, if failures exist send one email with all the failure info from infotable above.
result: INFOTABLE
var result = Resources["InfoTableFunctions"].Distinct(params);
var tableLength = result.rows.length;
var emailContent="<html><body><table>";


Add heading row (column names)
emailContent = emailContent + "<tr><th>UserID</th><th>TOE</th><th>Shift</th><th>LineID</th><th>Item</th>";
emailContent = emailContent + "<th>PassFailNa</th><th>Notes</th>";
emailContent = emailContent + "</tr>"; // End heading row

if (tableLength > 0) {

for (var x=0; x < tableLength; x++) {
var row = result.rows[x]; // Get Row Data

emailContent =emailContent +"<tr><td>"+row.UserID+"</td><td>"+row.TOE+"</td><td>"+row.Shift+"</td>";
emailContent =emailContent + "<td>"+row.LineID+"</td><td>"+row.Item+"</td><td>"+row.PassFailNa+"</td>";
emailContent =emailContent + "<td>"+row.Notes+"</td>";
// Add closing tag to row.
emailContent =emailContent + "</tr>";

} // Next Row

emailContent = emailContent + "</table></body></html>"; // Add Closing HTML tags to body content.

Things["MailServer"].SendMessage({
cc: undefined /* STRING */,
bcc: undefined /* STRING */,
subject: "Piece Check Audit Failure" /* STRING */,
from: undefined /* STRING */,
to: EmailList,
body: emailContent /* HTML */
});
}
}

nmutter
14-Alexandrite
(To:JP_9655053)

I'm not so clear what you want to do as the code you provided seems to be missing some comments and brackets.

 

But creating a string like "email1;email2;email3;" for the SendMessage service you can just do

var tableLength = result.rows.length;
var emailReceivers = ""; // empty mail string
for (var x = 0; x < tableLength; x++) {
    var row = result.rows[x];
    emailReceivers += row.Email + ";"; // append "email;" to receivers
}

if (emailReceivers.length > 0)
    emailReceivers = emailReceivers.slice(0, -1); // remove last ;

Hope this helps.

JP_9655053
4-Participant
(To:nmutter)

Thank you very much for this. Yes sorry I didn’t comment out what I was wanting to do very well. I’m excited to try your suggestion and will give feedback soon! Thanks again!
JP_9655053
4-Participant
(To:nmutter)

Ok so thanks again for the solution! It does work by pulling up the correct emails from my table and converts them to a string separated by ;. Just two side notes. One, everything does work and goes through, however I get the following error in the application log 

 

"class com.thingworx.dsl.engine.adapters.ThingworxInfoTableAdapter cannot be cast to class com.thingworx.types.InfoTable (com.thingworx.dsl.engine.adapters.ThingworxInfoTableAdapter and com.thingworx.types.InfoTable are in unnamed module of loader org.apache.catalina.loader.ParallelWebappClassLoader @6de54b40)"

 

Two, the slice removed all ;'s out of the result. I commented it out of the service and just left last one and email still send so no big deal. Thanks again for your help, I really appreciate it.

Top Tags