Showing posts with label Batch API. Show all posts
Showing posts with label Batch API. Show all posts

Monday, February 3, 2025

Push Multiple Records in a Single Http Request Using Batch API in Logic Apps

 Pushing Multiple Records in a Single Request Using Batch API in Logic Apps:

        We can perform the multiple operations in single request by using Batch API. To know more Click Here

Please follow the below steps:

  • I have added the HTTP request.
  • I configured the following URL and headers.
    Content-Type: multipart/mixed; boundary=batch_CR


  • I used the batch number as batch_CR and the change set number as changeset_EE.
  • I am performing three operations:
    • Creating a record in the vendgroup table.
    • Updating a record in the custgroup table.
    • Deleting a record in the vendgroup table
      --batch_CR
      Content-Type: multipart/mixed; boundary=changeset_EE
      
      --changeset_EE
      Content-Type: application/http
      Content-Transfer-Encoding: binary
      Content-ID: 1
      
      POST VendorGroups?cross-company=true HTTP/1.1
      Content-Type: application/json; type=entry
      
      {
          "dataAreaId": "USMF",
          "VendorGroupId": "Test04",
          "Description": "Test 4",
      }
      
      --changeset_EE
      Content-Type: application/http
      Content-Transfer-Encoding: binary
      Content-ID: 2
      
      PATCH CustomerGroups(dataAreaId='USMF',CustomerGroupId= 'TestC01')?Cross-company=true HTTP/1.1
      Content-Type: application/json; type=entry { "Description": "Test C1111" } --changeset_EE Content-Type: application/http Content-Transfer-Encoding: binary Content-ID: 3 DELETE VendorGroups(dataAreaId='USMF',VendorGroupId= 'Test03')?Cross-company=true HTTP/1.1 Content-Type: application/json; type=entry --changeset_EE-- --batch_CR--
  • I used OAuth authentication, providing the Client ID, Secret, and Tenant ID.

Output:

 

If the Batch API request Succeeds:

  1. It returns a 201 status code for the create operation, along with the entire payload of the first request.


  2. It returns a 204 status code for the update and delete operations, as no content is returned after an update and delete.

 

If the Batch API request Fails:

  • The main status code is 200, but within the response body, it returns a 500 error.
  • Below is the output of the Batch API request, with error message highlighted in a green box.

Fetching the Error Message:

  • I added conditions to handle both success and failure cases.

  • If the Batch API request fails, I retrieve the error message using the following expression.
    • Fetch the error
      body('Batch_API')['$multipart'][0]['body']['$multipart'][0]['body']['$applicationHttp']['body']['error']
    • Fetch the error status code
      body('Batch_API')['$multipart'][0]['body']['$multipart'][0]['body']['$applicationHttp']['statusCode']

 

  • If the Batch API request succeeds, I extract the status code using the appropriate expression.
    • Fetch the body
      body('Batch_API')['$multipart'][0]['body']['$multipart'][0]['body']['$applicationHttp']['body']
    • Fetch the status code
      body('Batch_API')['$multipart'][0]['body']['$multipart'][0]['body']['$applicationHttp']['statusCode']



Keep Daxing!!

Monday, December 23, 2024

Batch API for D365FO

Using Batch API for D365FO 
        


  • You can send requests for multiple operations in a single HTTP request.
  • Batch requests can include up to 1000 individual requests but cannot contain other batch requests.

Without Change Sets :

  • If you send 10 requests in a batch and 2 fail, the remaining requests will not be stopped and will continue to be processed.
              "Prefer": "odata.continue-on-error" - Need to add this in header to continue the process

Using Change Sets :

  • In addition to individual requests, a batch request can include change sets:
  • When multiple operations are included in a change set, if any one operation fails, all completed operations are rolled back.


1. Batch URL for request.

{{resource}}/data/$batch

2. Include the following keys and values in the header:

    Content-Type : multipart/mixed; boundary="batch_22975cad-7f57-410d-be15-6363209367ea"


3. Batch number: Must be unique for the entire request. 

        If you use the same batch ID for multiple requests, there will be no issue. However, if you change it, make sure to cross-check it once.

batch_22975cad-7f57-410d-be15-6363209367ea

4. Content-ID: Must be unique for each step.

5. While starting the batch or change set use -- as prefix.

6. While ending use -- as suffix.

 

7. The 200 OK response indicates the status of the entire batch request. To find the response for each individual request, you need to review the responses within the batch response separately. 

Success Response:                                                                        Failed Response:

Success ResponseFailed Response

8. The below part is considered as one request.


Batch API for Insert

    This example will insert a vendor group and a customer group using the Batch API. 
Request : 

--batch_22975cad-7f57-410d-be15-6363209367ea
Content-Type: multipart/mixed; boundary="changeset_246e6bfe-89a4-4c77-b293-7a433f082e8a"

--changeset_246e6bfe-89a4-4c77-b293-7a433f082e8a
Content-Type: application/http
Content-Transfer-Encoding: binary
Content-ID: 1

