Basic information on scripting.

Tagged: ,

Viewing 7 reply threads
  • Author
    Posts
  • October 24, 2020 at 2:10 AM #42386

    Michael Mckenna
    Participant

    Hi, I have been using TF5 for the past 2 months and am really enjoying a lot of the very basic elements making forms. The problem is my knowledge of scripting is zero and so I am very limited with what I can achieve. I have looked online and trawled the forums and it seems its all or nothing, it took me 2 days to find “\r” to add into a script so I could add text to the line below. The next thing is getting information from one form onto another, in my mind that should be relatively easy but I cannot find a basic tutorial on how this is performed, explaining in simple language how this is achieved.The examples don’t have to be complex just simple enough so people like me can build on a foundation. I have seen some of Sam’s videos on YouTube and they are a good start but they are still assuming people have an understanding of writing a script. I have also been trying to find detailed info on snippets with examples and clear explanations on what each one does.I know this is sounding like a whinge but I am loving this app and want to get the most out of it. Interested in your thoughts. I hope this is taken in a positive way as it is not intended otherwise.

    October 24, 2020 at 6:14 AM #42388

    T.L. Ford
    Participant
    October 26, 2020 at 1:32 AM #42420

    Sam Moffatt
    Participant

    Part of the reason I started to do the videos is that there isn’t a lot of Tap Forms specific content out there and whilst it’s vanilla JavaScript in a strict sense, it’s also obviously a specific API as well. There is something in a video where seeing everything step by step can help expose something that written documentation sometimes misses pieces that are thought to be understood. The videos I try to record in a single take without editing so as to not lose anything which also means real surprise when things don’t go 100% to plan. That’s also useful in my mind because it helps to show what can go wrong, at least a few things anyway (or just my own confusion at my own copy paste).

    Videos are of course inspired by what I see in the forums. I’ll take a chop at looking at a copying video and hopefully it can help. I’m hoping to slowly build up a library of a bunch of different recipes for solving problems for folk so that eventually it’s got a little bit of everything.

    I think what @cattailnu is doing with the tutorials is a great place to start and if you spend some time on the regular JavaScript tutorials it can help as well even though they’re generally browser focused or Node.js focused.

    That said I did a quick Google search and I think this might help as well:

    • Mozilla’s JavaScript documentation is great albeit focused on web based usage of JavaScript at times. They’ve got one of the more detailed references and if you don’t mind picking up some other web stuff (HTML/CSS), then it’s the traditional way to learn JavaScript.
    • Apple’s Mac Automation Scripting Guide has some JavaScript references but it’s pretty light, even pointing to Mozilla’s site for JavaScript.
    • Beginners Guide to JavaScript for Automation looks a little more at the script editor and how to use it with JavaScript, a similar environment to Tap Forms.
    • Looking for JXA resources also took me to a page with JavaScript for Automation resources linking to a bunch of different pages.

    We do need more resources dedicated to scripting in Tap Forms and getting folk up to speed. I’m looking forward to TF Scripting 103 helping to expand documentation base further too :)

    October 30, 2020 at 12:44 AM #42446

    Sam Moffatt
    Participant

    I had an attempt at making a much in depth look at the script field and also the first snippet which is the if statement. I’m not sure if it hits the mark or not but I figured I’d start by working through each of the snippets but then after I’d done a few test attempts decided to spend a lot more time on script fields in general and some of their quirks.

    September 13, 2021 at 10:52 AM #45277

    Kimberley Hoffman
    Participant

    I have the same problem. I really don’t know where to start because I am usually facing scripts when I have no time to learn them – kind of like a deer facing the headlights of an oncoming truck. Right now, I am guessing what might work, but have no idea how to connect it to what I am doing.

    September 14, 2021 at 1:09 AM #45288

    Sam Moffatt
    Participant

    I feel the hardest aspects of programming is mapping your mental model to what the computer is doing and forming that understanding. That isn’t easy to do and I’m not sure there are any good shortcuts for what will help you make that jump. here are some tools that help me.

    The Tap Forms Script Editor is perhaps a rough start to learning to code because it’s not a fully featured editor. That’s where online tools like an online Javascript beautifier like beautifier.io comes in handy for spotting missing brackets or jslint.com can point out that you’re reusing a variable or that a variable isn’t defined (though make sure to tell jslint about record, form, document in it’s globals section). It also can complain about stuff like long lines but that isn’t really too much of a concern.

    If your script stops working (parse error) and you don’t understand why this line is he problem you can use multiline comments to disable bits of your code and then progressively add more lines. You start by commenting a large chunk you don’t know about and then progressively expand the lines, generally top down. Take your example elsewhere on the forum:

    function Get_Zertifikatsnummer() {
    
    // Get the Zertificatsnummer from the Seriennummern form
    
    var Seriennummern_form_id = 'fld-123…';
    var Seriennummern_form = record.getFieldValue(Seriennummern_form_id);
    var name_id = 'fld-abc…';
    var name_value = customers_form.getFieldValue(name_id);
    }
    
    // Get the Print Registry, create a new record in it and set the value in a field
    
    var Print_Registry_Form = document.getFormNamed("Print Registry")
    var name_id = 'fld-xyz…';
    var seriennummer_record = get_zertifikatsnummer.addNewRecord
    get_zertifikatsnummer.setFieldValue(field_id, name_value);
    
    form.saveAllChanges()
    
    }
    

    We can probably spot this using the earlier tools easily but let’s add a multiline comment (they start with /* and end with */):

    function Get_Zertifikatsnummer() {
    
    /*
    // Get the Zertificatsnummer from the Seriennummern form
    
    var Seriennummern_form_id = 'fld-123…';
    var Seriennummern_form = record.getFieldValue(Seriennummern_form_id);
    var name_id = 'fld-abc…';
    var name_value = customers_form.getFieldValue(name_id);
    }
    
    // Get the Print Registry, create a new record in it and set the value in a field
    
    var Print_Registry_Form = document.getFormNamed("Print Registry")
    var name_id = 'fld-xyz…';
    var seriennummer_record = get_zertifikatsnummer.addNewRecord
    get_zertifikatsnummer.setFieldValue(field_id, name_value);
    
    form.saveAllChanges()
    */
    }

    Ok this gives no errors, it also does nothing but it doesn’t fail. Let’s move it down line by line until we hit an error:

    function Get_Zertifikatsnummer() {
    
    // Get the Zertificatsnummer from the Seriennummern form
    
    var Seriennummern_form_id = 'fld-123…';
    var Seriennummern_form = record.getFieldValue(Seriennummern_form_id);
    var name_id = 'fld-abc…';
    var name_value = customers_form.getFieldValue(name_id);
    /*
    }
    
    // Get the Print Registry, create a new record in it and set the value in a field
    
    var Print_Registry_Form = document.getFormNamed("Print Registry")
    var name_id = 'fld-xyz…';
    var seriennummer_record = get_zertifikatsnummer.addNewRecord
    get_zertifikatsnummer.setFieldValue(field_id, name_value);
    
    form.saveAllChanges()
    */
    }
    

    You can see I’ve moved the line with /* on it down the script, when we introduce the line it causes an error that customers_form isn’t defined. Let’s fix that and move one more line:

    function Get_Zertifikatsnummer() {
    
    // Get the Zertificatsnummer from the Seriennummern form
    
    var Seriennummern_form_id = 'fld-123…';
    var Seriennummern_form = record.getFieldValue(Seriennummern_form_id);
    var name_id = 'fld-abc…';
    var name_value = Seriennummern_form.map((rec) => rec.getFieldValue(name_id)).join(", ");
    }
    /*
    
    // Get the Print Registry, create a new record in it and set the value in a field
    
    var Print_Registry_Form = document.getFormNamed("Print Registry")
    var name_id = 'fld-xyz…';
    var seriennummer_record = get_zertifikatsnummer.addNewRecord
    get_zertifikatsnummer.setFieldValue(field_id, name_value);
    
    form.saveAllChanges()
    */
    }
    

    Ok now we get our parse error and it’s obvious this line is causing an issue and that’s because an extra } that shouldn’t be there.

    One last thing to do is to use console.log or just return stuff early. I’ve attached some screenshots from a simple test form that validated pulling values from the links that I used in the mail merge thread. Now I’ve done this pattern a bunch of times but I still built that line up, testing the output as I went in the output area to make sure it made sense. Now when you have some data you want to capture mid execution, then console.log will help you dump it. Here’s your script with some extra console.log statements sprinkled in:

    function Get_Zertifikatsnummer() {
    
        // Get the Zertificatsnummer from the Seriennummern form
        var Seriennummern_form_id = "fld-123…";
        var Seriennummern_form = record.getFieldValue(Seriennummern_form_id);
        console.log(Seriennummern_form);
        var name_id = 'fld-abc…';
        var name_value = Seriennummern_form
            .map((rec) => rec.getFieldValue(name_id))
            .join(", ");
    
        console.log(name_value);
    
        // Get the Print Registry, create a new record in it and set the value in a field
        var get_zertifikatsnummer = document.getFormNamed("Print Registry");
        var field_id = "fld-xyz…";
        var seriennummer_record = get_zertifikatsnummer.addNewRecord();
        console.log(seriennummer_record.getUrl());
        seriennummer_record.setFieldValue(field_id, name_value);
    
        form.saveAllChanges();
    }
    
    Get_Zertifikatsnummer();
    

    This puts into your console area of the script editor some extra information (conosle log also available at the bottom of the record view on the right) to help you triage and if you’re outside the script editor, it gives you a clickable link to the record you just created (the getUrl line).

    These little things help me all of the time debug and understand something I don’t fully understand…yet :D Hope it helps!

    Attachments:
    You must be logged in to view attached files.
    September 15, 2021 at 1:46 PM #45309

    Brendan
    Keymaster

    Not sure if you noticed, but I put links to TL Ford’s Scripting 101 tutorials and Sam’s scripting video tutorials on my Support page:

    https://www.tapforms.com/support/

    September 16, 2021 at 12:25 AM #45312

    Kimberley Hoffman
    Participant

    Thanks, Brendan. I do follow Sam on YouTube. However, I realise I need to learn the basic vocabulary in order to string sentences together. Thank you for linking to JavaScripting 101.

    On the bright side of life, despite my struggle with the scripting, I did manage to turn out a work-around without one. And I was super happy that I could filter information easily and quickly for this data bank. That won me back time that I otherwise would have lost had I not set up the forms. Thanks.

    September 19, 2021 at 2:15 PM #45324

    T.L. Ford
    Participant

    I’m glad the scripting tutorials are useful. Eventually, I really need to get around to 103. :)

Viewing 7 reply threads

You must be logged in to reply to this topic.