Sample code for 30+ languages & platforms
DataFlex

Xero Build Hashtable of Account Codes

To create a new account, a unique and unused account Code would need to be selected. How do we know what account Codes already exist? One way of finding out is to download the complete set of Accounts every time. Another way is to download once, and keep a local hash table of the account Codes. This makes it possible to find a new and unused Code without having to continually fetch data from the Xero server.

Note: Requires Chilkat v9.5.0.64 or greater.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoRest
    Variant vSbXml
    Handle hoSbXml
    Handle hoHtAccounts
    Boolean iBAutoTrim
    Handle hoXml
    Integer iNumAccounts
    Integer i
    String sHashKey
    String sNotUsed
    Variant vHtXmlSb
    Handle hoHtXmlSb
    String sTemp1
    Integer iTemp1

    Move False To iSuccess

    // Note: Requires Chilkat v9.5.0.64 or greater.

    // This requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    Get Create (RefClass(cComChilkatRest)) To hoRest
    If (Not(IsComObjectCreated(hoRest))) Begin
        Send CreateComObject of hoRest
    End

    // Before sending REST API calls, the REST object needs to be
    // initialized for OAuth1.
    // See Xero 2-Legged OAuth1 Setup for sample code.

    // Assuming the REST object's OAuth1 authenticator is setup, and the initial
    // connection was made, we may now send REST HTTP requests..

    // -----------------------------------------------------
    // Get the full list of accounts.
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbXml
    If (Not(IsComObjectCreated(hoSbXml))) Begin
        Send CreateComObject of hoSbXml
    End
    Get pvComObject of hoSbXml to vSbXml
    Get ComFullRequestNoBodySb Of hoRest "GET" "/api.xro/2.0/Accounts" vSbXml To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoRest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // A 200 response is expected for actual success.
    Get ComResponseStatusCode Of hoRest To iTemp1
    If (iTemp1 <> 200) Begin
        Get ComGetAsString Of hoSbXml To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Iterate over the accounts, and build a Hashtable where the
    // key is the account Code, and the value is the XML record for the account.
    Get Create (RefClass(cComChilkatHashtable)) To hoHtAccounts
    If (Not(IsComObjectCreated(hoHtAccounts))) Begin
        Send CreateComObject of hoHtAccounts
    End

    Move False To iBAutoTrim
    Get Create (RefClass(cComChilkatXml)) To hoXml
    If (Not(IsComObjectCreated(hoXml))) Begin
        Send CreateComObject of hoXml
    End
    Get pvComObject of hoSbXml to vSbXml
    Get ComLoadSb Of hoXml vSbXml iBAutoTrim To iSuccess
    Set ComEmitXmlDecl Of hoXml To False
    Set ComEmitCompact Of hoXml To True

    // How many accounts exist?
    Get ComNumChildrenAt Of hoXml "Accounts" To iNumAccounts
    Showln "numAccounts = " iNumAccounts

    Move 0 To i
    While (i < iNumAccounts)
        Set ComI Of hoXml To i
        Get ComGetChildContent Of hoXml "Accounts|Account[i]|Code" To sHashKey
        // Update the xml object's internal reference to the i'th account record.
        Get ComChilkatPath Of hoXml "Accounts|Account[i]|$" To sNotUsed

        Showln "hash key = " sHashKey
        If (i < 5) Begin
            // Show a few of the values..
            Get ComGetXml Of hoXml To sTemp1
            Showln sTemp1
        End

        Get ComGetXml Of hoXml To sTemp1
        Get ComAddStr Of hoHtAccounts sHashKey sTemp1 To iSuccess

        // Update the xml object's internal reference to the XML root node.
        Send ComGetRoot2 To hoXml
        Move (i + 1) To i
    Loop

    // At this point the Hashtable is built.
    // Save the Hashtable to a file.  This will be used in subsequent requests
    // when we need to find a unique/unused Code, or if we want to get
    // the account information from the local hash table.
    Get Create (RefClass(cComChilkatStringBuilder)) To hoHtXmlSb
    If (Not(IsComObjectCreated(hoHtXmlSb))) Begin
        Send CreateComObject of hoHtXmlSb
    End
    Get pvComObject of hoHtXmlSb to vHtXmlSb
    Get ComToXmlSb Of hoHtAccounts vHtXmlSb To iSuccess
    Get ComWriteFile Of hoHtXmlSb "qa_cache/xero_accounts_by_code.xml" "utf-8" False To iSuccess

    Showln "success = " iSuccess


End_Procedure