2 function backed options to update title, one works, one doesn't

Howdy all,

I have 2 function backed actions
Each one is supposed to do the same thing:

  • take existing property1 (where source = user edits)
  • take existing property2 (where source = user edits)
  • Identify existing property3 (where source = user edits)
  • Set property3 = concat (property1, property2)

Function backed action 1 works

import { OntologyEditFunction } from "@foundry/functions-api";
import { BilletAssignment, Awardv1Event, EventAssignment, Objects } from "@foundry/ontology-api";

export class MyFunctions {
    @OntologyEditFunction()
    public async updateEventAssignmentTitle(EventAssignment: EventAssignment): Promise<void> { // updated 
        if (!EventAssignment.billetAssignmentId || !EventAssignment.eventId) {
            throw new Error("EventAssignment is missing required fields");
        }
        // Get the billet assignment and event objects using objects.search
        const billet_assignment = await Objects.search().billetAssignment().filter(m => m.assignmentId.exactMatch(EventAssignment.billetAssignmentId!)).all()[0];
        const event = await Objects.search().awardv1Event().filter(b => b.eventId.exactMatch(EventAssignment.eventId!)).all()[0];

        if (!billet_assignment || !event) {
            throw new Error("Billet assignment or event not found");
        }

        // Create new title by concatenating lastName and billetName
        let newTitle = `${billet_assignment.maybeABetterTitle_} - ${event.eventName}`;
        
        // Update the betterTitle_ property
        EventAssignment.betterTitle_ = newTitle; // updated 
    }
}

function backed action 2 does not compile

import { OntologyEditFunction } from "@foundry/functions-api";
import { CollateralDutyAssignment, CollateralDuty, BilletAssignment, Objects } from "@foundry/ontology-api";

export class MyFunctions {
    @OntologyEditFunction()
    public async updateCollateralDutyAssignmentTitle(collateralDutyAssignment: CollateralDutyAssignment): Promise<void> { // updated 
        if (!CollateralDutyAssignment.idCollateralDuty || !CollateralDutyAssignment.idBilletAssignment) {
            throw new Error("CollateralDutyAssignment is missing required fields");
        }
        // Get the CollateralDuty and BilletAssignment objects using Objects.search()
        const collateral_duty = await Objects
            .search()
            .collateralDuty()
            .filter(d => d.collateralDutyId.exactMatch(collateralDutyAssignment.idCollateralDuty!))
            .all()[0];

        const billet_assignment = await Objects
            .search()
            .billetAssignment()
            .filter(b => b.assignmentId.exactMatch(collateralDutyAssignment.idBilletAssignment!))
            .all()[0];

        if (!collateral_duty || !billet_assignment) {
            throw new Error("Collateral Duty or Billet Assignment not found");
        }

        // Create new title by concatenating ___ and ____
        let newTitle = `${collateral_duty.dutyName} - ${billet_assignment.maybeABetterTitle_}`;
        
        // Update the betterTitle_ property
        CollateralDutyAssignment.properties.futureBetterTitle = newTitle;
    }
}

errors are as follows

I am sure I am missing something basic?
My best bad guess is that I recently created the main object type CollateralDutyAssignment (so maybe the properties need to “permeate” through the ontology?)
I triple checked my property API names.

thanks for any help!

Hi there @9aebee43292b35f5ac9a!

Two things to try, but can’t say for sure without further context:

Here:

the Object Type has a capital letter as a first letter, but the variable doesn’t.

but you’re then running the check on the object type, not in the object itself:

For the third and last error in your screenshot, I think you need to remove the .properties. in (the assignment should work by just calling collateralDutyAssignment.futureBetterTitle = newTitle;):

Hope this helps!

Best,

1 Like

Gorgeous

Thank you!
those were the edits I needed to get it working and it turned out like this:

import { OntologyEditFunction } from "@foundry/functions-api";
import { CollateralDutyAssignment, CollateralDuty, BilletAssignment, Objects } from "@foundry/ontology-api";

export class MyFunctions {
    @OntologyEditFunction()
    public async updateCollateralDutyAssignmentTitle(CollateralDutyAssignment: CollateralDutyAssignment): Promise<void> { // updated 
        if (!CollateralDutyAssignment.idCollateralDuty || !CollateralDutyAssignment.idBilletAssignment) {
            throw new Error("CollateralDutyAssignment is missing required fields");
        }
        // Get the CollateralDuty and BilletAssignment objects using Objects.search()
        const collateral_duty = await Objects
            .search()
            .collateralDuty()
            .filter(d => d.collateralDutyId.exactMatch(CollateralDutyAssignment.idCollateralDuty!))
            .all()[0];

        const billet_assignment = await Objects
            .search()
            .billetAssignment()
            .filter(b => b.assignmentId.exactMatch(CollateralDutyAssignment.idBilletAssignment!))
            .all()[0];

        if (!collateral_duty || !billet_assignment) {
            throw new Error("Collateral Duty or Billet Assignment not found");
        }

        // Create new title by concatenating ___ and ____
        let newTitle = `${collateral_duty.dutyName} - ${billet_assignment.maybeABetterTitle_}`;
        
        // Update the betterTitle_ property
        CollateralDutyAssignment.futureBetterTitle = newTitle;
    }
}

in general I find myself wanting to create new titles for objects a lot.

in this case I have relevant 4 objects:

  • billet

    • “ground truth” list of billets for an organization
    • example: Office manager, Office secretary
  • billet assignment

    • after ~1 year there is a new person that fills the billet assignment
    • example: Bob is the office manager
  • collateral duty

    • “ground truth” list of collateral duties for an organization
    • example: party planner, coffee grabber, guy who shreds paper
  • collateral duty assignment

    • after ~2 years there is a new person that fills the collateral duty
    • example: Bob the office manager (via billet assignment) is in charge of planning parties (specific collateral duty via foreign key)

Is this the best approach or am I missing something?

thanks again!

1 Like

Glad you got it up and running!

As for your question and use case, any general recommendations probably won’t apply without understanding the underpinnings of the specific data context, but one alternative to changing titles that comes to mind is in the case you need to track down who was the person filling in that role, say, 3 years ago.

For that, you would probably need to create a new object each time a role is filled in by a different employee, and have some properties associated with each record (e.g., timestamps such as EffectiveFrom, EffectiveTo to understand the timeframe each person filled in the position) and potentially a simple flag that lets you filter the current records in workshop and such, like a boolean “IsCurrent” flag.

Hope these help!

If no outstanding issues persist, feel free to close this topic. Thanks!

Best,

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.