11-26-2021 06:45 AM
Can we create two separate tables in a single mail? Can anyone provide me the logic as to how it can be achieved?
Solved! Go to Solution.
11-26-2021 08:27 AM
Hi @aditya.gupta41,
Yes you can absolutely create as many tables as you want. Let me give you an example.
Let’s say this is our dataset:
[
{
"people": [
{
"name": "John",
"age": 30,
"car": null
},
{
"name": "Robert",
"age": 25,
"car": "Ford Mustang"
},
{
"name": "Leon",
"age": 38,
"car": "Jeep"
},
{
"name": "Elizabeth",
"age": 21,
"car": null
}
],
"items": [
{
"item": "Sunglasses",
"price": "$5.00"
},
{
"item": "Smartwatch",
"price": "$22.00"
},
{
"item": "Wireless Headphones",
"price": "$18.00"
}
]
}
]
Now, if we want to create two tables, I suggest you to use the XML Generator snap together with Apache Velocity.
From this dataset we can easily create two tables and even style them with the XML generator snap.
The following code goes into the XML generator snap.
<html>
<head>
<style>
table {
width: 100%;
}
thead th {
background: black;
color: white;
}
tbody tr:nth-child(even) {
background: #D9D9D9;
}
td {
padding: 5px;
}
</style>
</head>
<body>
<h1>This is the 'people' table</h1>
<table>
<thead>
<tr>
<th>name</th>
<th>age</th>
<th>car</th>
</tr>
</thead>
<tbody>
#foreach ( $person in $people )
<tr>
<td>$person.name</td>
<td>$person.age</td>
<td>$person.car</td>
</tr>
#end
</tbody>
</table>
<h1>This is the 'items' table</h1>
<table>
<thead>
<tr>
<th>item</th>
<th>price</th>
</tr>
</thead>
<tbody>
#foreach ( $item in $items )
<tr>
<td>$item.item</td>
<td>$item.price</td>
</tr>
#end
</tbody>
</table>
</body>
</html>
Next, just place an Email Sender snap, add your account settings and in the property “Email type” select “HTML text”. Once you set this, just enable the expression for the “Template body” property and use the following expression to remove the “\n” characters.
$xml.replaceAll("\n", "")
And this is the preview of the two tables created with the sample dataset.
11-26-2021 07:46 AM
Hey @aditya.gupta41 ,
By tables you mean HTML tables?
11-27-2021 02:27 PM
Yes…there are two sets of data coming through in a json format
11-26-2021 08:27 AM
Hi @aditya.gupta41,
Yes you can absolutely create as many tables as you want. Let me give you an example.
Let’s say this is our dataset:
[
{
"people": [
{
"name": "John",
"age": 30,
"car": null
},
{
"name": "Robert",
"age": 25,
"car": "Ford Mustang"
},
{
"name": "Leon",
"age": 38,
"car": "Jeep"
},
{
"name": "Elizabeth",
"age": 21,
"car": null
}
],
"items": [
{
"item": "Sunglasses",
"price": "$5.00"
},
{
"item": "Smartwatch",
"price": "$22.00"
},
{
"item": "Wireless Headphones",
"price": "$18.00"
}
]
}
]
Now, if we want to create two tables, I suggest you to use the XML Generator snap together with Apache Velocity.
From this dataset we can easily create two tables and even style them with the XML generator snap.
The following code goes into the XML generator snap.
<html>
<head>
<style>
table {
width: 100%;
}
thead th {
background: black;
color: white;
}
tbody tr:nth-child(even) {
background: #D9D9D9;
}
td {
padding: 5px;
}
</style>
</head>
<body>
<h1>This is the 'people' table</h1>
<table>
<thead>
<tr>
<th>name</th>
<th>age</th>
<th>car</th>
</tr>
</thead>
<tbody>
#foreach ( $person in $people )
<tr>
<td>$person.name</td>
<td>$person.age</td>
<td>$person.car</td>
</tr>
#end
</tbody>
</table>
<h1>This is the 'items' table</h1>
<table>
<thead>
<tr>
<th>item</th>
<th>price</th>
</tr>
</thead>
<tbody>
#foreach ( $item in $items )
<tr>
<td>$item.item</td>
<td>$item.price</td>
</tr>
#end
</tbody>
</table>
</body>
</html>
Next, just place an Email Sender snap, add your account settings and in the property “Email type” select “HTML text”. Once you set this, just enable the expression for the “Template body” property and use the following expression to remove the “\n” characters.
$xml.replaceAll("\n", "")
And this is the preview of the two tables created with the sample dataset.
11-27-2021 03:41 PM
Thanks. This worked for me 🙂