Using PowerShell Class to generate JSON for Batch Call API with Business Central
Tags: api, rest, businesscentral, powershell
Published: 2022-03-08 16:16:11


In this first article, I will showcase how to use PowerShell Class to easily create JSON document for Business Central API.

This sample was designed to do massive insert of Sales Invoice by lot of 100 using Batch Call : see this excellent [article](https://www.kauffmann.nl/2020/12/18/batch-calls-with-business-central-apis-1-basic-operation/) for more details about API Batch Call.

The "trick" is to use **PowerShell Class** and (nested) Arrays to generate the right JSON document.

We will start by designing Sales Invoice and Sales Invoice Line in a very simple way (using [] for Array definition to store multiple lines).

Theses classes are intended to be used with [Sales Invoice API](https://docs.microsoft.com/es-es/dynamics365/business-central/dev-itpro/api-reference/v2.0/resources/dynamics_salesinvoice) and [Sales Invoice Line API](https://docs.microsoft.com/es-es/dynamics365/business-central/dev-itpro/api-reference/v2.0/resources/dynamics_salesinvoiceline).



```
class SalesInvoiceClasss
{
    [string] $customerId 
    [SalesInvoiceLineClass[]] $salesInvoiceLines
    
    [void]addLine([SalesInvoiceLineClass]$SalesInvoiceLine) 
    {
        $this.salesInvoiceLines += $SalesInvoiceLine
    }
}

class SalesInvoiceLineClass
{
    [string] $accountId 
    [decimal] $quantity  
    [decimal] $unitPrice     
}

```
To use [Batch Call](https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/webservices/use-odata-batch#request-body) we need to POST requests. We will define classes to manage the list of request.
 

```
class ListOfRequestClass
{
    [RequestClass[]] $requests
    
    [void]addRequest([RequestClass]$request) 
    {
        $this.requests += $request
    }
}

class RequestClass
{
    [string] $method = "POST"
    [string] $id  = ""
    [string] $url = ""
    [ContentTypeClass] $headers     
    [SalesInvoiceClass] $body    
}

```
Please note the AddLine()/AddRequest() method to manage the corresponding collections inside each class and the default "POST" value.

We need one last (utility) class to have a default value in the header :

```
class ContentTypeClass {
    [string] ${Content-Type} = "application/json"
}

```
Now we can instantiate objects to create an invoice with two lines : 

```
# Init a new Sales Invoice line
$salesInvoiceLine1 = New-Object SalesInvoiceLineClass
$salesInvoiceLine1.accountid = "$accountid"
$salesInvoiceLine1.quantity = 1
$salesInvoiceLine1.unitPrice = 10

# Init a new Sales Invoice line
$salesInvoiceLine2 = New-Object SalesInvoiceLineClass
$salesInvoiceLine2.accountid = "$accountid"
$salesInvoiceLine2.quantity = 2
$salesInvoiceLine2.unitPrice = 15

# Init a new Sales Invoice 
$salesInvoice = New-Object SalesInvoiceClass
$salesInvoice.customerId = "$customerid"
$salesInvoice.addLine($salesInvoiceLine1)
$salesInvoice.addLine($salesInvoiceLine2)

```

The corresponding JSON using command $salesInvoice | ConvertTo-Json is : 
```
{
    "customerId":  "4636f8d6-c141-ec11-bb7b-000d3a256200",
    "salesInvoiceLines":  [
                              {
                                  "accountId":  "2236f8d6-c141-ec11-bb7b-000d3a256200",
                                  "quantity":  1,
                                  "unitPrice":  10
                              },
                              {
                                  "accountId":  "2236f8d6-c141-ec11-bb7b-000d3a256200",
                                  "quantity":  2,
                                  "unitPrice":  15
                              }
                          ]
}


```
We can now build a list of request containing 100 invoices (using the Invoice in the body request):

```
$listofrequest = New-Object ListOfRequestClass

$numberOfInvoices = 100
for($i = 0; $i -lt $numberOfInvoices ; $i++) 
{
    # Init a new request
    $request = New-Object RequestClass
    $request.id = $i
    $request.url = "companies($companyid)/salesInvoices" 
    $request.headers = $ContentType
    $request.body = $salesInvoice

    $listofrequest.addRequest($request)
} 
```

Please find the result [here](https://drive.google.com/file/d/1ZyKgLaWknzm7J6CmfFs7-cmg2KdHjWns/view?usp=sharing) : a full JSON document that can be used with your batch API [URL](https://api.businesscentral.dynamics.com/v2.0/Production/api/v2.0/$batch) to create 100 invoices in one call.

Enjoy !


![Using Powershell Class to generate JSON for Batch Call API with Business Central](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l83q0zvabxswcnsrvx5h.png)