If you’re looking for an easy way to build an online database application without the need for programming, you need to check out Knack.  With Knack you can get rid of those clunky spreadsheets and build easy to use web applications such as a custom CRM, proposal tracker, order management, and more.  Knack allows you to build some pretty powerful apps and we’re going to show you how you can easily generate documents directly from Knack using WebMerge.

In this example, we’re going to show you how to generate a contract and send it to our customer directly from a Knack CRM application.  First, we’ll setup our contract template in WebMerge, then we’ll add a custom button to our Knack application that will send our client data to WebMerge to generate the contract.

For our contract, we’re going to use a Word document as the document type.  In the document we are going to add all of the normal contract terms, and then we’ll add the WebMerge merge fields (ie {$company_name}, {$address}, etc) where we want the Knack data to go.  Here’s what our contract template looks like:

After we upload our document to WebMerge, we need to make sure we update the email delivery settings so that the contract is emailed to our client.  You can make these changes under the Deliver tab.  You’ll want to change the “To” address to the merge field for the email address.  If you don’t have an email address merge field setup yet, simply click the <<Other>> option and type in {$email}.  This will add “email” as a merge field.

On the Merge tab inside WebMerge, you’ll notice there is a “Merge URL” for your document.  This is the unique URL that you’ll use when sending data to WebMerge to populate your contract.  Save this for later.

Next, let’s jump into Knack and setup the custom button that will be used to generate a contract for a specific client.  The setup process is pretty straight forward, but it is a little technical.  We’ve tried to include as many examples as we could to help you, but Knack has a great support team that is more than happy to help with this custom coding.

We’re going to use Javascript to customize our Knack application to show a “Create Contract” button on our Client Detail page.  To edit the Javascript for our application, we’re going to go to the API & Code page in our application settings.  Once there, click the Javascript tab and you should see a code editor (text box).  This is where we are going to use jQuery to manipulate our page.

We’re going to use this code snippet as a starting point for our application.  There are a few things that you need to figure out when setting up the javascript:

  1. Which “Scene” is your page in
  2. Which “View” in that scene is your page
  3. What the Field IDs are that you need to send to WebMerge

The easiest way to find this information is to “inspect” the HTML in your browser (either using the built-in development tools or using a plugin like Firebug).  You will be looking for elements in the HTML like <div id=”kn-scene_3”> and <div id=”view_4”>.   These IDs will be important in our javascript.

When a new scene loads, an event is fired and we can capture that with jQuery.  You’ll notice the first line in our code example is looking for “$(document).on('knack-scene-render.scene_3', …”  - This event will fire when our scene is rendered.  This is when we want to add the button to our view page.

To add the button to our view page, we’re going to use jQuery to create a new div container that will contain our button.  Then we will attach an event handler on the button that will use AJAX to send the data to our WebMerge contract.  Here’s what our code looks like:

As you’ll see in the code above, we add the link to our page, set an event handler that uses AJAX to send the contact data to WebMerge (using our Merge URL) and displays a message when complete.  This happens behind the scenes without ever having to leave Knack!

Here’s what the button looks like in action:

Let’s click that button!  Everything worked great and here’s the document we have generated.  All of our data from Knack is in the document and it has been emailed off to the client.  How’s that for easy?

Wait, there’s more…

Generate and Download Merged Document

Creating a document behind the scenes is cool, but what if you want to open up the document right away without having to go to your email?  No problem, here is an updated code example that will generate a link that sends data to WebMerge in a new window rather than AJAX and then your document will be there for you


Use Repeatable/Loop Data

Do you have repeating data that is being printed into a table and you want those line items to come through to WebMerge? No problem! Check out the example code below:


Automatically Generate Documents

If you’re looking to automatically generate documents every time a new record is added to your Knack application, you can automate the process by using Zapier.  Zapier will detect new records and send that new data over to WebMerge without anyone needing to click a button.  Setup is quite simple and all you need to do is map your Knack fields to the WebMerge fields and Zapier will handle the rest.

Have more questions? Submit a request


  • Avatar
    Kimberley Sare

    Hi Jeremy

    I am looking to use the generate and download script you have provided an example for but using multiple view's in a single scene, some of which will be tables which is loop data.

    I am brand new to Javascript so wondering which parts of the loop data script I need to add to the "generate and download" script in order for that loop data to pull through to the downloaded report please?

    Also lets say there are four 'views' to put into a single document, which bits of the javascript would need to be repeated for each view to ensure that they all come through?


  • Avatar
    Jeremy Clarke

    Hi Kim,

    I would actually suggest using the Loop example as your base javascript, then you can update a few lines to make it downloadable.

    1. Add "download=url" at the end of your Merge URL

    2. Update the "success" function of the AJAX POST to this:

    success: function(data) {
    window.location = data.url;

    For the other views, you'll just pull in the other data like this:

    var data_123 = Knack.models['view_123'].toJSON();
    var data_129 = Knack.models['view_129'].toJSON();

  • Avatar
    Kimberley Sare

    Thanks very much Jeremy. I will give this a go shortly!

  • Avatar
    Kimberley Sare

    Hi again Jeremy

    If I want to apply the above to a data route rather than a document, is it as simple as replacing the merge URL?

  • Avatar
    Jeremy Clarke

    Yes :)

  • Avatar
    Kimberley Sare

    Hi Jeremy

    I am looking to add data to my webmerge template from different scenes and views, which are linked. Is it possible to do so using a single button? If so, can you give me some example code please? Some of the scenes will have loop data whilst others will not.

    I am not a developer and learning as I go so really appreciate your guidance - you've been incredibly helpful so far!


    Edited by Kimberley Sare
  • Avatar
    Jeremy Clarke

    Hi Kimberley,

    Yes, but you'll need to make sure those other views are loaded somewhere on your scene. Knack can only pull data (via Javascript) from views that are currently on the screen.

    To grab that other data, you would just add more lines in your code like this:

    var data_156 = Knack.models['view_156'].toJSON();
    var data_183 = Knack.models['view_183'].toJSON();
    var data_209 = Knack.models['view_209'].toJSON();

    Then you would just reference the fields within each of those views like normal (ie view_183.field_34)


  • Avatar
    Jacqueline Hannan

    Hi Jeremy,

    I have set the connection up and it works great if the view is in "details" format.

    However, if I am using a table, list or other, the code doesnt seem to work.

    Do you know how I would modify the code so that it recognises the field when it is in a table or list?



    Edited by Jacqueline Hannan
  • Avatar
    Jeremy Clarke

    Hi Jacqueline,

    Please see the code example under the "Use Repeatable/Loop Data" section above. The "incidents" are in a table on the knack page.

  • Avatar
    Aonghus Stevens

    Hi Jeremy - how can multiple buttons be placed side by side? Currently, we have 4 WebMerge exports on the one page, all stacked on top of each other.

  • Avatar
    Jeremy Clarke

    Instead of using a "div" tag around your button, try without it like this:

    $('#view_380 .view-header').after('Create Report');

Please sign in to leave a comment.
Powered by Zendesk