Shopware 6 API Bulk Update entity product and other issue single error dont update all

962 Views Asked by At

Require a small help in API updating the products

https://shopware.stoplight.io/docs/admin-api/faf8f8e4e13a0-bulk-payloads#performance

  • We have products 1, 2, 3 .....upto 100 and we update quantity in BULK from our ERP (single operation)
  • Due to some reason (suppose) instead of integer we send string to 80th product --- so the API response gives error (which is ok)
  • But not only 80th product fails to update but the complete batch fails so 1 to 100 product fails - non of them are updated

Please help how to fix this or correct us if we are wrong??

Using the reference https://shopware.stoplight.io/docs/admin-api/faf8f8e4e13a0-bulk-payloads#performance

- We are updating "entity": "product", and "action": "upsert" with headers
// --header 'single-operation: 1'
// --header 'indexing-behavior: use-queue-indexing'

All working very good but as we are using the entity in bulk if any one of the entity fails the complete bulk gives error and non of the product from the bulk list gets updated

If we are sending 1 to 100 products and problem is only at 80th product, only that does not get updated -- but rest 99 products should get updated

2

There are 2 best solutions below

1
On

The behavior you describe is the expected behavior when you use single-operation: 1, as it is described in the reference you linked.

With single-operation: 1 all operations you sent are executed inside a single database transaction, so when one of the operations fails, the others are also rollbacked.

From what you describe you want to use single-operation: 0, which means that each operation you sent has it's own database transaction, so that when one operation fails the others still go through.

0
On

Reference URL https://shopware.stoplight.io/docs/admin-api/faf8f8e4e13a0-bulk-payloads#examples

Update product stocks
{
    "stock-updates": { // Name of the transaction, choose freely 
        "entity": "product", // Name of the entity you would like to update
        "action": "upsert", // Available actions are upsert and delete,
        "payload": [ // A list of objects, each representing a subset of the entity scheme referenced in `entity`. `id` is required for upsert operations.
            {
                "id": "c197170c60ab472b8dc1218acaba220e",
                "stock": 41
            },
            {
                "id": "a10c59fce8214fdaa552d74e0c6347ff",
                "stock": 'XXXXX'
            },
            {
                "id": "1b13176b6e0f4bb496c9a31b4fd7e97b",
                "stock": 43
            }
        ]
    }
}

We have tried the following to update the stock -- but instead of integer sending string XXXXX for one product in the bulk, so that we can see the error for one product which does not update other products. (complete bulk not updated )

Below are the scenario 1 ( single-operation: 1 ) and scenario 2 ( single-operation: 0 ) in both our stock is not getting updated. As both scenario are not updating the stock which we feel is wrong.

Our Problem

Single Operation: 0 (Separate transaction) - ideally should work and atleast update the stock of c197170c60ab472b8dc1218acaba220e to 41 and 1b13176b6e0f4bb496c9a31b4fd7e97b to 43 but the complete bulk is update is ignored by API.

==========

Scenario 1 = single-operation: 1

Response 1: Array
(
    [error] => 1
    [message] => Array
        (
            [errors] => Array
                (
                    [0] => Array
                        (
                            [code] => ba785a8c-82cb-4283-967c-3cf342181b40
                            [status] => 400
                            [detail] => This value should be of type int.
                            [template] => This value should be of type {{ type }}.
                            [meta] => Array
                                (
                                    [parameters] => Array
                                        (
                                            [{{ value }}] => "XXXXX"
                                            [{{ type }}] => int
                                        )

                                )

                            [source] => Array
                                (
                                    [pointer] => /product/2/stock
                                )

                        )

                )

        )

)

==========

Scenario 2 = single-operation: 0

Response 2: Array
(
    [error] => 1
    [message] => Array
        (
            [success] => 
            [data] => Array
                (
                    [product] => Array
                        (
                            [result] => Array
                                (
                                    [0] => Array
                                        (
                                            [entities] => Array
                                                (
                                                )

                                            [errors] => Array
                                                (
                                                )

                                        )

                                    [1] => Array
                                        (
                                            [entities] => Array
                                                (
                                                )

                                            [errors] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [code] => ba785a8c-82cb-4283-967c-3cf342181b40
                                                            [status] => 400
                                                            [detail] => This value should be of type int.
                                                            [template] => This value should be of type {{ type }}.
                                                            [meta] => Array
                                                                (
                                                                    [parameters] => Array
                                                                        (
                                                                            [{{ value }}] => "XXXXX"
                                                                            [{{ type }}] => int
                                                                        )

                                                                )

                                                            [source] => Array
                                                                (
                                                                    [pointer] => /2/stock
                                                                )

                                                        )

                                                )

                                        )

                                    [2] => Array
                                        (
                                            [entities] => Array
                                                (
                                                )

                                            

                                        )

                                )

                            [extensions] => Array
                                (
                                )

                        )

                )

        )

)