Autogenerating frontmatter field from two other fields?

What I’m trying to do

I have a Templater template that is set to run on note creation in a certain folder. That template also runs a Modal Forms form to collect some additional frontmatter fields. That part all works perfectly. However, I’d also like a script that auto-runs that will take two of the fields created by Modal Forms (class_type_id and class_start_date) and combine them into a class_instance_id field (e.g. “class-2025-12-08”). Here’s the template:

type: class_instance
class_instance_id: “”
<%*
const modalForm = app.plugins.plugins.modalforms.api;
const result = await modalForm.openForm(“_new_class_instance”);
tR += result.asFrontmatterString();
-%>
materials_version: “”
students:
notes:

and after creation it looks like this:

type: class_instance
class_instance_id: “”
class_type_id: chest
class_name: Class Name
class_start_date: 2025-12-08
class_location: Class Location
class_city: City
class_state: State
ce_hours: “16”
price: 123
materials_version: “”
students:
notes:

Things I have tried

I have tried scripts like this:
<%*
// Populates ‘instance_id’ from ‘class_type’ and ‘start_date’
tp.hooks.on_all_templates_executed(async () => {
const file = tp.file.find_tfile(tp.file.path(true));
await app.fileManager.processFrontMatter(file, (frontmatter) => {
// instance_id composed from other fields
const type = frontmatter[“class_type_id”];
const date = frontmatter[“start_date”];
if (type && date) {
frontmatter[“instance_id”] = ${type}_${date};
}
});
});
%>
and numerous variations.

Currently you are writing all your modal form in the frontmatter (tR += result.asFrontmatterString()). You need to get the different entries separately (obsidian-modal-form/docs/managing-results.md at master · danielo515/obsidian-modal-form · GitHub) and then combine the values you want to combine.

1 Like

Thank you so much!!! I got it working!

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