Sending Record by Email (Updated)

(Updated April 3, 0223/bug fixes and linked form support added)

This is a simple form script for emailing a TapForms record without using the print feature.

Here is how a record will look like in the email:

Title: Fall Colors #4
Folio: Fall Colors
Bar Code: 2022
Print Size: 16x20
Medium: Panel Mounted Print with Floater Frame
Medium Detail: Epson Ultra Premium Photo Paper Luster
Framed: true
Year: 2021
Capture Date: Oct 21, 2028

Only primitive data field types such as Text, Number, Date, Time etc are supported. Complex data fields such as Photo or File attachments, or tables are ignored! Linked-form fields are followed and their content added as well. For contact fields, only the name is printed. Empty fields are discarded.

To use this, create a form script and copy following code in it. When executing it, the script will format the fields as described and then call the mailtool with the body of the message prefilled.

/*
   copyRecordToText(currentRecord)
   This function copies all fields that represent a primitive data
   type from the currentRecord and arranges them as a formatted
   text. There is currently no beautification done! Object data 
   fields such a photo or file attachments, or tables as well as 
   empty fields are ignored. Linked-fields are followed.
   Returns the formatted text.
*/
function copyRecordToText(currentRecord, level=0){
   let currentForm = currentRecord.form;
   let rec = [];
   let fields = currentForm.getFields();
   let intend = " ".repeat(level*3);
   for (f of fields){
      let value = currentRecord.getFieldValue(f.getId());
      if (value) {
	      if (typeof(value) != "object"){
	      	rec.push(intend + f.name + ": " + value);
	      } else if (f.fieldType == "contact") {
	      	rec.push(intend + f.name + ": " + value.name);
	      	//rec.push(f.name + ": " + JSON.stringify(value));
	      } else if (f.fieldType == "date") {
	      	let d = new Date(value)
	      	rec.push(intend + f.name + ": " + d.toDateString());	      
	      } else if (f.fieldType == "time") {
	      	let d = new Date(value)
	      	rec.push(intend + f.name + ": " + d.toTimeString());
	      } else if (f.fieldType == "date_time") {
	      	let d = new Date(value)
	      	rec.push(intend + f.name + ": " + d);	      
	      } else if (f.fieldType == "form") {
	      	if (value.length > 0){
		    rec.push(intend + f.name + ": ");
		    for (v of value){
		       rec.push(intend + copyRecordToText(v, level+1));
		    }
	      	}
	      } else {
	           console.log("Unsupported field type" + f.name + "(" + f.fieldType + ")");
	      }
	   }
   }
   rec = rec.join("\n");
   console.log("============= Record =============");
   console.log(rec);
   console.log("============End Record ===========");
   return rec;
}
 
/*
   createEmailURL(to, cc, bcc, subject, body)
   Creates a text string with the given arguments. This then later
   can be used when calling Utils.openUrl(emailUrl);
   Returns URL text string.
*/
function createEmailURL(to, cc, bcc, subject, body) {
   let argsList = [];
	
   (cc) ? argsList.push("cc=" + cc): null;
   (bcc) ? argsList.push("bcc=" + bcc): null;
   (subject) ? argsList.push("subject=" + encodeURIComponent(subject)): null;
   (body) ? argsList.push("body=" + encodeURIComponent(body)): null;
   let url = "mailto:" + to + "?" + argsList.join("&");
	
   console.log("emailUrl: " + url);
   return url;
}
 
 
function Mail_Record() {
   let emailBody = copyRecordToText(record);
   let emailUrl = createEmailURL("", "", "", "Email Record", emailBody);
 
   // Send email
   Utils.openUrl(emailUrl);
}
 
Mail_Record();

Instead of emailing a record, the data could be copied to the clipboard as well and then used in another application:

function Copy_Record() {
   let emailBody = copyRecordToText(record);

   // Copy email
   Utils.copyToClipboard(emailBody);
}

Copy_Record();

Note: This code includes data from forms that are linked to. But if data needs to be included from a form that is linked form, then line 32 needs to be changed from

} else if (f.fieldType == "form") {

to

} else if (f.fieldType == "from-form") {

Did you like this post? Did you use the given code? Please consider supporting me by buying me a coffee!

Buy Me A Coffee


Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *

Web Design Consulting

Do you need help designing your web site or getting Backlight working the way you want? Contact me to discuss your idea or project.


Buy me a Coffee

If you like what I share here, please consider buying me a coffee or a print. This helps keeping me motivated to continue posting. Thank you!

Buy Me A Coffee

Categories