set

To update a value of a given field

This function updates a field and its value. You can use set to modify a field by specifying its name or ID as text, such as "Name" or "A."

Syntax

set(nid, string, any)

Return

void

Benefits of set

  • Easy to update: The set function makes it easy to update multiple fields without writing a lot of repetitive script. This is especially helpful when you have many fields to handle.

  • Easy maintenance: Since you don't have to hardcode each field name, your scripts are easier to maintain and update. If field names change or new fields are added, you can adjust your script more easily.

  • More flexibility: You can dynamically reference fields by their names or IDs, which makes your scripts more adaptable. This is useful if your data structure changes or if you're working with different data sources that have different fields.

Using set can help you manage your data more efficiently in Ninox, making the process smoother and reducing the likelihood of errors.

Examples

  1. Use set(nid, fieldName, text) to set the field named "Name" to the value "Vania" using the current table's record ID (nid) and text:

set(this, "Name", "Vania")

Return: void

  1. Use set(nid, fieldName, number) to set the field named "Age" to the value "42" using the the current table's record ID (nid) and a number:

set(this, "Age", 42)

Return: void

  1. Use set(this, fieldName, boolean) to set the field named "isActive" to the value "true" using the current table's record ID (nid) and a boolean value:

set(this, "isActive", true)

Return: void

Traditional method

In a typical scenario, you might have an API response with a list of records. For example:

[
  {
    "id": 2,
    "fields": {
      "First name": "Vania",
      "Last name": "Pringour",
      "Birthday": 555292800000,
      "Email": "vpringour0@umn.edu",
      "Address": "4807 Hagan Circle",
      "ZIP": "80995",
      "City": "Munich",
      "Country": "Germany"
    }
  },
  {
    "id": 3,
    "fields": {
      "First name": "Cecil",
      "Last name": "Dorgan",
      "Birthday": 56851200000,
      "Email": "cdorgan1@goo.gl",
      "Address": "12 Ruskin Court",
      "ZIP": "20226",
      "City": "Washington",
      "Country": "United States"
    }
  },
  {
    "id": 4,
    "fields": {
      "First name": "Bettina",
      "Last name": "Clubley",
      "Birthday": 674092800000,
      "Email": "bclubley2@thetimes.co.uk",
      "Address": "5 Onsgard Pass",
      "ZIP": "72905",
      "City": "Fort Smith",
      "Country": "United States"
    }
  },
  {
    "id": 5,
    "fields": {
      "First name": "Raimundo",
      "Last name": "Gatward",
      "Birthday": 524361600000,
      "Email": "rgatward3@topsy.com",
      "Address": "18002 Ramsey Lane",
      "ZIP": "27150",
      "City": "Winston Salem",
      "Country": "United States"
    }
  },
  {
    "id": 6,
    "fields": {
      "First name": "Inge",
      "Last name": "Castelin",
      "Birthday": 19958400000,
      "Email": "icastelin4@netscape.com",
      "Address": "0 La Follette Hill",
      "ZIP": "44143",
      "City": "Dortmund",
      "Country": "Germany"
    }
  },
  {
    "id": 7,
    "fields": {
      "First name": "Rubin",
      "Last name": "Sawell",
      "Birthday": 772502400000,
      "Email": "rsawell5@spiegel.de",
      "Address": "8 Chive Center",
      "ZIP": "44795",
      "City": "Bochum",
      "Country": "Germany"
    }
  },
  {
    "id": 8,
    "fields": {
      "First name": "Susie",
      "Last name": "Tompkiss",
      "Birthday": 530668800000,
      "Email": "stompkiss6@digg.com",
      "Address": "8685 Schmedeman Alley",
      "ZIP": "47732",
      "City": "Evansville",
      "Country": "United States"
    }
  },
  {
    "id": 9,
    "fields": {
      "First name": "Ashil",
      "Last name": "Noraway",
      "Birthday": 417139200000,
      "Email": "anoraway7@xing.com",
      "Address": "9570 Oxford Plaza",
      "ZIP": "94273",
      "City": "Sacramento",
      "Country": "United States"
    }
  },
  {
    "id": 10,
    "fields": {
      "First name": "Gerek",
      "Last name": "Hugk",
      "Birthday": 598406400000,
      "Email": "ghugk8@cargocollective.com",
      "Address": "50571 Kropf Park",
      "ZIP": "93762",
      "City": "Fresno",
      "Country": "United States"
    }
  },
  {
    "id": 11,
    "fields": {
      "First name": "Nick",
      "Last name": "Sink",
      "Birthday": 704419200000,
      "Email": "nsink9@twitter.com",
      "Address": "98637 Westerfield Point",
      "ZIP": "80241",
      "City": "Denver",
      "Country": "United States"
    }
  }
]

To create these records in Ninox, you might traditionally use:

for i in response.result do
	let new := (create Contacts);
	new.(
		'First name' := i.fields.'First name';
		'Last name' := i.fields.'Last name';
		Birthday := i.fields.Birthday;
		Address := i.fields.Address;
		'Email' := i.fields.'Email';
		ZIP := i.fields.ZIP;
		City := i.fields.City;
		Country := i.fields.Country
	)
end

The traditional method requires you to specify each field individually. As the number of fields increases, your script grows and becomes more complex. With set, your script stays concise and manageable, regardless of how many fields you have.

Dynamic field updates with set

The set function simplifies this process by allowing you to iterate over the fields dynamically. Here’s how you can use it:

for i in response do
	let new := (create Contacts);
	for key, value in i.fields do
		set(new, key, value)
	end
end

See also

get which returns a value of a given field.

Last updated