0 votes
760 views
in Javascript API by (30.6k points)

1 Answer

0 votes
by (30.6k points)

You can, but you do not have to: for a BO list to CSV a few lines of JavaScript in a script task are simpler than an XSLT. Both versions:

JavaScript (recommended):

// service flow script: tw.local.orders (list of Order) -> tw.local.csv (String)
var cols = ["number", "customer", "total", "status"];
var q = function (v) { v = (v === null || v === undefined) ? "" : String(v); return '"' + v.replace(/"/g, '""') + '"'; };
var lines = [cols.map(q).join(",")];
for (var i = 0; i < tw.local.orders.listLength; i++) {
  var o = tw.local.orders[i];
  lines.push(cols.map(function (c) { return q(o[c]); }).join(","));
}
tw.local.csv = "\ufeff" + lines.join("\r\n");        // BOM so Excel reads UTF-8

XSLT - BPM's business objects serialise to XML (tw.local.orders.toXML() returns the XML document, toXMLString() the text), and an XSL transformation (the System Data toolkit's XML transformation service, or javax.xml.transform from a script) applies a stylesheet stored as a managed asset:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <xsl:template match="/">number,customer,total,status
<xsl:for-each select="//Order">"<xsl:value-of select="number"/>","<xsl:value-of select="customer"/>",<xsl:value-of select="total"/>,"<xsl:value-of select="status"/>"
</xsl:for-each></xsl:template>
</xsl:stylesheet>
// script: xml = tw.local.orders.toXML();  then the transform step with the stylesheet -> tw.local.csv
var xml = tw.local.orders.toXML();                       // XMLDocument / E4X on 8.5.x; String on BAW 20+ (toXMLString)

Deliver the CSV: tw.system.createDocument("orders.csv", "text/csv", tw.local.csv, false) to attach it to the instance, or return it from a service called by a coach and download it client-side (new Blob([csv], {type: "text/csv"}) + a link, as the UI Toolkit table's export does). Use XSLT when the same stylesheet is shared with other systems or the transformation is complex; otherwise JavaScript.

References

Related questions

0 votes
1 answer 2.0k views
0 votes
1 answer 3.6k views
0 votes
1 answer 849 views
0 votes
1 answer 1.7k views
+1 vote
1 answer 1.8k views
0 votes
1 answer 1.5k views
0 votes
1 answer 2.3k views
0 votes
1 answer 2.6k views
0 votes
1 answer 1.6k views
+1 vote
1 answer 1.2k views
0 votes
1 answer 1.3k views

723 questions

807 answers

98 comments

4.9k users

Join BPM Community Discord Channel

Welcome to BPM Tips Q&A, Community wiki/forum where you can ask questions and receive answers from other IBM BPM experts and members of the community. Users with 2000 points will automatically be promoted to expert level.
Created by Dosvak LLC
Our Youtube Channel
...