OpportunityController

public class OpportunityController {

    public Id oppId{get;set;}
    public Opportunity opp {get;set;}
    public List<OpportunityLineItem> olis {get;set;}

    public OpportunityController(ApexPages.StandardController controller) {
        oppId = controller.getRecord().Id;
        opp = OpportunityHelper.getOpportunity(oppId);
        olis = opp.OpportunityLineItems;
    }

    public Pagereference createInvoice(){
        
        List<SObject> upsertObjects = OpportunityHelper.mapInvoice(opp, olis);
        Invoice__c inv;
        List<Invoice_Line_Item__c> ilis = new List<Invoice_Line_Item__c>();
        for(SObject so : upsertObjects){
            if(so instanceof Invoice__c){
                inv = (Invoice__c) so;
            }else if(so instanceof Invoice_Line_Item__c){
                ilis.add((Invoice_Line_Item__c) so);
            }
        }
        insert inv;
        for(Invoice_Line_Item__c ili : ilis){
            ili.Invoice__c = inv.Id;
        }
        insert ilis;
        return new PageReference('/'+ inv.Id);
    }
    
}
public class InvoiceHelper {
  public static void mapInvoices(String query){
        List<Invoice__c> invList = new List<Invoice__c>();
        List<Invoice_Line_Item__c> iliList = new List<Invoice_Line_Item__c>();
        InvoiceQueryJSON queryJson = AuthCallout.queryInvoices(query);
        List<SObject> returnList = CustomerMap.mapInvoices(queryJson);
        for(SObject so : returnList){
            System.debug(so);
            if(so instanceOf Invoice__c){
                invList.add((Invoice__c) so);
            }else if(so instanceof Invoice_Line_Item__c){
                iliList.add((Invoice_Line_Item__c) so);
            }
        }

        Database.UpsertResult [] invoiceResults = Database.upsert(invList,Invoice__c.QBO_Id__c,false);
        for(Database.UpsertResult result : invoiceResults){
            if(!result.success){
                System.debug(result.errors);
            }
        }
        Database.UpsertResult [] iliResults = Database.upsert(iliList,Invoice_Line_Item__c.QB_Id__c,false);
        for(Database.UpsertResult result : iliResults){
            if(!result.success){
                System.debug(result.errors);
            }
        }
       


    public static void mapInvoice(Invoice__c inv){
        List<Invoice__c> invList = new List<Invoice__c>();
        List<Invoice_Line_Item__c> iliList = new List<Invoice_Line_Item__c>();

        String query = 'select * from Invoice where id = \''+ inv.QBO_Id__c + '\'';
        InvoiceQueryJSON queryJson = AuthCallout.getInvoice(inv);
        
        if(queryJson != null){
            if(queryJson.QueryResponse.Invoice.size() == 1){
                inv.QBO_Id__c = queryJson.QueryResponse.Invoice[0].Id;
                inv.QB_SyncToken__c = queryJson.QueryResponse.Invoice[0].SyncToken;
            }else{
                
                throw new ListException(); 
            }
        }
        List<SObject> returnList = customerMap.mapInvoices(queryJson);
        for(SObject so : returnList){
            System.debug(so);
            if(so instanceOf Invoice__c){
                invList.add((Invoice__c) so);
            }else if(so instanceof Invoice_Line_Item__c){
                iliList.add((Invoice_Line_Item__c) so);
            }
        }

        Database.UpsertResult [] invoiceResults = Database.upsert(invList,Invoice__c.QBO_Id__c,false);
        for(Database.UpsertResult result : invoiceResults){
            if(!result.success){
                System.debug(result.errors);
            }
        }
        Database.UpsertResult [] iliResults = Database.upsert(iliList,Invoice_Line_Item__c.QB_Id__c,false);
        for(Database.UpsertResult result : iliResults){
            if(!result.success){
                System.debug(result.errors);
            }
        }
    public static void createOrUpdateInvoices(List<Invoice__c> invs){
        List<SObject> returnList = new List<SObject>();
        List<QBO_Config__mdt> config = QIME.getConfig();
        for(Invoice__c inv : invs){
            String customerId = inv.Opportunity__r.Account.QBO_Id__c;
           
            if(checkCustomerCreate(config) && String.isBlank(inv.Opportunity__r.Account.QBO_Id__c) ){
                Account acc = QBCustomerHelper.getAccount(inv.Opportunity__r.AccountId);
                acc =QBCustomerHelper.createOrUpdateCustomer(acc);
                returnList.add(acc);
            } else if(String.isBlank(inv.Opportunity__r.Account.QBO_Id__c)){
                inv.addError('Please sync customer before syncing invoice');
                continue;
            }
  
            String invoiceJson = QBMap.mapInvoice(inv, inv.Invoice_Line_Item__r, customerId);
            try{
                InvoiceResponseJSON resJSON = QBCallout.createInvoice(invoiceJson);
                returnList.addAll(CustomerMap.mapInvoiceData(inv, inv.Invoice_Line_Item__r, resJSON));
            }catch(Exception e){
                System.debug(e);
            }
        }
        createOrUpdateInvoiceDML(returnList);
    }

    private static void createOrUpdateInvoiceDML(List<SObject> returnList){
        List<Invoice__c> invList = new List<Invoice__c>();
        List<Invoice_Line_Item__c> iliList = new List<Invoice_Line_Item__c>();
        List<Account> accList = new List<Account>();
        for(SObject so : returnList){
            System.debug(so);
            if(so instanceOf Invoice__c){
                invList.add((Invoice__c) so);
            }else if(so instanceof Invoice_Line_Item__c){
                iliList.add((Invoice_Line_Item__c) so);
            } else if(so instanceof Account){
                accList.add((Account) so);
            }
        }

        Database.UpsertResult [] accResults = Database.upsert(accList,false);
        debugResults(accResults);
        Database.UpsertResult [] invoiceResults = Database.upsert(invList,false);
        debugResults(invoiceResults);
        Database.UpsertResult [] iliResults = Database.upsert(iliList,false);
        debugResults(iliResults);
    }
    public static Invoice__c getInvoice(Id invId){
        return [SELECT Id,
                     Due_Date__c,
                      Subtotal__c,
                      Balance__c,
                       Name, 
                       QBO_Id__c,
                       Opportunity__r.Account.QBO_Id__c,
                       Opportunity__r.AccountId,
                       QB_SyncToken__c,
                      

                       (SELECT Id, Unit_Price__c, Quantity__c, 
                                Amount__c, ItemRef_Id__c, QB_Id__c,
                                Product__r.QB_Id__c, Product__r.QB_Description__c,Product__r.Name,Product__r.Description,
 
Product__c FROM Invoice_Line_Item__r)
FROM Invoice__c 
WHERE Id =: invId];
    }
}

And then map all the invoices in which you just sent the products as an invoice, or you can use LWC to sync the product.”

<apex:page standardcontroller="Invoice__c" extensions="SendInvoiceController" action="{!syncQB}">
</apex:page>

public class SendInvoiceController{
    public Id invId{get;set;}
    public Invoice__c inv {get;set;}

    public SendInvoiceController(ApexPages.StandardController controller) {
        invId = controller.getRecord().Id;
        inv = [SELECT Id,
                     
                       Name, 
                       QBO_Id__c, 
                       QBO_SyncToken__c,
                       
                       Description__c,
                       Invoice__c.Opportunity__r.Name,
                       Invoice__c.Opportunity__r.Account.contact__r.email,
                       Invoice__c.Opportunity__r.Account.QBO_Id__c,
                       Invoice__c.opportunity__r.account.BillingCity,  

                       Invoice__c.opportunity__r.account.name,
                    FROM Invoice__c 
                    WHERE Id =: invId];
    }

    public Pagereference syncQB(){
        AuthCallout callout = new AuthCallout ();
        QBInvoiceQueryJSON queryJson = callout.getInvoice(inv);
        if(queryJson != null){
            if(queryJson.QueryResponse.Invoice.size() == 1){
                inv.QBO_Id__c = queryJson.QueryResponse.Invoice[0].Id;
                inv.QBO_SyncToken__c = queryJson.QueryResponse.Invoice[0].SyncToken;
            }else{

               throw new Exception(); 
            }
        }
        InvoiceResponseJSON res = AuthCallout.createInvoiceCall(inv);
        callout.updateRefreshToken();
        inv.QBO_Id__c = res.Invoice.Id;
        inv.QBO_SyncToken__c = res.Invoice.SyncToken;
        inv.tax__c = res.Invoice.TxnTaxDetail.TotalTax;
        inv.total__c = res.Invoice.TotalAmt;
        update inv;
        return new PageReference('/'+ invId);
    }
}

Thank you for taking the time to read our blog! We appreciate your interest and hope you found valuable insights within these articles. If you have any questions, comments, or suggestions, we’d love to hear from you.