Hi There,
I want to send an email notification from vro action/workflow with dynamic HTML content.
For example, I have a json object which contains an array of objects now I want to convert this json into HTML table.
the above link will demonstrate converting json into HTML table dynamically.
Can anyone please help me out to achieve this.
Thanks in advance.
Laxmi
At reference link you provided in the first post, there is almost a complete implementation of how to process the JSON object to create HTML - check the body of the function CreateTableFromJSON there. The only problem is that it uses DOM document to save the output HTML, which would not work for vRO as there is no DOM.
So instead you need to build a normal string containing the whole HTML, following the HTML table format. Check, for example, https://html.com/tables/ to see how the structure of HTML table looks like.
Hi,
Do you need help with converting your JSON object to HTML table, or with sending the HTML table in an email?
If the former, then could you provide a sample showing what your JSON object looks like?
Hi Ilian Iliev,
Thank you for quick your response, here is my task.
Step#1: I need to call rest API which will give json response like below:
[
{
"Book ID": "1",
"Book Name": "Computer Architecture",
"Category": "Computers",
"Price": "125.60"
},
{
"Book ID": "2",
"Book Name": "Asp.Net 4 Blue Book",
"Category": "Programming",
"Price": "56.00"
},
{
"Book ID": "3",
"Book Name": "Popular Science",
"Category": "Science",
"Price": "210.40"
}
]
Step#2: I need to capture the above output and parse it and finally it should send an email notification as HTML table format.
Like below:
Note: The JSON output will not be the same always, that means we have different API's calls with different headers and values but finally.
--
Regards,
Laxmi
At reference link you provided in the first post, there is almost a complete implementation of how to process the JSON object to create HTML - check the body of the function CreateTableFromJSON there. The only problem is that it uses DOM document to save the output HTML, which would not work for vRO as there is no DOM.
So instead you need to build a normal string containing the whole HTML, following the HTML table format. Check, for example, https://html.com/tables/ to see how the structure of HTML table looks like.
is this not achievable using Resource Elements in vRO?
Hmm, what do you want to store in the resource element?
I just thought that I will put complete HTML in resource element and somehow I will send the json which is returned by API. vRO should process that HTML give us final output then I can send an email notification with the same output.
As of now, I have resolved this way:
var headers = [];
var jsonAsInput =
[
{
"Book ID": "1",
"Book Name": "Computer Architecture",
"Category": "Computers",
"Price": "125.60"
},
{
"Book ID": "2",
"Book Name": "Asp.Net 4 Blue Book",
"Category": "Programming",
"Price": "56.00"
},
{
"Book ID": "3",
"Book Name": "Popular Science",
"Category": "Science",
"Price": "210.40"
}
]
var data = JSON.parse(jsonAsInput)
for (var key in data[0]) {
headers.push(key);
}
var doctype = "<!DOCTYPE html>"
var style = "<style>#reports { font-family: \"Trebuchet MS\", Arial, Helvetica, sans-serif; border-collapse: collapse; width: 100%;}#reports td, #reports th { border: 1px solid #ddd; padding: 8px;}#reports tr:nth-child(even){background-color: #f2f2f2;}#reports tr:hover {background-color: #ddd;}#reports th { padding-top: 12px; padding-bottom: 12px; text-align: left; background-color: #4CAF50; color: white;}</style>";
var body = "<html><head><title>Report Data</title>" + style + "</head><body><p><br/> Report details:<br/></p>"
var table = "<table id=\"reports\"><tr>"
for each(var header in headers) {
table = table + "<th>" + header + "</th>"
}
for each(var item in data) {
table = table + "</tr><tr>"
for (var td in item) {
table = table + "<td>" + item[td] + "</td>"
}
table = table + "</tr>"
}
var html = doctype + body + table + "</tr></table></body></html><br/><br/><br/>"
System.log("Final Html Str: " + html);
Thank you Ilian Iliev for your support.
You can put the HTML in a resource element, but you still need to write some scripting code to read the HTML from the resource element and generate the final HTML content using the dynamic data. Resource elements will not do this automatically.