POST VendorGroups?cross-company=true HTTP/1.1
Content-Type: application/json; type=entry

{
    "dataAreaId": "USMF",
"VendorGroupId": "Test01", "Description": "Test 1" } --changeset_246e6bfe-89a4-4c77-b293-7a433f082e8a Content-Type: application/http Content-Transfer-Encoding: binary
Content-ID: 2 POST VendorGroups?cross-company=true HTTP/1.1 Content-Type: application/json; type=entry { "dataAreaId": "USMF",
"VendorGroupId": "Test02", "Description": "Test 2" } --changeset_246e6bfe-89a4-4c77-b293-7a433f082e8a Content-Type: application/http Content-Transfer-Encoding: binary
Content-ID: 3 POST CustomerGroups?cross-company=true HTTP/1.1 Content-Type: application/json; type=entry { "dataAreaId": "USMF", "CustomerGroupId": "TestC01", "Description": "Test 1" } --changeset_246e6bfe-89a4-4c77-b293-7a433f082e8a-- --batch_22975cad-7f57-410d-be15-6363209367ea--


Request:
       1. Added Vend group record and customer group record.




Response:







Batch API for Update

--batch_22975cad-7f57-410d-be15-6363209367ea
Content-Type: multipart/mixed; boundary="changeset_246e6bfe-89a4-4c77-b293-7a433f082e8a"

--changeset_246e6bfe-89a4-4c77-b293-7a433f082e8a
Content-Type: application/http
Content-Transfer-Encoding: binary
Content-ID: 1

PATCH VendorGroups(dataAreaId='USMF',VendorGroupId= 'Test01')?Cross-company=true HTTP/1.1
Content-Type: application/json; type=entry { "Description": "Test 1111" } --changeset_246e6bfe-89a4-4c77-b293-7a433f082e8a Content-Type: application/http Content-Transfer-Encoding: binary Content-ID: 2 PATCH CustomerGroups(dataAreaId='USMF',CustomerGroupId= 'TestC01')?Cross-company=true HTTP/1.1
Content-Type: application/json; type=entry { "Description": "Test C1111" } --changeset_246e6bfe-89a4-4c77-b293-7a433f082e8a-- --batch_22975cad-7f57-410d-be15-6363209367ea--

Response:

Batch API for Delete

--batch_22975cad-7f57-410d-be15-6363209367ea
Content-Type: multipart/mixed; boundary="changeset_246e6bfe-89a4-4c77-b293-7a433f082e8a"

--changeset_246e6bfe-89a4-4c77-b293-7a433f082e8a
Content-Type: application/http
Content-Transfer-Encoding: binary
Content-ID: 1

DELETE VendorGroups(dataAreaId='USMF',VendorGroupId= 'Test01')?Cross-company=true HTTP/1.1
Content-Type: application/json; type=entry --changeset_246e6bfe-89a4-4c77-b293-7a433f082e8a Content-Type: application/http Content-Transfer-Encoding: binary Content-ID: 2 DELETE CustomerGroups(dataAreaId='USMF',CustomerGroupId= 'TestC01')?Cross-company=true HTTP/1.1
Content-Type: application/json; type=entry --changeset_246e6bfe-89a4-4c77-b293-7a433f082e8a-- --batch_22975cad-7f57-410d-be15-6363209367ea--
Response:

Batch API for Insert, update and Delete

--batch_22975cad-7f57-410d-be15-6363209367ea
Content-Type: multipart/mixed; boundary="changeset_246e6bfe-89a4-4c77-b293-7a433f082e8a"

--changeset_246e6bfe-89a4-4c77-b293-7a433f082e8a
Content-Type: application/http
Content-Transfer-Encoding: binary
Content-ID: 1

POST VendorGroups?cross-company=true HTTP/1.1
Content-Type: application/json; type=entry

{
    "dataAreaId": "USMF",
"VendorGroupId": "Test03", "Description": "Test 3" } --changeset_246e6bfe-89a4-4c77-b293-7a433f082e8a Content-Type: application/http Content-Transfer-Encoding: binary Content-ID: 2 PATCH CustomerGroups(dataAreaId='USMF',CustomerGroupId= 'TestC01')?Cross-company=true HTTP/1.1
Content-Type: application/json; type=entry { "Description": "Test C1111" } --changeset_246e6bfe-89a4-4c77-b293-7a433f082e8a Content-Type: application/http Content-Transfer-Encoding: binary Content-ID: 3 DELETE VendorGroups(dataAreaId='USMF',VendorGroupId= 'Test01')?Cross-company=true HTTP/1.1
Content-Type: application/json; type=entry --changeset_246e6bfe-89a4-4c77-b293-7a433f082e8a-- --batch_22975cad-7f57-410d-be15-6363209367ea--
Response:

     1. Create record response.
 
         
       2. Update and Delete responses.


MS Link.

Keep Daxing!!