“Here, we have created a custom object ‘Invoice’ for the simple mapping of opportunity product lists for product creation purposes to send invoices for related customers.
The approach we follow is to first query the customer and add them to the invoice. Then, we send a POST request for the invoice related to that particular customer.
Here, we have a sample JSON file for the official documentation, which they provided. You can also visit and refer to this link to check them out:
{
"Line": [
{
"DetailType": "SalesItemLineDetail",
"Amount": 100.0,
"SalesItemLineDetail": {
"ItemRef": {
"name": "Services",
"value": "1"
}
}
}
],
"CustomerRef": {
"value": "1"
}
}
Create a wrapper class for the request and response handling of the invoice object. You can refer to this website for the JSON data to convert the wrapper.
public class InvoiceJSON {
public class Line {
public Double Amount;
public String DetailType;
public Double Qty;
public String Description;
public SalesItemLineDetail SalesItemLineDetail;
}
public class CustomField {
public String DefinitionId;
public String StringValue;
public String Type;
public String Name;
}
public class BillAddr {
public String Id;
public String Line1;
public String Line2;
public String Line3;
public String Line4;
public String Line5;
public String City;
public String CountrySubDivisionCode;
public String PostalCode;
public String Lat;
public String Long_x;
public String Country;
}
public class SalesTermRef {
public String value;
}
public List<Line> Line;
public List<CustomField> CustomField;
public CustomerRef CustomerRef;
public Date DueDate;
public Double Balance;
public String Id;
public String SyncToken;
public SalesTermRef SalesTermRef;
public BillAddr BillAddr;
public BillAddr ShipAddr;
public EmailAddress BillEmail;
public MemoRef CustomerMemo;
public String EmailStatus;
public String DocNumber;
public Boolean AutoDocNumber;
public Boolean AllowOnlineACHPayment;
public Boolean AllowOnlineCreditCardPayment;
public TxnTaxDetail TxnTaxDetail;
public class EmailAddress{
public string Address;
}
public class TxnTaxDetail{
public TxnTaxCodeRef TxnTaxCodeRef;
public Decimal TotalTax;
}
public class TxnTaxCodeRef{
public String value;
}
public class MemoRef{
public string value;
}
public class SalesItemLineDetail {
public ItemRef ItemRef;
public Decimal Qty;
public Decimal UnitPrice;
public ItemRef TaxCodeRef;
}
public class ItemRef {
public String value;
public String name;
}
public class CustomerRef {
public String value;
}
public static InvoiceJSON parse(String json) {
return (InvoiceJSON ) System.JSON.deserialize(json, InvoiceJSON .class);
}
}
public class InvoiceQueryJSON {
public class CurrencyRef {
public String value {get;set;}
public String name {get;set;}
public CurrencyRef(JSONParser parser) {
while (parser.nextToken() != System.JSONToken.END_OBJECT) {
if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME) {
String text = parser.getText();
if (parser.nextToken() != System.JSONToken.VALUE_NULL) {
if (text == 'value') {
value = parser.getText();
} else if (text == 'name') {
name = parser.getText();
} else {
System.debug(LoggingLevel.WARN, 'CurrencyRef consuming unrecognized property: '+text);
consumeObject(parser);
}
}
}
}
}
}
public class Invoice {
public Integer Deposit {get;set;}
public Boolean AllowIPNPayment {get;set;}
public Boolean AllowOnlinePayment {get;set;}
public Boolean AllowOnlineCreditCardPayment {get;set;}
public Boolean AllowOnlineACHPayment {get;set;}
public String domain {get;set;}
public Boolean sparse {get;set;}
public String Id {get;set;}
public String SyncToken {get;set;}
public MetaData MetaData {get;set;}
public List<CustomField> CustomField {get;set;}
public String DocNumber {get;set;}
public String TxnDate {get;set;}
public CurrencyRef CurrencyRef {get;set;}
public List<LinkedTxn> LinkedTxn {get;set;}
public List<Line> Line {get;set;}
public CurrencyRef CustomerRef {get;set;}
public TaxCodeRef SalesTermRef {get;set;}
public String DueDate {get;set;}
public Double TotalAmt {get;set;}
public Boolean ApplyTaxAfterDiscount {get;set;}
public String PrintStatus {get;set;}
public String EmailStatus {get;set;}
public Double Balance {get;set;}
public String PrivateNote {get;set;}
public TaxCodeRef CustomerMemo {get;set;}
public BillEmail BillEmail {get;set;}
public BillEmail BillEmailCc {get;set;}
public BillEmail BillEmailBcc {get;set;}
public TxnTaxDetail TxnTaxDetail {get;set;}
public DeliveryInfo DeliveryInfo {get;set;}
public Invoice(JSONParser parser) {
while (parser.nextToken() != System.JSONToken.END_OBJECT) {
if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME) {
String text = parser.getText();
if (parser.nextToken() != System.JSONToken.VALUE_NULL) {
if (text == 'Deposit') {
Deposit = parser.getIntegerValue();
} else if (text == 'AllowIPNPayment') {
AllowIPNPayment = parser.getBooleanValue();
} else if (text == 'AllowOnlinePayment') {
AllowOnlinePayment = parser.getBooleanValue();
} else if (text == 'AllowOnlineCreditCardPayment') {
AllowOnlineCreditCardPayment = parser.getBooleanValue();
} else if (text == 'AllowOnlineACHPayment') {
AllowOnlineACHPayment = parser.getBooleanValue();
} else if (text == 'domain') {
domain = parser.getText();
} else if (text == 'sparse') {
sparse = parser.getBooleanValue();
} else if (text == 'Id') {
Id = parser.getText();
} else if (text == 'SyncToken') {
SyncToken = parser.getText();
} else if (text == 'MetaData') {
MetaData = new MetaData(parser);
} else if (text == 'CustomField') {
CustomField = arrayOfCustomField(parser);
} else if (text == 'DocNumber') {
DocNumber = parser.getText();
} else if (text == 'TxnDate') {
TxnDate = parser.getText();
} else if (text == 'PrivateNote') {
PrivateNote = parser.getText();
} else if (text == 'CurrencyRef') {
CurrencyRef = new CurrencyRef(parser);
} else if (text == 'DeliveryInfo') {
DeliveryInfo = new DeliveryInfo(parser);
} else if (text == 'LinkedTxn') {
LinkedTxn = arrayOfLinkedTxn(parser);
} else if (text == 'Line') {
Line = arrayOfLine(parser);
} else if (text == 'CustomerRef') {
CustomerRef = new CurrencyRef(parser);
} else if (text == 'SalesTermRef') {
SalesTermRef = new TaxCodeRef(parser);
} else if (text == 'BillEmail') {
BillEmail = new BillEmail(parser);
} else if (text == 'BillEmailBcc') {
BillEmailBcc = new BillEmail(parser);
} else if (text == 'BillEmailCc') {
BillEmailCc = new BillEmail(parser);
} else if (text == 'TxnTaxDetail') {
TxnTaxDetail = new TxnTaxDetail(parser);
} else if (text == 'DueDate') {
DueDate = parser.getText();
} else if (text == 'TotalAmt') {
TotalAmt = parser.getDoubleValue();
} else if (text == 'ApplyTaxAfterDiscount') {
ApplyTaxAfterDiscount = parser.getBooleanValue();
} else if (text == 'PrintStatus') {
PrintStatus = parser.getText();
} else if (text == 'EmailStatus') {
EmailStatus = parser.getText();
} else if (text == 'Balance') {
Balance = parser.getDoubleValue();
} else if (text == 'CustomerMemo') {
CustomerMemo = new TaxCodeRef(parser);
} else {
System.debug(LoggingLevel.WARN, 'Invoice consuming unrecognized property: '+text);
consumeObject(parser);
}
}
}
}
}
}
public class Line_Z {
public String Id {get;set;}
public Integer LineNum {get;set;}
public Double Amount {get;set;}
public String DetailType {get;set;}
public SalesItemLineDetail SalesItemLineDetail {get;set;}
public LinkedTxn SubTotalLineDetail {get;set;}
public Line_Z(JSONParser parser) {
while (parser.nextToken() != System.JSONToken.END_OBJECT) {
if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME) {
String text = parser.getText();
if (parser.nextToken() != System.JSONToken.VALUE_NULL) {
if (text == 'Id') {
Id = parser.getText();
} else if (text == 'LineNum') {
LineNum = parser.getIntegerValue();
} else if (text == 'Amount') {
Amount = parser.getDoubleValue();
} else if (text == 'DetailType') {
DetailType = parser.getText();
} else if (text == 'SalesItemLineDetail') {
SalesItemLineDetail = new SalesItemLineDetail(parser);
} else if (text == 'SubTotalLineDetail') {
SubTotalLineDetail = new LinkedTxn(parser);
} else {
System.debug(LoggingLevel.WARN, 'Line_Z consuming unrecognized property: '+text);
consumeObject(parser);
}
}
}
}
}
}
public class DeliveryInfo {
public String DeliveryType {get;set;}
public String DeliveryTime {get;set;}
public DeliveryInfo(JSONParser parser) {
while (parser.nextToken() != System.JSONToken.END_OBJECT) {
if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME) {
String text = parser.getText();
if (parser.nextToken() != System.JSONToken.VALUE_NULL) {
if (text == 'DeliveryType') {
DeliveryType = parser.getText();
} else if (text == 'DeliveryTime') {
DeliveryTime = parser.getText();
} else {
System.debug(LoggingLevel.WARN, 'InvoiceQueryJSON consuming unrecognized property: '+text);
consumeObject(parser);
}
}
}
}
}
}
public class Line_Y {
public String Id {get;set;}
public Integer LineNum {get;set;}
public Double Amount {get;set;}
public String DetailType {get;set;}
public SalesItemLineDetail_Z SalesItemLineDetail {get;set;}
public LinkedTxn SubTotalLineDetail {get;set;}
public Line_Y(JSONParser parser) {
while (parser.nextToken() != System.JSONToken.END_OBJECT) {
if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME) {
String text = parser.getText();
if (parser.nextToken() != System.JSONToken.VALUE_NULL) {
if (text == 'Id') {
Id = parser.getText();
} else if (text == 'LineNum') {
LineNum = parser.getIntegerValue();
} else if (text == 'Amount') {
Amount = parser.getDoubleValue();
} else if (text == 'DetailType') {
DetailType = parser.getText();
} else if (text == 'SalesItemLineDetail') {
SalesItemLineDetail = new SalesItemLineDetail_Z(parser);
} else if (text == 'SubTotalLineDetail') {
SubTotalLineDetail = new LinkedTxn(parser);
} else {
System.debug(LoggingLevel.WARN, 'Line_Y consuming unrecognized property: '+text);
consumeObject(parser);
}
}
}
}
}
}
public class SalesItemLineDetail {
public CurrencyRef ItemRef {get;set;}
public CurrencyRef ItemAccountRef {get;set;}
public TaxCodeRef TaxCodeRef {get;set;}
public Decimal UnitPrice {get;set;}
public Decimal Qty {get;set;}
public String ServiceDate {get;set;}
public SalesItemLineDetail(JSONParser parser) {
while (parser.nextToken() != System.JSONToken.END_OBJECT) {
if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME) {
String text = parser.getText();
if (parser.nextToken() != System.JSONToken.VALUE_NULL) {
if (text == 'ItemRef') {
ItemRef = new CurrencyRef(parser);
} else if (text == 'ItemAccountRef') {
ItemAccountRef = new CurrencyRef(parser);
} else if (text == 'UnitPrice') {
UnitPrice = parser.getDecimalValue();
} else if (text == 'Qty') {
Qty = parser.getDecimalValue();
} else if (text == 'ServiceDate') {
ServiceDate = parser.getText();
} else if (text == 'TaxCodeRef') {
TaxCodeRef = new TaxCodeRef(parser);
} else {
System.debug(LoggingLevel.WARN, 'SalesItemLineDetail consuming unrecognized property: '+text);
consumeObject(parser);
}
}
}
}
}
}
public class TxnTaxDetail {
public Double TotalTax {get;set;}
public TxnTaxDetail(JSONParser parser) {
while (parser.nextToken() != System.JSONToken.END_OBJECT) {
if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME) {
String text = parser.getText();
if (parser.nextToken() != System.JSONToken.VALUE_NULL) {
if (text == 'TotalTax') {
TotalTax = parser.getDoubleValue();
} else {
System.debug(LoggingLevel.WARN, 'InvoiceQueryJSON consuming unrecognized property: '+text);
consumeObject(parser);
}
}
}
}
}
}
public class Line {
public String Id {get;set;}
public Integer LineNum {get;set;}
public Double Amount {get;set;}
public String DetailType {get;set;}
public SalesItemLineDetail SalesItemLineDetail {get;set;}
public GroupLineDetail GroupLineDetail {get;set;}
public LinkedTxn SubTotalLineDetail {get;set;}
public String Description {get;set;}
// 17549
public Line(JSONParser parser) {
while (parser.nextToken() != System.JSONToken.END_OBJECT) {
if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME) {
String text = parser.getText();
if (parser.nextToken() != System.JSONToken.VALUE_NULL) {
if (text == 'Id') {
Id = parser.getText();
} else if (text == 'LineNum') {
LineNum = parser.getIntegerValue();
} else if (text == 'Amount') {
Amount = parser.getDoubleValue();
} else if (text == 'DetailType') {
DetailType = parser.getText();
} else if (text == 'Description') {
Description = parser.getText();
} else if (text == 'SalesItemLineDetail') {
SalesItemLineDetail = new SalesItemLineDetail(parser);
} else if (text == 'SubTotalLineDetail') {
SubTotalLineDetail = new LinkedTxn(parser);
} else if (text == 'GroupLineDetail') {
GroupLineDetail = new GroupLineDetail(parser);
} else {
System.debug(LoggingLevel.WARN, 'Line consuming unrecognized property: '+text);
consumeObject(parser);
}
}
}
}
}
}
public class GroupLineDetail {
public GroupItemRef GroupItemRef {get;set;}
public Decimal Quantity {get;set;}
public Decimal Amount {get;set;}
public GroupLineDetail(JSONParser parser){
while (parser.nextToken() != System.JSONToken.END_OBJECT) {
if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME) {
String text = parser.getText();
if (parser.nextToken() != System.JSONToken.VALUE_NULL) {
if (text == 'GroupItemRef') {
GroupItemRef = new GroupItemRef(parser);
} else if(text == 'Quantity'){
Quantity = parser.getDecimalValue();
} else if(text == 'Amount'){
Amount = parser.getDecimalValue();
} else {
System.debug(LoggingLevel.WARN, 'SalesItemLineDetail_Z consuming unrecognized property: '+text);
consumeObject(parser);
}
}
}
}
}
}
public class SalesItemLineDetail_Z {
public CurrencyRef ItemRef {get;set;}
public Integer UnitPrice {get;set;}
public Integer Qty {get;set;}
public CurrencyRef ItemAccountRef {get;set;}
public TaxCodeRef TaxCodeRef {get;set;}
public SalesItemLineDetail_Z(JSONParser parser) {
while (parser.nextToken() != System.JSONToken.END_OBJECT) {
if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME) {
String text = parser.getText();
if (parser.nextToken() != System.JSONToken.VALUE_NULL) {
if (text == 'ItemRef') {
ItemRef = new CurrencyRef(parser);
} else if (text == 'UnitPrice') {
UnitPrice = parser.getIntegerValue();
} else if (text == 'Qty') {
Qty = parser.getIntegerValue();
} else if (text == 'ItemAccountRef') {
ItemAccountRef = new CurrencyRef(parser);
} else if (text == 'TaxCodeRef') {
TaxCodeRef = new TaxCodeRef(parser);
} else {
System.debug(LoggingLevel.WARN, 'SalesItemLineDetail_Z consuming unrecognized property: '+text);
consumeObject(parser);
}
}
}
}
}
}
public class GroupItemRef {
public String name {get;set;}
public String value {get;set;}
public GroupItemRef(JSONParser parser){
while (parser.nextToken() != System.JSONToken.END_OBJECT) {
if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME) {
String text = parser.getText();
if (parser.nextToken() != System.JSONToken.VALUE_NULL) {
if (text == 'value') {
value = parser.getText();
} else if (text == 'name') {
name = parser.getText();
} else {
System.debug(LoggingLevel.WARN, 'TaxCodeRef consuming unrecognized property: '+text);
consumeObject(parser);
}
}
}
}
}
}
public class TaxCodeRef {
public String value {get;set;}
public String name {get;set;}
public TaxCodeRef(JSONParser parser) {
while (parser.nextToken() != System.JSONToken.END_OBJECT) {
if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME) {
String text = parser.getText();
if (parser.nextToken() != System.JSONToken.VALUE_NULL) {
if (text == 'value') {
value = parser.getText();
} else if (text == 'name') {
name = parser.getText();
} else {
System.debug(LoggingLevel.WARN, 'TaxCodeRef consuming unrecognized property: '+text);
consumeObject(parser);
}
}
}
}
}
}
public QueryResponse QueryResponse {get;set;}
public InvoiceQueryJSON (JSONParser parser) {
while (parser.nextToken() != System.JSONToken.END_OBJECT) {
if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME) {
String text = parser.getText();
if (parser.nextToken() != System.JSONToken.VALUE_NULL) {
if (text == 'QueryResponse') {
QueryResponse = new QueryResponse(parser);
} else {
System.debug(LoggingLevel.WARN, 'QBInvoiceQueryJSON consuming unrecognized property: '+text);
consumeObject(parser);
}
}
}
}
}
public class MetaData {
public String CreateTime {get;set;}
public String LastUpdatedTime {get;set;}
public MetaData(JSONParser parser) {
while (parser.nextToken() != System.JSONToken.END_OBJECT) {
if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME) {
String text = parser.getText();
if (parser.nextToken() != System.JSONToken.VALUE_NULL) {
if (text == 'CreateTime') {
CreateTime = parser.getText();
} else if (text == 'LastUpdatedTime') {
LastUpdatedTime = parser.getText();
} else {
System.debug(LoggingLevel.WARN, 'MetaData consuming unrecognized property: '+text);
consumeObject(parser);
}
}
}
}
}
}
public class QueryResponse {
public List<Invoice> Invoice {get;set;}
public Integer startPosition {get;set;}
public Integer maxResults {get;set;}
public Integer totalCount {get;set;}
public QueryResponse(JSONParser parser) {
while (parser.nextToken() != System.JSONToken.END_OBJECT) {
if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME) {
String text = parser.getText();
if (parser.nextToken() != System.JSONToken.VALUE_NULL) {
if (text == 'Invoice') {
Invoice = arrayOfInvoice(parser);
} else if (text == 'startPosition') {
startPosition = parser.getIntegerValue();
} else if (text == 'maxResults') {
maxResults = parser.getIntegerValue();
} else if (text == 'totalCount') {
totalCount = parser.getIntegerValue();
} else {
System.debug(LoggingLevel.WARN, 'QueryResponse consuming unrecognized property: '+text);
consumeObject(parser);
}
}
}
}
}
}
public class CustomField {
public String DefinitionId {get;set;}
public String Type_Z {get;set;} // in json: Type
public CustomField(JSONParser parser) {
while (parser.nextToken() != System.JSONToken.END_OBJECT) {
if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME) {
String text = parser.getText();
if (parser.nextToken() != System.JSONToken.VALUE_NULL) {
if (text == 'DefinitionId') {
DefinitionId = parser.getText();
} else if (text == 'Type') {
Type_Z = parser.getText();
} else {
System.debug(LoggingLevel.WARN, 'CustomField consuming unrecognized property: '+text);
consumeObject(parser);
}
}
}
}
}
}
public class LinkedTxn {
public String TxnId;
public String TxnType;
public LinkedTxn(JSONParser parser) {
while (parser.nextToken() != System.JSONToken.END_OBJECT) {
if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME) {
String text = parser.getText();
if (parser.nextToken() != System.JSONToken.VALUE_NULL) {
{
System.debug(LoggingLevel.WARN, 'LinkedTxn consuming unrecognized property: '+text);
consumeObject(parser);
if (text == 'TxnId') {
TxnId = parser.getText();
} else if (text == 'TxnType') {
TxnType = parser.getText();
} else {
System.debug(LoggingLevel.WARN, 'LinkedTxn consuming unrecognized property: '+text);
consumeObject(parser);
}
}
}
}
}
}
}
public class BillEmail {
public String Address;
public BillEmail(JSONParser parser) {
while (parser.nextToken() != System.JSONToken.END_OBJECT) {
if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME) {
String text = parser.getText();
if (parser.nextToken() != System.JSONToken.VALUE_NULL) {
{
System.debug(LoggingLevel.WARN, 'LinkedTxn consuming unrecognized property: '+text);
consumeObject(parser);
if (text == 'Address') {
Address = parser.getText();
} else {
System.debug(LoggingLevel.WARN, 'LinkedTxn consuming unrecognized property: '+text);
consumeObject(parser);
}
}
}
}
}
}
}
public static InvoiceQueryJSON parse(String json) {
System.JSONParser parser = System.JSON.createParser(json);
return new QBInvoiceQueryJSON(parser);
}
public static void consumeObject(System.JSONParser parser) {
Integer depth = 0;
do {
System.JSONToken curr = parser.getCurrentToken();
if (curr == System.JSONToken.START_OBJECT ||
curr == System.JSONToken.START_ARRAY) {
depth++;
} else if (curr == System.JSONToken.END_OBJECT ||
curr == System.JSONToken.END_ARRAY) {
depth--;
}
} while (depth > 0 && parser.nextToken() != null);
}
private static List<CustomField> arrayOfCustomField(System.JSONParser p) {
List<CustomField> res = new List<CustomField>();
if (p.getCurrentToken() == null) p.nextToken();
while (p.nextToken() != System.JSONToken.END_ARRAY) {
res.add(new CustomField(p));
}
return res;
}
private static List<Line> arrayOfLine(System.JSONParser p) {
List<Line> res = new List<Line>();
if (p.getCurrentToken() == null) p.nextToken();
while (p.nextToken() != System.JSONToken.END_ARRAY) {
res.add(new Line(p));
}
return res;
}
private static List<LinkedTxn> arrayOfLinkedTxn(System.JSONParser p) {
List<LinkedTxn> res = new List<LinkedTxn>();
if (p.getCurrentToken() == null) p.nextToken();
while (p.nextToken() != System.JSONToken.END_ARRAY) {
res.add(new LinkedTxn(p));
}
return res;
}
private static List<Line_Z> arrayOfLine_Z(System.JSONParser p) {
List<Line_Z> res = new List<Line_Z>();
if (p.getCurrentToken() == null) p.nextToken();
while (p.nextToken() != System.JSONToken.END_ARRAY) {
res.add(new Line_Z(p));
}
return res;
}
private static List<Invoice> arrayOfInvoice(System.JSONParser p) {
List<Invoice> res = new List<Invoice>();
if (p.getCurrentToken() == null) p.nextToken();
while (p.nextToken() != System.JSONToken.END_ARRAY) {
res.add(new Invoice(p));
}
return res;
}
private static List<Line_Y> arrayOfLine_Y(System.JSONParser p) {
List<Line_Y> res = new List<Line_Y>();
if (p.getCurrentToken() == null) p.nextToken();
while (p.nextToken() != System.JSONToken.END_ARRAY) {
res.add(new Line_Y(p));
}
return res;
}
}