Using Markdown for contracts with referenced fields

Tap Forms – Organizer Database App for Mac, iPhone, and iPad Forums Using Tap Forms Using Markdown for contracts with referenced fields

Viewing 6 reply threads
  • Author
    Posts
  • May 27, 2021 at 1:49 AM #44465

    Juan Borrás
    Participant

    Hi all!!

    I’m trying to figure out how to use the Markdown as a template for contracts.

    Is there anyway to search and substitute special “texts” in the markdown document via the script.

    For example, using %customer_name% and stuff like that?

    Thanks!

    May 27, 2021 at 2:26 AM #44469

    Brendan
    Keymaster

    Hi Juan,

    There’s no built-in function for doing substitutions like that outside of a static text object on the Mac version using a custom layout.

    With a static text object on a custom layout, you can do things like this:

    Dear [First Name],

    Your account is now overdue by $[Amount Due]

    Sincerely,

    The Management

    But it’s not using the Markdown editor. It’s just using the regular rich text editor.

    And you will see the substitution results only at print time.

    If you were to use a script running against the Markdown field, you’d just end up replacing the values. Although I suppose if you set the Default Value on your Markdown field to your template, whenever you added a new record, you’d get the untouched template value which you could then run a script on to do the substitutions.

    Thanks,

    Brendan

    May 28, 2021 at 11:20 PM #44485

    Sam Moffatt
    Participant

    A path forward might be using a script field and Javascript’s template strings as the template and set the Markdown field.

    You can use the script field to get all of the fields you’re interested in. Then you can use the template strings to compose the Markdown content and set it to a Markdown field. As the fields you reference in the script field are changed, the Markdown field will be updated automatically based on the template in the script field.

    Here’s a quick sample from a movie library (maybe the TF sample one?) which has a few extra fields in it. This shows pulling in a number of fields, handling a table field to gather records (actors) and doing some simple composition. It uses template strings to compose the individual lines with the actor in it and the final Markdown string. The last few lines set the field value of the Markdown field I created, tells Tap Forms to save all changes (very important) and returns the raw Markdown so that I can debug in the script editor.

    
    function Md_Generator() {
    	var movie_title = record.getFieldValue('fld-987c9aac738a4f0fa1d18395902b3fb1');
    	var barcode = record.getFieldValue('fld-7e3ead95de51427fa5a467b2476d58cd');
    	var summary = record.getFieldValue('fld-28cc8f7f082c43818f1169c96c96e5a9');
    
    	var photo_count = record.getFieldValue('fld-55340bfd611b411b87da481ae23e8db9');
    	var actors_total_cost = record.getFieldValue('fld-1837d1a6be83424a90e0e7ebdd6265e3');
    	var imdb_website = record.getFieldValue('fld-9bc90613e8db45fea77579f35eb53b9d');
    
    	var actorLines = [];
    	var actors = record.getFieldValue('fld-984cf79ccafb45a381b0f95b0aa28e78');
    	for(var actor of actors) {		
    		var first_name = actor.getFieldValue('fld-92b3b496a6cb4c20a32d36aa61e7a04f');
    		var last_name = actor.getFieldValue('fld-a907066570844290a27940d34de98b4f');
    		var fee = actor.getFieldValue('fld-b1ea1f22da5f421c93e1e20520e3ea22');
    		
    		actorLines.push(`- ${first_name} ${last_name} (Fee: $${fee})`);
    	}
    	
    	var actorString = "No actors listed."
    	if (actorLines.length > 0) {
    		actorString = actorLines.join("\n");
    	}
    
    	var markdown = `
    # ${movie_title}
    
    Barcode: ${barcode}  
    Photos: ${photo_count}  
    Actor Cost: ${actors_total_cost}
    
    ## Actors 
    ${actorString}
    
    ## Summary
    ${summary}
    
    ## Links
    - [IMDB Link](${imdb_website})
    `;
    
    	record.setFieldValue('fld-d0a67ce5cf1a4ab8b0a3e2357cc18d45', markdown);
    	document.saveAllChanges();
    	return markdown;
    }
    
    Md_Generator();
    
    May 29, 2021 at 12:15 AM #44486

    Juan Borrás
    Participant

    Thanks! will try!

    May 31, 2021 at 1:58 AM #44495

    Juan Borrás
    Participant

    It works!

    Now I want to “inject” the formed Markdown with the substituted values into a Markdown field so I can see the formatted document in order to print it.

    I set up a new layout with the empty markdown field and a button. When the button is pressed I set the record.setFieldValue for such field with the substituted markdown, but I can’t see it in the layout.. should I do some refresh or something?

    Thanks!

    May 31, 2021 at 3:06 AM #44497

    Juan Borrás
    Participant

    Found!

    have to do document.saveAllChanges()

    May 31, 2021 at 10:47 AM #44499

    Sam Moffatt
    Participant

    Glad to hear you got it sorted. The saveAllChanges() is important for ensuring that everything gets saved. Hopefully that got you to where you needed to be.

Viewing 6 reply threads

You must be logged in to reply to this topic.