Skip to main content

Query an Invoice Body Field

Zuora

Query an Invoice Body Field

You can use a specially-encoded PDF file for an invoice (through the Invoice object's Body field). Once you add the invoice in the system, you can query on that field. This use case provides information about this procedure.

Querying an Invoice Object's Body Field

The body of an invoice is a PDF file encoded using Base64 encoding. Once the body is queried, you need to unencode it using a standard Base64 utility.

To query an Invoice object's Body field,

  1. Query Body from the Invoice object, using ZOQL. See the example query, below.
  2. Decode the Body field of the Invoice as a Base64 value into a byte array.
  3. Write the resulting byte array to disk.

Example Query

The following is an example of querying Body from the Invoice object:

SELECT Id, AccountId, Amount, Balance, DueDate, InvoiceDate, InvoiceNumber, Status, TargetDate, Body FROM Invoice WHERE id='someInvoiceId'

The query will work only if a single row is returned. If more than one row matches the WHERE clause, the system will generate an error.

Example of Querying an Invoice Body Field

Invoice in = query("select Id, AccountId, Amount, Balance, DueDate, InvoiceDate, InvoiceNumber, Status, TargetDate, Body from Invoice where id='someInvoiceId'");
String str = in.getBody();
byte[] b = Base64.decodeBase64(str.getBytes());
File file = new File(in.getInvoiceNumber()+".pdf");
if (!file.exists()) file.createNewFile();
FileOutputStream out = new FileOutputStream(file);
out.write(b);