cancel
Showing results for 
Search instead for 
Did you mean: 

Creating two tables in single mail

aditya_gupta41
Contributor

Can we create two separate tables in a single mail? Can anyone provide me the logic as to how it can be achieved?

1 ACCEPTED SOLUTION

j_angelevski
Contributor III

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.
image

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", "")

image

And this is the preview of the two tables created with the sample dataset.
image

View solution in original post

4 REPLIES 4

bojanvelevski
Valued Contributor

Hey @aditya.gupta41 ,

By tables you mean HTML tables?

Yes…there are two sets of data coming through in a json format

j_angelevski
Contributor III

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.
image

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", "")

image

And this is the preview of the two tables created with the sample dataset.
image

Thanks. This worked for me 🙂