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 instance, use the following methods. The methods are split into categories based on how the the objects are identified in the API:

// This operation explicitly lists the objectId values of the objects
// which will be updated in the database. These objectId values must 
// be present in the objectIds argument. Each referenced 
// object must be stored in the database table with the "tableName" 
// name. Backendless identifies the objects in the database and 
// updates them with the values from the changes argument. 
// The "changes" argument is an object treated as a collectin of
// key/value pairs. Each key in the object must correspond to a 
// column/property of the updated objects. The value corresponding 
// to the key is the update the objects will receive.
unitOfWork.bulkUpdate( tableName: string, 
                       objectIds: string[], 
                       changes: object );
// This operation runs a query expressed through the whereClause 
// argument. The query is executed in a table with the "tableName"
// name. For more information see the API documentation about 
// querying with whereClause. Objects which match the query 
// are updated with the values from the "changes" argument. 
// The "changes" argument is an object treated as a collection of 
// key/value pairs. Each key in the  object must correspond to 
// a column/property of the updated objects. The value 
// corresponding to the key is the update the objects will receive.
unitOfWork.bulkUpdate( tableName: string, 
                       whereClause: string, 
                       changes: object );
// Update the objects identified by the opResult argument,
// which is a result of another operation in the same transaction. 
// These operations can be either "Retrieving objects" or "Saving 
// multiple objects". Objects identified by "opResult" 
// are updated in the database with the values from the "changes" 
// objectt.
unitOfWork.bulkUpdate( opResult: OpResult, changes: object );

Return Value

The operation returns an OpResult object which represents the result of this operation - number of objects updated in the database.

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

const unitOfWork = new Backendless.UnitOfWork();

// compose a list of objects to be updated
// here we hard-code the objectId values,
// but in your code these values may be
// coming either from a query or from your
// data model on the client side.
const objectsForChanges = [ 
   "EE3BF4B5-DB88-1425-FF89-CC11B7707500",
   "0CF23E36-FCC0-4E04-FF3E-8B67E6E27200",
   "DFFEDE1D-E423-2472-FF71-26EEC3F23700" 
]

// compose the changes object. The change it
// has it to update the "orderStatus" column
// with the value "completed"
const changes = {orderstatus : "completed" }

// add the "bulkUpdate" operation to the transaction
unitOfWork.bulkUpdate( "Order", objectsForChanges,  changes );

// run the transaction
unitOfWork.execute()
   .then(function (unitOfWorkResult) {
       // transaction is complete. Use uowResult to check the result
    })
   .catch( function( error ) {
       // Server reported an error.
   });
const unitOfWork = new Backendless.UnitOfWork();

// set the where clause. It will identify all
// objects where the "orderStatus" column has the
// value of "delivery pending".
const whereClause = "orderStatus = 'delivery pending'";

// compose the changes object. The change it
// has it to update the "orderStatus" column
// with the value "completed"
const changes = {orderstatus : "completed" }

// add the "bulkUpdate" operation to the transaction
unitOfWork.bulkUpdate( "Order", whereClause,  changes );

// run the transaction
unitOfWork.execute()
   .then(function (unitOfWorkResult) {
       // transaction is complete. Use uowResult to check the result
    })
   .catch( function( error ) {
       // Server reported an error.
   });
const unitOfWork = new Backendless.UnitOfWork();

// compose a query. It will identify all Order
// objects where the "orderStatus" column has the
// value of "delivery pending".
const queryBuilder = Backendless.DataQueryBuilder.create();
queryBuilder.setWhereClause( "orderStatus = 'delivery pending'" );
queryBuilder.setPageSize( 100 );
// fetch the order objects with the query.
const pendingOrdersResult = unitOfWork.find( "Order", queryBuilder );


// compose the changes object. The change it
// has it to update the "orderStatus" column
// with the value "completed"
const changes = {orderstatus : "completed" }

// add the "bulkUpdate" operation to the transaction
unitOfWork.bulkUpdate( pendingOrdersResult,  changes );

// run the transaction
unitOfWork.execute()
   .then(function (unitOfWorkResult) {
       // transaction is complete. Use uowResult to check the result
    })
   .catch( function( error ) {
       // Server reported an error.
   });