Skip to content

Updating multiple objects

This operation updates one or more objects in a database table. The objects to be updated can be identified with:

  1. objectId values
  2. A query string (a where clause)
  3. Result of a previously ran bulkCreate or object retrieval operation in the same transaction.

To add this operation to a Unit Of Work request, use the following operation in the request payload. The payload are split into categories based on how the the objects are identified in the API:

URL:
POST https://api.backendless.com/APP-ID/REST-API-KEY/transaction/unit-of-work
Request header:
Content-Type:application/json
Request body:
{
  "isolationLevelEnum" : "REPEATABLE_READ" | "READ_COMMITTED" | "READ_UNCOMMITTED" | "SERIALZABLE",
  "operations": [
    {
      "operationType": "UPDATE_BULK",
      "table": "TABLENAME",
      "opResultId": "OPRESULT-ID",
      "payload": {
         "unconditional" : [ "objectId VALUE", "objectId VALUE", "objectId VALUE" ],
         "changes" : { object with key/value changes to apply }
      }
    }
  ]
}

where:

Argument                Description
"operationType" The value must be "UPDATE_BULK", this designates the operation as "updating multiple objects". The objects to update in the database are defined in the "payload" > "unconditional" property.
"table" The value must contain the name of the table where the objects will be updated.
"opResultId" This is an optional property. If present, must contain an Id for this operation. An operation Id is a free-form string that must be unique within the transaction. If the result of this operation is to be used as an input in another operation within the same transaction, the Id will be used to reference the result.
"payload" > "unconditional" The value is mandatory. Must contain a JSON array. The array contains objectId values identifying the objects which will be updated by this operation.
"payload" > "changes" The value is mandatory. A JSON object containing the changes which will be applied to the objects from the "unconditional" property.

URL:
POST https://api.backendless.com/APP-ID/REST-API-KEY/transaction/unit-of-work
Request header:
Content-Type:application/json
Request body:
{
  "isolationLevelEnum" : "REPEATABLE_READ" | "READ_COMMITTED" | "READ_UNCOMMITTED" | "SERIALZABLE",
  "operations": [
    {
      "operationType": "UPDATE_BULK",
      "table": "TABLENAME",
      "opResultId": "OPRESULT-ID",
      "payload": {
         "conditional" : "where clause query to identify objects",
         "changes" : { object with key/value changes to apply }
      }
    }
  ]
}

where:

Argument                Description
"operationType" The value must be "UPDATE_BULK", this designates the operation as "updating multiple objects". The objects to update in the database is defined in the "payload" > "unconditional" property.
"table" The value must contain the name of the table where the objects will be updated. The table must exist in the database before the transaction is executed.
"opResultId" This is an optional property. If present, must contain an Id for this operation. An operation Id is a free-form string that must be unique within the transaction. If the result of this operation is to be used as an input in another operation within the same transaction, the Id will be used to reference the result.
"payload" > "conditional" The value is mandatory. Must contain a string value which is a where clause query.The server runs the query in the "table" table to identify the objects which will be updated by this operation.
"payload" > "changes" The value is mandatory. A JSON object containing the changes which will be applied to the objects from the "unconditional" property.

URL:
POST https://api.backendless.com/APP-ID/REST-API-KEY/transaction/unit-of-work
Request header:
Content-Type:application/json
Request body:
{
  "isolationLevelEnum" : "REPEATABLE_READ" | "READ_COMMITTED" | "READ_UNCOMMITTED" | "SERIALZABLE",
  "operations": [
    {
      "operationType": "UPDATE_BULK",
      "table": "TABLENAME",
      "opResultId": "OPRESULT-ID",
      "payload": {
         "unconditional" : {
            "___ref":true,
            "opResultId":"OPERATION ID OF FIND OR BULK CREATE OPERATION"
        },
        "changes" : { object with key/value changes to apply }
      }
    }
  ]
}

where:

