How to Create and Populate ResultMatrix Object

Here is a code snippet revealing the most common features:

def getTrafficColor(value) {
    if (value <= 0) {
        return "red"
    } else if (value > 0 && value < 0.1) {
        return "yellow"
    } else if (value >= 0.1) {
        return "green"

// initialize new matrix with three columns
def matrix = api.newMatrix("Item", "Quantity", "Price", "Pricegrid", "Pricelist", "Margin Status", "Image", "CustomerId")

// add more columns dynamically
matrix.addColumn("Margin %")

// set column formats
matrix.setColumnFormat("Item", FieldFormatType.TEXT)
matrix.setColumnFormat("Quantity", FieldFormatType.NUMERIC_LONG)
matrix.setColumnFormat("Price", FieldFormatType.MONEY_EUR)
matrix.setColumnFormat("Margin %", FieldFormatType.PERCENT)
matrix.setColumnFormat("Url", FieldFormatType.LINK)

// allow users to filter values

// add data row
        "Item": matrix.styledCell("Red bold text", "#ff0000", "transparent", "bold"),
        "Quantity": 1.23456,
        "Price": 78.9012,
        "Margin %": 0.3456,
        "Url": "<a href=\"\">link</a>",
        // optionally you can add link to a pricegrid of a given id
        "Pricegrid" : matrix.linkToPriceGrid("Open Pricegrid", 123, null),
        // or to a pricelist of a given id
        "Pricelist" : matrix.linkToPriceList("Open Pricelist", 123, null),
        // or a to custom page (result identical to the previous)
        "CustomerId" : matrix.linkCell("Open Customer", "customersPage", "123456"),
        // or add a library images, such as Traffic, BlackTraffic or Arrow
        "Margin Status" : matrix.libraryImage("BlackTraffic", getTrafficColor(-0.2f)),
        // or add a general image
        "Image" : matrix.imageCell("/images/grid/approve.png"),

return matrix

(tick) If you have performance issues with large tables, avoid saving preferences with the 'Auto fit all columns' option enabled, as mentioned in the Knowledge Base.