Skip to content
v2.7 (latest)

Save and Update Entities ​

This guide explains how to save new Persisted Entity instances and update existing ones.

Prerequisites ​

Save a new instance ​

To save a new instance of a Persisted Entity, create the instance and call .save() on it.

  1. Open your Ruleset and create a Rule named SavePerson.
  2. Enter the following expression:
javascript
let person = new Person({ FirstName: 'Jane', LastName: 'Doe', Birthdate: '1990-06-15' })
person.save()
return person.getId()
  1. Save the Ruleset and click Try it out to verify the entity is saved.

The first call to .save() creates a new record in the database and assigns an ID to the instance. Subsequent calls to .save() on the same instance update the existing record.

Update an existing instance ​

To update an existing entity, retrieve it first, modify its properties, then call .save() again.

  1. Create a Rule named UpdatePerson with the following expression:
javascript
let results = Person.getAll()
let person = results.items[0]
person.FirstName = 'John'
person.save()
return person

The entity is updated in the database. The ID remains the same.

Save multiple instances at once ​

To save an array of entity instances in a single operation, use the static Person.save() method.

  1. Create a Rule with the following expression:
javascript
let person1 = new Person({ FirstName: 'Alice', LastName: 'Smith', Birthdate: '1985-03-22' })
let person2 = new Person({ FirstName: 'Bob', LastName: 'Jones', Birthdate: '1978-11-05' })
return Person.save([person1, person2])

The method returns the saved instances, including their assigned IDs.

Next steps ​

For a full overview of all available methods, see the Persisted Entities Language Reference.