Button that creates a new note with name and other fields set by prompts/user input

What I’m trying to do

I’m trying to add a button to my note templates for daily, weekly, and customer notes which will create a file in a given directory with the appropriate template and feed it values so certain things in the file start out correctly populated. For context, the created file will be used while working on a customer case.

From my weekly and daily notes, I would like the button to prompt for the customer name, ID number, and description.
From the customer’s page, the template for the customer page should ask me for the customer name when I create that page, and set up the button so that it doesn’t ask for Customer Name when it is used from there.
When the Customer page is created, it should rename the page with a string composed of the customer name, ID, and description concatenated and cammelCased, and populate other fields through the new note.)

I’ve gone through tickets here and threads on Reddit but haven’t found an example that helped me, so if you know of one, please lmk. So far, everything I’ve worked through has just gotten me deeper down the hole, so now I’m running into concurrency errors.

Things I have tried

I’ve been playing with the Meta Bind and Buttons plugins but my templates fail.

I’m hesitant to share what I’ve done so far because I want to hear about other ideas, not poisoned by mine. Still, jic I’m on the right track, or someone knows what stupid simple thing I did wrong:
The route I’m going with atm is to have the button create the file and leave all the actual work to Templater. Here’s the block of Templater code that’s getting concurrency issues.
This does not work but my focus is just getting the data flowing correctly, then I can work on things like the camel case function etc.

Templater code
<%-*
    let fileName = tp.file.title, description = "", titleArray = [], customer, ticketID;
    
//    if (fileName.startsWith("Untitled") ) {
    customer = await tp.system.prompt("Customer: ");
    customerHyph = customer.replace(/ /g, "-");
    customerNoSpace = customer.replace(/ /g, '');
  
    ticketID = await tp.system.prompt("Ticket ID: ");
        
    description = await tp.system.prompt("Issue Description: ");
    descriptionNoSpace = description.replace(/ /g, '');\

    await filename = "Case/" + tp.user.camelCase(customer) + "-" + ticketID + "-" + tp.user.camelCase(description)
//    await tp.user.convertToCamelCase(filename)
    await tp.file.move(filename)
-%>
JS Function
async function camelCase(str) {
// Split the input string into an array of words
const words = str.split(' ');
  // Check if there's more than one word
if (words.length > 1) {
// Convert the first word to lowercase
words[0] = words[0].toLowerCase();
}
  // Convert each word to camelCase
return words.map((word, index) =>
// For words other than the first one, capitalize the first letter
index > 0 ? word.charAt(0).toUpperCase() + word.slice(1) :
// For the first word, keep it lowercase
word
).join('');
}
 module.exports = camelCase;

First, you only need to use sync/await when you are calling some method that is going to have to wait for user input or some kind of file operation or something where the method waits for something. Your camelCase() code is simple javascript, so you can get rid of the sync in the JS code and you don’t need await when you are calling it. I don’t think that’s the concurrency issue, but I’m not a javascript expert either.

Second, I’m not sure I understand fully what you want. Are you just creating a note with the customer/id/description as the name with no content? I don’t see where in your templater file you actually create the note. Neither Meta Bind nor Buttons (I think Buttons is deprecated) creates a file. You’ll need tp.file.create_new(…) somewhere to actually create the file.

Third, if you want the template to act differently when called from your daily note as opposed to the customer note, you’ll probably need something like a flag in the frontmatter that says you are in a customer note and don’t need to ask for the customer name. You may find it easier just to have two templates, one that runs from the daily note and one that runs in the customer note.

I would look at QuickAdd. You can call QuickAdd from Meta Bind buttons. You can specify templates for QuickAdd to use when it creates a file. QuickAdd would take care of a lot of the bookkeeping (like checking for a file’s existence). Additionally, QuickAdd has a capture command that would allow you insert customer stuff into a pre-existing customer note.

Hope this helps

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