Skip to main content

Change the sort order on a table

Zuora

Change the sort order on a table

You can control the order that charges appear on invoices. To do this, define the sort order within a table on your invoice template. For example, you can group charges first by subscription first, then by charge date.

Control the Sort Order

To control the sort order of a table, use the TableSort​ field. The field has the following format:

<<TableSort:FieldName1 [Order], FieldName2 [Order], FieldName3 [Order], ...>>​

Where:

  • FieldName# is a merge field in the table.
  • Order is ASC for ascending order or DESC for descending order.

Charges are first ordered according to FieldName1. If two charges have the same value for FieldName1, they are then ordered according to FieldName2, and so on.

The TableSort field must be placed between TableStart and TableEnd fields.

The following example field code would sort by ChargeNumber (in ascending order) and then by AmountWithoutTax (in descending order):

{ MERGEFIELD "TableSort:InvoiceItem.ChargeNumber ASC, InvoiceItem.AmountWithoutTax DESC" \* MERGEFORMAT }

Note that the table sort parameter is enclosed in quotation marks. When a parameter contains a space, you must enclose the parameter in quotation marks, otherwise it will not be processed correctly.

Group and subtotal in nested tables is the only way you can sort by custom fields. For other cases, custom fields are not supported when using TableSort

Example: Sort by Charge Number and Service Start Date

The following example template sorts invoice items by their charge number (descending) and then by service start date (ascending).

Amount Without Tax Charge Number Service Start Date

<<TableStart:InvoiceItem>><<TableSort:InvoiceItem.ChargeNumber DESC, InvoiceItem.ServiceStartDate ASC>><<InvoiceItem.AmountWithoutTax>>

<<InvoiceItem.ChargeNumber>>

<<InvoiceItem.ServiceStartDate>>

<<TableEnd:InvoiceItem>>

The template produces the following table.

Amount Without Tax Charge Number Service Start Date

8.00

C-00000001

2009-01-01

1.00

C-00000002

2009-02-01

5.20

C-00000002

2009-03-01

Example: Sort by Charge Number and Amount without Tax

The following example template sorts invoice items by their charge number (descending) and then by amount without tax (descending).

Amount Without Tax Charge Number Service Start Date

<<TableStart:InvoiceItem>>
<<TableSort:InvoiceItem.ChargeNumber DESC>> <<InvoiceItem.AmountWithoutTax DESC>>

<<InvoiceItem.ChargeNumber>>

<<InvoiceItem.ServiceStartDate>>

<<TableEnd:InvoiceItem>>

The template produces the following table.

Amount Without Tax Charge Number Service Start Date

1.00

C-00000002

2012-02-01

5.20

C-00000002

2012-03-01

8.00

C-00000001

2012-01-01