Argument                Description
"operationType" The value must be "UPDATE_BULK", this designates the operation as "updating multiple objects". The objects to update in the database is defined in the "payload" > "unconditional" property.
"table" The value must contain the name of the table where the objects will be updated. The table must exist in the database before the transaction is executed.
"opResultId" This is an optional property. If present, must contain an Id for this operation. An operation Id is a free-form string that must be unique within the transaction. If the result of this operation is to be used as an input in another operation within the same transaction, the Id will be used to reference the result.
"payload" > "unconditional" The value is mandatory. Must contain a JSON object with the structure as shown above. The "opResultId" property must contain operation result id of a previous operation in the same transaction. These previous operations can be either Retrieving objects or Saving multiple objects.
"payload" > "changes" The value is mandatory. A JSON object containing the changes which will be applied to the objects from the "unconditional" property.

Return Value

This operation returns the number of updated objects. The returned value can be used in other operations of the same transaction.

Example

Consider the example below. The example adds an operation to update multiple objects in the database. The change is to set the orderStatus property to "completed" for all orders which were delivered. This is the what the data table looks like before the transaction runs:

data-before-bulkupdate.zoom70

Request:

URL:

POST https://api.backendless.com/APP-ID/REST-API-KEY/transaction/unit-of-work
Request header:
Content-Type:application/json
Request body:
{
  "operations": [
    {
      "operationType": "UPDATE_BULK",
      "table": "Order",
      "opResultId": "updateOrderOperation",
      "payload": {
        "unconditional" : [
            "86804C97-0944-9EE8-FFC1-EFB2F4491700",
            "0E724EC9-FD8E-29E4-FFD8-DFFFE69A0200",
            "BC8B738C-B1B0-DA83-FFC5-E7ECCAADC700"
            ],
        "changes" : {
          "orderStatus":"completed" 
        }
      }
    }    
  ]
}

Response:

{
    "success": true,
    "error": null,
    "results": {
        "updateOrderOperation": {
            "type": "UPDATE_BULK",
            "result": 3
        }
    }
}

Request:

URL:

POST https://api.backendless.com/APP-ID/REST-API-KEY/transaction/unit-of-work
Request header:
Content-Type:application/json
Request body:
{
  "operations": [
    {
      "operationType": "UPDATE_BULK",
      "table": "Order",
      "opResultId": "updateOrderOperation",
      "payload": {
        "conditional" : "orderStatus = 'delivery pending'",
        "changes" : {
          "orderStatus":"completed" 
        }
      }
    }    
  ]
}

Response:

{
    "success": true,
    "error": null,
    "results": {
        "updateOrderOperation": {
            "type": "UPDATE_BULK",
            "result": 3
        }
    }
}

Request:

URL:

POST https://api.backendless.com/APP-ID/REST-API-KEY/transaction/unit-of-work
Request header:
Content-Type:application/json
Request body:
{
  "operations": [
    {
      "operationType": "FIND",
      "table": "Order",
      "opResultId": "findOrdersOperation",
      "payload": {
        "whereClause" : "orderStatus = 'delivery pending'"
      }
    },
    {
      "operationType": "UPDATE_BULK",
      "table": "Order",
      "opResultId": "updateOrderOperation",
      "payload": {
        "unconditional" : {
            "___ref":true,
            "opResultId":"findOrdersOperation"
        },
        "changes" : {
          "orderStatus":"completed" 
        }
      }
    }   
  ]
}

Response:

{
    "success": true,
    "error": null,
    "results": {
        "findOrdersOperation": {
            "type": "FIND",
            "result": [
                {
                    "orderId": null,
                    "orderStatus": "delivery pending",
                    "created": 1586030143000,
                    "updated": 1586038151000,
                    "objectId": "F3020117-E8CE-18D8-FFAF-EA62DB582000",
                    "amount": 3.0,
                    "deliveryDate": null,
                    "ownerId": null,
                    "___class": "Order"
                }
            ]
        },
        "updateOrderOperation": {
            "type": "UPDATE_BULK",
            "result": 1
        }
    }
}

This is what the objects look like in the database after the transaction runs:

data-after-bulkupdate.zoom70