Hi ,
I am looking for sending Infotable data as body part of mail in html table format , but I am getting overwritten data while iterating. please see below code and screenshots too.
var result = me.getDataForWeek(); //infotable result
var tableLength = result.rows.length;
for (var x=0; x < tableLength; x++) {
var row = result.rows[x];
var rowvalue1= row.M_Key;
var rowvalue2 = row.PowerFactor;
var emailContent = "<html><body>\
<table>\
<tr><td>M_Key</td><td>Powerfactor</td></tr>\
<tr><td>"+rowvalue1+"</td><td>"+rowvalue2+"</td></tr>\
</table></body></html>"
}
Things["EMS_MailThing"].SendMessageWithAttachment({
from: " xyz@gmail.com" /* STRING */,
to: " abcd@gmail.com" /* STRING */,
cc: undefined /* STRING */,
bcc: undefined /* STRING */,
subject:"Mail with table content" /* STRING */,
//content : emailContent,
body: emailContent /* HTML */,
key | location | source | sourceType | tags | timestamp | M_Key | M_Description | ApparentPower | ConsumedPower | PowerFactor | Date |
---|---|---|---|---|---|---|---|---|---|---|---|
5.0 | 0, 0, 0 | EMS_DataTable | Thing |
Data Tag not selected
|
2020-02-03 08:09:03 |
5
|
undefined |
300
|
200
|
0.8
|
undefined |
7.0 | 0, 0, 0 | EMS_DataTable | Thing |
Data Tag not selected
|
2020-02-04 06:39:39 |
7
|
undefined |
340
|
260
|
0.9
|
undefin |
output I am getting for current code is:
M_Key |
Powerfactor |
7 |
0.9 |
Thank you
Solved! Go to Solution.
Hi,
Please add semicolon after variable declaration.
Please see below how it should look:
var emailContent="<html><body><table>";
for (var x=0; x < tableLength; x++) {
var row = result.rows[x];
var rowvalue1= row.M_Key;
var rowvalue2 = row.PowerFactor;
emailContent =emailContent+"<tr><td>M_Key</td><td>Powerfactor</td></tr><tr><td>"+rowvalue1+"</td><td>"+rowvalue2+"</td></tr>";
}
emailContent=emailContent+ "</table></body></html>";
logger.warn(emailContent);
Things["EMS_MailThing"].SendMessageWithAttachment({
from: " xyz@gmail.com" /* STRING */,
to: " abcd@gmail.com" /* STRING */,
cc: undefined /* STRING */,
bcc: undefined /* STRING */,
subject:"Mail with table content" /* STRING */,
//content : emailContent,
body: emailContent /* HTML */
....
I have added a logger.warn to write the string html in the log for troubleshooting.
Hope it helps,
Raluca Edu
I think that based on what you wrote you would want a single email with the two rows.
The error is the following: in the loop you declare a variable called emailContent. Each time you execute the loop you overwrite that variable, instead of adding to it.
try googling on how to print html table through loops in javascripts.
it would be somewhat similar to // here for loop would be replaced by yours
var i = 0;
for (i=0;i<=10;i++){
document.write("<table width=50 border=1><tr><td>" +i+ "</td><tr></table>")
}
Hi ,
as above code written i am getting overwritten data, ie first row data is overwritten by second row and this way i am getting only second row data in emailContent as i have given in output. need help ! how we can avoid it.
Thank you
Hi,
Try the following:
var emailContent="<html><body>\
<table>";
for (var x=0; x < tableLength; x++) {
var row = result.rows[x];
var rowvalue1= row.M_Key;
var rowvalue2 = row.PowerFactor;
emailContent =emailContent+"<tr><td>M_Key</td><td>Powerfactor</td></tr>\
<tr><td>"+rowvalue1+"</td><td>"+rowvalue2+"</td></tr>\"
}
emailContent=emailContent+ "</table></body></html>";
Things["EMS_MailThing"].SendMessageWithAttachment({
from: " xyz@gmail.com" /* STRING */,
to: " abcd@gmail.com" /* STRING */,
cc: undefined /* STRING */,
bcc: undefined /* STRING */,
subject:"Mail with table content" /* STRING */,
//content : emailContent,
body: emailContent /* HTML */
....
Note: I did not test the code, please correct syntax if necessary. The idea is to create HTML string in different parts, actually you are creating step by step the content of the email.
Hope it helps,
Raluca Edu
Hi,
This doesn't work at the moment.
It gives the following error attached in the screenshot.
Hi,
Please add semicolon after variable declaration.
Please see below how it should look:
var emailContent="<html><body><table>";
for (var x=0; x < tableLength; x++) {
var row = result.rows[x];
var rowvalue1= row.M_Key;
var rowvalue2 = row.PowerFactor;
emailContent =emailContent+"<tr><td>M_Key</td><td>Powerfactor</td></tr><tr><td>"+rowvalue1+"</td><td>"+rowvalue2+"</td></tr>";
}
emailContent=emailContent+ "</table></body></html>";
logger.warn(emailContent);
Things["EMS_MailThing"].SendMessageWithAttachment({
from: " xyz@gmail.com" /* STRING */,
to: " abcd@gmail.com" /* STRING */,
cc: undefined /* STRING */,
bcc: undefined /* STRING */,
subject:"Mail with table content" /* STRING */,
//content : emailContent,
body: emailContent /* HTML */
....
I have added a logger.warn to write the string html in the log for troubleshooting.
Hope it helps,
Raluca Edu