Querying an Invoice Body Field
From API Documentation
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 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:
SELECT Id, AccountId, Amount, Balance, DueDate, InvoiceDate, InvoiceNumber, Status, TargetDate, Body FROM Invoice WHERE id='someInvoiceId'
Note: 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.
2. Unencode the Body field of the Invoice as a Base64 value into a byte array.
3. Write the resulting byte array to disk.
Example
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);
