Hi there, I’m creating a very simple function that updates the “communication group” of a “communication template”.
import { Edits, OntologyEditFunction } from "@foundry/functions-api";
import { CommunicationsGroup, CommunicationTemplate } from "@foundry/ontology-api";
export class CommunicationsFunctions {
@Edits(CommunicationTemplate)
@OntologyEditFunction()
setTemplateGroup(template: CommunicationTemplate, group: CommunicationsGroup): void {
template.group.set(group);
}
}
The function (executed from an Action or even from the Live Preview) throws the error “Primary key change not supported”
The objects have been imported
Also the links seems to be corrected:
Here is the full Error
Primary key change not supported.
Error Parameters: {
"objectLocator": "[rid: ri.phonograph2-objects.main.object.c621783f-11cf-4420-971a-95ad643d6e8a, type: a8gtdv3f.communication-template, primaryKey: {\"id\":\"1ce7afcf-ca4c-435d-8746-55ca49fb951d\"}]"
}
SafeError: Primary key change not supported
at t [as constructor] (FunctionsIsolateRuntimePackage:2:1306616)
at new t (FunctionsIsolateRuntimePackage:2:531913)
at Object.t.primaryKeyChangeIsNotSupported (FunctionsIsolateRuntimePackage:2:1188551)
at t.ObjectPropertyStore.set (FunctionsIsolateRuntimePackage:2:1299995)
at t.TypeConvertingPropertyStore.set (FunctionsIsolateRuntimePackage:2:1166708)
at t.DefaultObjectStorageProvider.setPropertyValue (FunctionsIsolateRuntimePackage:2:1181095)
at t.DefaultObjectStorageProvider.addSingleKeyLink (FunctionsIsolateRuntimePackage:2:1184377)
at t.DefaultObjectStorageProvider.setSingleKeyLink (FunctionsIsolateRuntimePackage:2:1184107)
at Object.set (FunctionsIsolateRuntimePackage:2:1183226)
at Object.set (FunctionsIsolateRuntimePackage:2:1108821)
Also a link for a tutorial I used to see if something was off.
I can’t spot any error, if someone could point out where the mistake is I would really appreciate it
You would need to validate what is the API of the relation between your two object. You can check that in Ontology Manager to see the name of it.
My assumptions:
- Maybe you have a misconfigured ontology (are you using a primary key as a key for the relation as well ? Shouldn’t be possible I believe)
- Your function by doing the “.set()” is actually editing a primary key (strange, because set should be for relations ? so I want to validate that the .set() is the correct , aka the API of the relation you are trying to edit)
Hi VincentF, thank you for the quick response.
Is this a representation of the API?
If so, I’m actually using both group and template ids as a reference for the link. But they are the only unique properties among those objects
Should I create a new property that is unique but not set as primary key for the link?
Hey,
(Piggy-backing off of @VincentF’s reply)
The screenshot you included is correct as that’s the Overview page for the relevant Link Type. This page’s configuration essentially maps the primary key property of one Object Type (i.e., Communications Group on the right), with a foreign key property of another Object Type (i.e., Communications Template on the left).
If the Id property of your Communications Template Object Type contains the primary key values of your Communications Group Object Type, then you’re all set here and can just make a quick update to your code. Otherwise, you may need to designate another/new property that you’ll pass into that dropdown menu.
Finally, when writing an OntologyEditFunction(), you can use the standard assignment operator or equals sign (=) instead of the .set() method when you want to update the value stored inside of an object’s property. You’ll also want to grab the actual primary key value of the Group object you’re passing in via the same dot notation.
Your final code should look like this:
import { Edits, OntologyEditFunction } from "@foundry/functions-api";
import { CommunicationsGroup, CommunicationTemplate } from "@foundry/ontology-api";
export class CommunicationsFunctions {
@Edits(CommunicationTemplate)
@OntologyEditFunction()
setTemplateGroup(template: CommunicationTemplate, group: CommunicationsGroup): void {
// This assumes your CommunicationTemplate Object Type has a foreign key property called "Group Id"
// which is also used in the relevant Link Type.
// Replace the value after `template.` with the API name of the correct property
template.groupId = group.id;
}
}
1 Like
Oh, I see now, I was under the impression that there was an instance of Link for each pair of communication ↔ group, (like the table used by the many-to-many links).
But the one-to-one or many-to-one links are just a method on communication that will look for the instance of Group that has the “id” == “communication.groupId”. Sort of, right?
Thank you very much joshOntologize. All set and running
1 Like