You are here: API Foundations > Use Cases > Querying an Invoice Body Field

Querying an Invoice Body Field

One of the options you have for an invoice is to use a specially encoded PDF file (through the Invoice object's Body field). Once you have that invoice in the system, you may wish to query on that field. This use case provides information on doing so.

 Note  

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 Invoice using ZOQL. Here is an example query:
  2. SELECT Id, AccountId, Amount, Balance, DueDate, InvoiceDate, InvoiceNumber, Status, TargetDate, Body FROM Invoice WHERE id='someInvoiceId'

     Note  

    The query will only work if there is a single row returned. If more than one row matches the WHERE clause, the system will throw a fault.

  3. Unencode the Body field of the Invoice as a Base64 value into a byte array.
  4. Write the resulting byte array to disk.

Sample Code

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);
out.close();

Copyright © 2008-2009 Zuora, Inc.