Pull in Specific UUID to New Draft to Mail


#1

Trying to put together a simple script that pulls an existing draft UUID and then sends it out to mail. This draft UUID would be pulled into a blank draft to run.

var contact = Draft.find('LONG-UUID')
var d = draft
d.content = d.content + contact

This [[draft]] then gets sent to an email. Unfortunately it shows up in the email as [object ActionKit.ScriptObjectDraft]. Anyone have a fix for me on this one?


#2

I’m not sure I understand exactly what you are doing here…do you want the contents of the contact to be sent in the email?

Currently you are assigning a draft object to your contact variable. You don’t retrieve any properties from it, so it can’t return anything other than an object, which of course you can’t email directly.

If you want the contents of contact, use contact.content instead. Based on the code you posted, it would append the contents of contact to the end of the current draft the script is called from.

const 
    contact = Draft.find('[[UUID]]'),
    d = draft;

d.content += contact.content;
d.update();

If you just want to send a draft as an email, you don’t need to pull it into a blank draft. You could just do something like this (adapted from the script reference:

(() => {
    const 
        contact = Draft.find('[[UUID]]'),
        mail = Object.assign(Mail.create(), {
          subject: contact.content,
          body: contact.title
        }),
        success = mail.send();
    if (!success) {
      console.log(mail.status);
      context.fail();
    }
})()

Nick


#3

This helped - I forget that I need to retrieve something from the draft object - using this worked.

To answer what I was trying to do - basically I have two draft notes with contact details - one for work and one for personal. There is an action I have that pulls up two buttons and then populates the email with the right draft.

Next step will be to try and figure out how to use the mail object - ideally I would enter an email address into the draft - run the Action to select work or personal - the email address in the draft goes into the to: portion and then the email gets sent from the correct work or personal email address. That is the next thing to try here…

Thanks for the help.


#4

You would have to pick the “from” account manually when the email pops up to send, but the rest of what you are looking for can be done easily. For example, if you had one email address per line of the draft, you could parse that into an array and assign it to mail.toRecipients. That’s all there is to it!


#5

Thanks for all the help - will post the script I used here in case it is helpful to anyone else. Probably opens up my scripting skills for comments as well - tried to make this relatively short, but feel like I might be a little sloppy with the if statements. Would also be great if there was a way to have Drafts make the choice of email account to send from as a variable.

// Send Contact Details - version 2
// Put email address in draft, ask for category, then prepare email
// Updated: July 12, 2018

// reference drafts of contact details
var workcontact = Draft.find('UUID-W')
var personalcontact = Draft.find('UUID-P')

// prompt to select a category
var p = Prompt.create();
p.title = "Select contact details";
p.addButton("Work");
p.addButton("Personal");

if (p.show()) { // user made a selection

   if (p.buttonPressed === "Work") {
       var contactchoice = workcontact;
   } else if (p.buttonPressed === "Personal") {
       var contactchoice = personalcontact;   
   } 
   
   var mail = Mail.create();
       mail.toRecipients = [draft.content];
       mail.subject = "Contact Details";
       mail.body = contactchoice.content;
       var success = mail.send();
       if (!success) {
           console.log(mail.status);
           context.fail();
           }
   
   else {
   }

} else {
	context.cancel();
}