Batch actions with app items

When you need to process a large number of app items in a single request, you can write a script using methods for batch adding or updating of the app item fields:

  • Application.batch().save() — batch add app items;
  • Application.batch().update() — batch update app item fields;
  • Application.batch().delete() — batch delete app items. Please note:
  • No more than 10,000 items can be processed in a single request;
  • The app where batch operations are performed must have data import rights granted to the current user. These limitations apply to all of the specified methods.

    Batch add app items

    By usingApplication.batch().save(), you can, in a single request:
  • Save new items to the database;
  • Update fields of existing items. In this case, specify their identifiers to find the required items, make changes to them, and save the edited items in the database. When listing the items to be created, you can use the following in the method:
  • array, to create a specific number of data items;
    await Application.batch().save().items(orders).all(); // Here `orders` is an array of app items that were changed
    
  • generator, to work with a large volume of data and dynamically create items one by one until the set number is reached. Then the created objects are saved in the database. This way the memory of the worker service is not overloaded.
    await Application.batch().save().items((function*(){ // Set up batch saving of items and, into the items method, get the items to be created by the generator function
      for (var i = 0; i < 500; i++) { // Loop 500 times to create 500 items
          var item = Application.create(); // Create an item each time
          yield item // Return the created item from the generator
      }
    })()).all(); // Save all items
    
    For more information on Application.batch().save() see the following articles:
  • "Object of saving app items";
  • "Configuration object of batch saving of app items";
  • "Batch actions with app items in scripts".

    Batch update of app item fields

    You can use the method Application.batch().update() to update many fields in one request. You can:
  • Specify the rules for such updates;
  • Define search criteria for the items that need to be updated;
  • Limit the number of items to be updated. When writing a script with Application.batch().update(), you can define criteria for the items to be updated using the set()method, where:

1.\tSet the name of the field to be updated. You can only select an existing app field. 2.\tSpecify what to do with this field:

  • Fill it with a specific value. For a Number field, only a number can be specified; for a String field, text, and so on;
    Application.batch().update().set('numberField', (f, op) => 1); // OK. The constant matches the field type `numberField` – Number
    Application.batch().update().set('numberField', (f, op) => ''); // Error. A string constant does not match the field type Number
    
  • Fill it with the value of another field of the same type;
    Application.batch().update().set('numberField1', (f, op) => f.numberField2); // OK. The field types `numberField1` and `numberField2`are both numbers
    Application.batch().update().set('numberField1', (f, op) => f.strField); // Error. The string field `strField` cannot be matched to the `numberField1` number field
    
  • Perform an action on the field value:
    • Increase a Number field by a specified value;
      Application.batch().update().set('numberField', (f, op) => op.inc(f.numberField, 1)); // Increase the value of the `numberField` field by 1
      
    • Join two or more String fields;
      Application.batch().update().set('strLocation', (f, op) => op.concat(f.strCountry, ',', f.strCity)); // The`strLocation` field will be updated as '{strCountry}, {strCity}', meaning it will contain both values separated by a comma
      
    • Remove an item in fields with the Many option;
      Application.batch().update().set('users', (f, op) => op.remove(user)); // User deleted
      
    • Add an item to the fields with the Many option.
      Application.batch().update().set('users', (f, op) => op.push(user, user1)); // Add two users to the end of the array. The type of the`user` and `user1` fields must be the same as the single item of the `users` field
      
      When using theApplication.batch().update() method, be aware of the following:
  • A field can only be updated once per request.
  • If the number of updated items is not specified in the script, 10 items will be updated by default. The maximum value is 10,000 items.
  • The actions you define in a single request within the set() methods are independent of each other and do not affect one another. Therefore, the order in which they are listed does not matter. Example:
    var rows1 = await Application.batch()
         .update()
         .set('number', _ => 100)
         .set('another_number', f => t.number)
         .all();
    var rows2 = await Application.batch()
         .update()
         .set('another_number', f => t.number)
         .set('number', _ => 100)
         .all();
    // In both cases, `number` = 100, and `another_number` = value of the `number` field before update
    
    For more information on using Application.batch().update(), also see the following articles:
  • "Batch update method";
  • "Object of updating app items";
  • "Configuration object of batch updating app items";
  • "Batch actions with app items in scripts".

    Batch delete app items

    You can use the Application.batch().delete() item to delete multiple app items in a single request. You can:
  • Define search criteria for the items to be deleted;
  • Limit the number of items to be deleted. When using the Application.batch().delete() method, note that if the number of items to be deleted is not specified in the script, 10 items will be deleted by default. The maximum value is 10,000 items. Example:
    var count = await Application.batch()
    .delete()
    // Define search criteria for the items to be deleted
    .where(f => f.number.gte(10))
    .size(100)
    .all();,
    
    For more information on using Application.batch().delete(), also see the following articles:
  • "Batch update method";
  • "Configuration object of batch updating app items";
  • "Batch actions with app items in scripts".