Getting Data

Once you have configured your GetConnector and your authentication token you can generate a script to extract the data. AFAS doesn't limit the number of records but limits the time for running a request, e.g. you run the script for 5 minutes and after 5 minutes it will give you a time out. The time it takes to execute a script depends on the complexity of your GetConnector. A GetConnector based on 1 table will run faster than a GetConnector based on multiple tables with complex joins between the tables.


There is no unique record ID, however AFAS offers a method using SKIP and TAKE and you will need to configure these settings to reach an optimal for collecting a number of records in a batch without getting a time out. So the first batch you will say SKIP = 0, TAKE = e.g 100, in the next batch you then say SKIP = 100, take = 100.


Below you will find an example script for collecting batches of 10.000 transactions


After collecting each batch the system will count the number of records if this is equal to 10.000 then it will run another batch. But the SKIP will be increased in each pass with 10.000

 


$token = 'AfasToken XXXXXXXXXXXXXXXXXXXX'


$body =@{

    skip=0

    take=10000

}

 

$headers = @{

    Authorization = $token

    


# The First 100

$destinationFile = 'C:\temp\Afas_test.csv'

$url = "https://XXXXXX.resttest.afas.online/profitrestservices/connectors/QV_FIN_FinMut"


try {

   $response = Invoke-RestMethod -Uri $url -method GET -Headers $headers -Body $body

   #$response = Invoke-WebRequest -Uri $url -method GET -Headers $headers 

  #$response.Content | ConvertFrom-Json |ConvertTo-Csv -NoTypeInformation | Set-Content $destinationFile

   $response.rows | Select-Object *  | Export-Csv  $destinationFile -Delimiter ';' -NoTypeInformation


    } 

catch {

   # Dig into the exception to get the Response details.

   # Note that value__ is not a typo.

   Write-Host "StatusCode:" $_.Exception.Response.StatusCode.value__

   Write-Host "StatusDescription:" $_.Exception.Response.StatusDescription

}


#Get the last transaction

$Transaction_ID = Import-CSV $destinationFile -Delimiter ';' | select -ExpandProperty Journaalpost


#Count the transactions

$count = 0

foreach($line in $Transaction_ID)

{

    if($line -ne ""){$count++}

}

write-host $count


# Loop

$rows = 0


while ($count -eq 10000)

{

    $rows=$rows + 10000

    $destinationFile = 'C:\temp\Afas_test_'+ $rows+ '.csv'


     $body =@{

            skip=$rows

            take=10000

    }

     

    $headers = @{

        Authorization = $token

        

    } 

    

    $url = "https://XXXXXX.resttest.afas.online/profitrestservices/connectors/QV_FIN_FinMut"

    

    try {

            $response = Invoke-RestMethod -Uri $url -method GET -Headers $headers -Body $body

            #$response = Invoke-WebRequest -Uri $url -method GET -Headers $headers 

            #$response.Content | ConvertFrom-Json |ConvertTo-Csv -NoTypeInformation | Set-Content $destinationFile

            $response.rows | Select-Object *  | Export-Csv  $destinationFile -Delimiter ';' -NoTypeInformation

        } 

    catch {

       # Dig into the exception to get the Response details.

       # Note that value__ is not a typo.

       Write-Host "StatusCode:" $_.Exception.Response.StatusCode.value__

       Write-Host "StatusDescription:" $_.Exception.Response.StatusDescription

    }


    #Get the last transaction

    $Transaction_ID = Import-CSV $destinationFile -Delimiter ';' | select -ExpandProperty Journaalpost


    #Count the transactions


    $count = 0

    foreach($line in $Transaction_ID)

    {

        if($line -ne ""){$count++}

    }

    write-host $count

    

    if ($count -ne 10000) {break}

}