Script behavior - new draft overrides earlier append

I am trying to append text to the active draft, then create a new draft. The portion of the script that appends works fine on its own, but when I add the subsequent lines to the script creating the new draft, the append action is not executed.

This is the problem portion of the script (variables today and subject are previously defined in the script):

// append new draft link

let mn = (today + " " + subject);
draft.append ("[[" + mn + “]]”);

// create new draft

mnd = new Draft();
mnd.isArchived = true;
mnd.content = (mn + “\n” + “\n”);
mnd.addTag(“meeting_notes”);
editor.load(mnd)

When run, the new draft is created, but the append command does not run. If I delete everything after “// create new draft” the append command runs perfectly.

It looks like you are applying changes, but not updating the drafts, and you missed out the fact that mnd is being defined, not set.

This works for me:

// set up missing variables for testing
let subject = "some subject";
let today = draft.processTemplate("[[date|yyy-mm-dd]]");

// append new draft link
let mn = (today + " " + subject);
draft.append ("[[" + mn + "]]");
draft.update();

// create new draft
let mnd = new Draft();
mnd.isArchived = true;
mnd.content = (mn + "\n" + "\n");
mnd.addTag("meeting_notes");
mnd.update();
editor.load(mnd);

Some additional points:

  1. Do try and provide a script that people can test. Noting that code is missing simply leaves a gap and the issue, or an additional issue, could be in that missing code. It does not have to be your final code necessarily, but it should be code that faithfully reproduces your issue. That’s why I’ve included the set up code in the above - so you can test it in exactly the way I have, and then (with any luck), any other issues you might encounter, you know will exist outside of that.
  2. Some of the double quotes in your code as pasted in the forum are smart quotes, ("these quotes work" vs “these quotes error”) - do make sure your posted code quotes are correct. Sometimes the forum software can be overzealous, but as long as you put your triple back tick code block in first, it should faithfully reproduce the code you copy from a Drafts action step.
  3. While your new draft code ran perfectly, you may note I have also included an update call for that draft too. In most(?) circumstances, there is an implicit draft update at the end of an action, which would be why it worked without it. My preference is to explicitly include them every time as it ensures it will work if I reuse or extend the existing code at any point, potentially saving future me a few headaches.

Hope that helps.

2 Likes

Thank you, it was the updating that was the issue. This aspect of my code now works, and thanks for the other tips. Stay tuned for possible future questions!