DEV Community

Cover image for Using PowerShell Class to generate JSON for Batch Call API with Business Central
Xavier Garonnat
Xavier Garonnat

Posted on

Using PowerShell Class to generate JSON for Batch Call API with Business Central

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 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 and Sales Invoice Line API.

class SalesInvoiceClasss
{
    [string] $customerId 
    [SalesInvoiceLineClass[]] $salesInvoiceLines

    [void]addLine([SalesInvoiceLineClass]$SalesInvoiceLine) 
    {
        $this.salesInvoiceLines += $SalesInvoiceLine
    }
}

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

Enter fullscreen mode Exit fullscreen mode

To use Batch Call 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    
}

Enter fullscreen mode Exit fullscreen mode

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"
}

Enter fullscreen mode Exit fullscreen mode

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)

Enter fullscreen mode Exit fullscreen mode

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
                              }
                          ]
}


Enter fullscreen mode Exit fullscreen mode

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)
} 
Enter fullscreen mode Exit fullscreen mode

Please find the result here : a full JSON document that can be used with your batch API URL to create 100 invoices in one call.

Enjoy !

Using Powershell Class to generate JSON for Batch Call API with Business Central

Top comments (0)