Create new objects from another object’s properties

If I have a ‘Program’ object with a struc array property “Projects” where each entry of the struc array has a ‘project title’ ‘start date’ ‘end date’; would a custom function enable the creation of an action that would create one “Project” object for each entry in the “projects” struc array with a single button click?

I’ve tried taking a TSv1 function to “create project” and have it loop but I can’t get it to work with a Struc array - which appears to be a matter of platform capability. I’m not as familiar with TSv2 or Python.

There are a few options which you could probably consider.

  1. Pipeline Builder
    1. Form a materialized dataset from your object type
    2. Create a pipeline that processes the data, extracting the struct, etc., through to the desired output object dataset, then use this to either back an object type or be one directly.
    3. This approach has some downsides, which you should explore independently. For example, reduced result object type flexibility, batch pipelines not being instant, etc.
  2. TypeScriptV2 / Python functions
    1. These have native support for structs and are reasonably intuitive if familiar with the way that the Ontology works. If not, then they are a great way to learn!
    2. Simply write a function that performs the appropriate handling and then bind the Edit function to an action.
    3. If you want this to happen automatically, include some idempotence logic and bind the action to an Automate resource.
    4. I have put a code sample below to demonstrate.
  3. Consider whether you even need a nested struct type // use an automap
    1. Why do you even need a nested fixed-schema struct? Might be dodgy in the long-term unless there is genuinely no other good way of doing it.
    2. Have you considered making them as separate object types then using a Link type or similar instead of this very clunky way of doing things.
    3. Maybe an automap is appropriate, though I don’t know your scenario and so not sure.
import { MyObject, MyObject2 } from "@ontology/sdk";
import { Client, Osdk } from "@osdk/client";
import { createEditBatch, Edits, Integer } from "@osdk/functions";

type OntologyEdit = Edits.Object<MyObject>;

export default function extractStructType(
    client: Client,
    objects: Osdk.Instance<Employee>[]
): OntologyEdit[] {
    const batch = createEditBatch<OntologyEdit>(client);

    objects.foreach((obj) => {
        batch.create(MyObject2, {
            // MAPPED PROPERTIES HERE
        });
    })
    

    // Do whatever else you want here

    return batch.getEdits();
}
1 Like

Thanks! I will give learning how to work with the TS2 and python, sounds like a good little project.