Skip to content

Updating a single object

Important

This operation updates a single object in the database. To update more than one object, use the bulkUpdate operation documented in the Updating multiple objects chapter of this guide.

To add this operation to a Unit Of Work request, use the following operation in the request payload:

{
  "isolationLevelEnum" : "REPEATABLE_READ" | "READ_COMMITTED" | "READ_UNCOMMITTED" | "SERIALZABLE",
  "operations": [
    {
      "operationType": "UPDATE",
      "table": "TABLENAME",
      "opResultId": "OPRESULT-ID",
      "payload": {

      }
    }
  ]
}

where:

Argument                Description
"operationType" The value must be "UPDATE", this designates the operation as "updating a single object". The object to update in the database is defined in the "payload" property.
"table" The value must contain the name of the table where the object will be saved. 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" The value is mandatory. Must contain a JSON object which will be updated in a table identified with the "table" property. The object must have the objectId property with a valid value. The value of the objectId property can be a reference to a result from a previous operation or an explicit value. All other properties in the object will overwrite what's already in the database thus performing the update operation.

Return Value

This operation returns the updated object. The object or any of its properties can be used in other operations of the same transaction.

Example

The examples below demonstrate the usage of the operation for all possible ways to reference the object that should be updated. This is the what the data table looks like before the transaction runs:

data-before-update.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",
      "table": "Order",
      "opResultId": "updateOrderOperation",
      "payload": {
        "objectId":"FFE7D010-91A9-ACD1-FFDD-FDC52B922000",
        "orderStatus":"complete",
        "deliveryDate":"04/03/2020"
      }
    }    
  ]
}

Response:

{
    "success": true,
    "error": null,
    "results": {
        "updateOrderOperation": {
            "type": "UPDATE",
            "result": {
                "orderStatus": "complete",
                "deliveryDate": 1585872000000,
                "updated": 1586026474000,
                "___class": "Order",
                "created": 1585961873000,
                "ownerId": null,
                "objectId": "FFE7D010-91A9-ACD1-FFDD-FDC52B922000",
                "orderId": "031820-CV1",
                "amount": 189.2
            }
        }
    }
}

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": "findOrderOperation",
      "payload": {
        "whereClause":"orderId = '031820-CV1'",
        "pageSize":1
      }
    },
    {
      "operationType": "UPDATE",
      "table": "Order",
      "opResultId": "updateOrderOperation",
      "payload": {
        "objectId" : {
            "___ref":true,
            "opResultId":"findOrderOperation",
            "resultIndex":0,
            "propName":"objectId"
        },
        "orderStatus":"complete",
        "deliveryDate":"04/01/2020"
      }
    }    
  ]
}

Response:

{
    "success": true,
    "error": null,
    "results": {
        "findOrderOperation": {
            "type": "FIND",
            "result": [
                {
                    "orderId": "031820-CV1",
                    "orderStatus": "null",
                    "created": 1585961922000,
                    "updated": 1586024673000,
                    "objectId": "F32E20DC-924A-AEA9-FF9D-C9FFBC37FD00",
                    "amount": 189.2,
                    "ownerId": null,
                    "___class": "Order"
                }
            ]
        },
        "updateOrderOperation": {
            "type": "UPDATE",
            "result": {
                "orderStatus": "complete",
                "deliveryDate": 1585699200000,
                "updated": 1586024728000,
                "___class": "Order",
                "created": 1585961922000,
                "ownerId": null,
                "objectId": "F32E20DC-924A-AEA9-FF9D-C9FFBC37FD00",
                "orderId": "031820-CV1",
                "amount": 189.2
            }
        }
    }
}

This is what the same object in the data table looks like after the transaction runs:

data-after-update.zoom70