Generate unique IDs for new objects

When writing an Ontology edit Function that creates objects, you may want to generate a unique ID for the newly created object. You can set this up in Functions by using the @foundry/functions-utils package to generate a globally unique identifier.

Import the package

The @foundry/functions-utils package is installed by default, but if the package is not present in the package.json file:

  • In the "dependencies" section, add "@foundry/functions-utils": "0.1.0"

As mentioned in the documentation on adding dependencies, remember to restart Code Assist to have the new package available for autocomplete.

Use the package in code

To generate a unique ID, you can use the Uuid.random() utility function from the @foundry/functions-utils package. The below code example shows how you could use the random function in an example Ontology edit Function.

Copied!
1 2 3 4 5 6 7 8 9 10 11 12 13 import { OntologyEditFunction, Timestamp } from "@foundry/functions-api"; import { Objects } from "@foundry/ontology-api"; import { Uuid } from "@foundry/functions-utils"; export class ExampleEditFunctions { @Edits(FlightScenario) @OntologyEditFunction() public createFlightScenario(): void { const scenario = Objects.create().flightScenarios(Uuid.random()); scenario.scenarioName = "New scenario"; scenario.creationTime = Timestamp.now(); } }