Tap Forms app icon half
Tap Forms Forum text image
Blue gradient background

Exchange tips and ideas with the Tap Forms community

Search Results for 'script'

Viewing 15 results - 1,201 through 1,215 (of 2,989 total)
  • Author
    Search Results
  • #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:

    Support

    #45302

    In reply to: Mail Merge not working

    Sam Moffatt
    Participant

    My brother says the hardest part about coding is knowing what you result you want to appear.
    I beg to differ. I know what I want to appear, but don’t know the magic formula for that yet.

    Once you know how to code I can see the first taking over but you’ve got to learn how to put the code together. That takes some time to figure out the pieces.

    So I will repeat my (thought) process here, maybe you will see my error:
    1. I import a list of 6 digit series numbers as a .csv file into the file “Seriennummern”. The .csv-file is exported from Numbers, so I reduce the likelihood of typos.You can easily create an ascending digit column in Numbers by typing in the first two fields, marking them and dragging the curser down the column.

    At some point this should become a script you run in TF to generate the entries so you don’t have to import a CSV file just to generate a series.

    2. I input information in the print registry, like Title, format, etc.
    3. I choose the “Zertifikatsnummer”. I have this as a one to many field
    4. The series of the certificate numbers, “Fortlaufende Hologrammnummern der Serie” is also a one to many field. Explanation: I must express in the certificate print form how many of Print X is made and what series numbers this print edition will have.
    5. I will print out the certificate, affix the appropriate hologram to both the print and the certificate

    Are both of these link to form fields (“Zertifikatsnummer” and the “Hologrammnummern”) linking to the same “Seriennummern” form? Or are they different series?

    I think what you are saying is that it is just better to copy and paste the serial number and the range of serial numbers into a document until I have finished my baby steps. Right?

    There was a quote I heard the other day that “You need to get things done before you get bored with them” and I’d substitute “bored” with “frustrated” in this case. There is a pragmatic path to getting unblocked and the long term process improvements. Learning when you’re stressed is a path to more stress :) Solve the initial problem and come back to making it better :)

    (But I still think that having the certificate databank in Tapforms is the best solution for my records, even if I cannot automatically output a certificate. I can add the images and still have a record of the series of certificate numbers)

    So, I have taken a work-around, because it is impossible to understand/learn coding on the fly for me. I have added some extra fields to copy and paste my numbers into. That does let me do a simple range.

    It isn’t an elegant solution, but it will work for me. Thanks for all your efforts on my behalf.

    It’s not an elegant solution but it’s something that enables us to have something that works and we can go from there. On the script talk basic information on scripting topic I had started to add some logging. If you run this in the script editor, you should see some output on the console log area on the right:

    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 should output some information to help understand the internal state of the script and why things are empty…or where they start being empty :)

    #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.
    #45284

    In reply to: Mail Merge not working

    Sam Moffatt
    Participant

    As an aside, it might be easier to just have a text field for the series number and manage it manually, especially if sometimes it comes from one place and sometimes another. If you’re only excluding numbers within a range (e.g. 20, 21 and 22 in the example), then you could still script it though it’s a little more work to mask those numbers but not impossible.

    One other thought is to copy/paste through an online Javascript beautifier like beautifier.io. Attached is what it looked at a first pass (I also had to fix the smart quotes). The indentation change makes it clear where the problem is and taking it out results in everything being indented properly. If I move over to jslint.com I can paste it and it’ll give me a slightly different error message:

    1. [JSLint was unable to finish] Expected ‘(end)’ and instead saw ‘}’.
    }

    Here is the code with a few more changes, but it still won’t work:

    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.getFieldValue(name_id);
    
        // 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 name_id = "fld-xyz…";
        var seriennummer_record = get_zertifikatsnummer.addNewRecord();
        seriennummer_record.setFieldValue(field_id, name_value);
    
        form.saveAllChanges()
    
    }
    

    Why won’t it work? Well this line:

        var Seriennummern_form = record.getFieldValue(Seriennummern_form_id);
    

    This line doesn’t return a Tap Forms record but returns an array of records, perhaps think of it like a bag. You need to loop over it to get a scalar or single value. Now there is a kind of quick way that will get you a comma separated list of fields, it looks like this:

        var name_value = record.getFieldValue(Seriennummern_form_id).map((rec) => rec.getFieldValue(name_id)).join(", ");
    

    This is a bit of black magic and is a little unreadable but unpacking:

    • record.getFieldValue(Seriennummern_form_id) will give us all of the child records, I’m assuming this is your link to the serial number form.
    • map is a special function that says take this value (in case rec which is technically a TFFormEntry) and then replace it with the result of the other command (in this case rec.getFieldValue(name_id) which I’m guessing is the serial number). So now instead of it all being child records, it’s the serial numbers.
    • Now that we’re dealing with just the serial numbers in the array, we can do join(", ") to make them into a comma separated list (technically it’s comma and then space since it’s ,).

      I suspect that’s what you’re after and will give you a quick start for that field. You could still loop to replace contiguous numbers but I feel doing some of it by hand might be ok whilst you learn.

      Here’s my final script with a few extra changes. I ran this through JSLint and fixed each of it’s complaints (e.g. there wasn’t a field_id and there was two name_id fields, so I renamed the second one to be field_id). I also added a call to the function, otherwise the script will do nothing (e.g. the Get_Zertifikatsnummer(); line at the end).

      function Get_Zertifikatsnummer() {
      
          // Get the Zertificatsnummer from the Seriennummern form
          var Seriennummern_form_id = "fld-123…";
          var name_id = "fld-abc…";
          var name_value = record.getFieldValue(Seriennummern_form_id)
              .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 get_zertifikatsnummer = document.getFormNamed("Print Registry");
          var field_id = "fld-xyz…";
          var seriennummer_record = get_zertifikatsnummer.addNewRecord();
          seriennummer_record.setFieldValue(field_id, name_value);
      
          form.saveAllChanges();
      }
      
      Get_Zertifikatsnummer();
      

      One other note, if there is a field in this form that links to the Print Registry then you should use record.addNewRecordToField(link_field_id) which will not only create a new record in the target form for you but also create the linking metadata as well.

    Attachments:
    You must be logged in to view attached files.
    #45278

    In reply to: Mail Merge not working

    Kimberley Hoffman
    Participant

    I was looking through the script forum. This seems close but it isn’t exact: https://www.tapforms.com/forums/topic/how-to-get-and-add-data-from-other-form/

    I got it up to here:
    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(“Get Zertificatesnummer”)
    var

    Am I getting close? The top part seems to turn up without error, but the field is empty. Or am I looking in the wrong direction?
    Thanks
    Kimberley

    #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.

    #45273

    In reply to: Mail Merge not working

    Kimberley Hoffman
    Participant

    Hi Brendan,
    Thanks for getting back to me so fast. I had originally used “Edition Nr.” but changed it, forgetting to correct it in the layout.

    For the series numbers, I need to display

      A certain certificate number that I get from the hologram label (Zertifikatsnummer)
      A specific range of certificate numbers, i.e 000019 to 000023 (or however high the edition is), but not all the certificate numbers that I enter in the second form

    I am not quite sure what kind of script I would need to apply to those two.(get records?) (My head begins to send panicky smoke signals when someone says “script”.)
    Thanks
    Regards,
    Kimberley

    #45271

    In reply to: Mail Merge not working

    Brendan
    Keymaster

    Hi Kimberly,

    The only one I see not working is [Edition Nr.]. But in your fields list you have it defined as Edition Nummer. All the others appeared to be substituted properly when you print.

    For the series numbers, you won’t be able to reference those in your record directly. Since it’s a one-to-many, is it ALL of the series numbers that you want to display that are related to the parent record? You would most likely need a script field to extract the data from the Link to Form field to get the list of series numbers to include in the report.

    Thanks,

    Brendan

    • This reply was modified 4 years, 4 months ago by Brendan.
    #45237
    pinkcadillac
    Participant

    Wow Sam, that was the missing bit and it works perfectly and is far quicker to click “Now” than to type out the current date and exact time.

    Many, many thanks.

    I’ve no idea about scripts but this fixed it for me.

    I’ll retain both my old Date field (using it’s calendar accessory) for historic orders and use the new T&D with it’s calendar from now on.

    My Sorting is sorted too!

    Great – thank you everyone!

    PS is there a link to a file/doc/record which has all the possible TF fields and can it be added to my TF?

    #45236
    Sam Moffatt
    Participant

    Enable “show accessory controls” on the layout panel for your layout to see the calendar icon.

    You could probably use a quick form script to copy the date across to your new field if you want to use it exclusively.

    #45207

    In reply to: Help with script

    Daniel Leu
    Participant

    In Javascript, comparison is == not =. So you should use

    if (group==1) {
       record.setFieldValue('fld-e59ea447a29b4c518bd307507e243f92', 20);
    }
    • This reply was modified 4 years, 4 months ago by Daniel Leu.

    Cheers, Daniel

    ---
    See https://lab.danielleu.com/tapformspro/ for scripts and tips&tricks

    #45204

    In reply to: Help with script

    Guillermo q
    Participant

    Thank you for your awesome work and help!! This is amazing, it worked!

    I have another question. I have a field (price) that has a fixed value depending on the input of another field (group).
    group 1= 20
    group 2= 40
    group 3 = 54

    I tried with my absolute ignorance of programming creating a field with a script.

    function pricetest() {
    
    var group = record.getFieldValue('fld-9565e24e59c6412ca388144d04e0bd07');
    
    if (group=1) {
    	record.setFieldValue('fld-e59ea447a29b4c518bd307507e243f92', 20);
    }
    
    if (group=2) {
    	record.setFieldValue('fld-e59ea447a29b4c518bd307507e243f92', 40);
    }
    
    if (group=3) {
    	record.setFieldValue('fld-e59ea447a29b4c518bd307507e243f92', 54);
    }
    
    }
    
    Pricetest();

    Any help? Thank you so much.

    • This reply was modified 4 years, 4 months ago by Guillermo q.
    #45198

    In reply to: Help with script

    Sam Moffatt
    Participant

    If you’re on iOS/iPadOS, I’d just copy the values out and paste into Numbers (Apple’s spreadsheeting tool) or Excel (free for phones, I think a charge exists on iPads). If you change the console output to use commas, it should paste fine.

    A small change could also directly put this on your pasteboard:

    
    	var lines = [];
    	// log to console the aggregated values.
    	for (var month in rollups) {
    		var line = month + "," + rollups[month]
    		console.log(line);
    		lines.push(line);
    	}
    
    	Utils.copyTextToClipboard(lines.join("\n"));
    

    The Mac version of Tap Forms has some charting functionality, I’m not entirely sure if it supports aggregation or not but that would be the first place I’d look. Supporting aggregation seems like a reasonable feature ask if it doesn’t exist but that can only be answered by the Keymaster.

    If you’re on the Mac, then an alternative could be to just write it to a new form and then chart it. To do this is going to be a bit more of a rewrite so that we can sort things easier

    	
    function Aggregate_By_Date() {	
    	// this is where we're going to store the rollups per day.
    	var rollups = {};
    	
    	// iterate to all of the records in the form
    	for (var rec of form.getRecords()) {
    		var purchase_date = rec.getFieldValue('fld-ccbd9a8f51d34246bebfb31aa4e397dd');
    		var price = parseFloat(rec.getFieldValue('fld-08129d71ab0f4fa4a2749456281fca07'));
    
    		// Skip entries that don't have a price or date set.
    		if (!price || !purchase_date) {
    			continue;
    		}
    		
    		// format the date for use in the rollup.
    		var formattedDate = purchase_date.getFullYear() + "-" + purchase_date.getMonth() + "-" + purchase_date.getDay();
    
    		// Rollup to this date, add to the existing value or set it if not set.
    		rollups[formattedDate] ? rollups[formattedDate] += price : rollups[formattedDate] = price;
    	}
    	
    	// Get the rollups form.
    	var rollupsForm = document.getFormNamed("Rollups");
    
    	// Delete previous roll up records.
    	rollupsForm.getRecords().forEach(rollupRec => rollupsForm.deleteRecord(rollupRec));
    	
    	var lines = [];
    	
    	// log to console the aggregated values.
    	for (var month in rollups) {
    		var line = month + "," + rollups[month]
    		console.log(line);
    		lines.push(line);
    
    		var rollupRec = rollupsForm.addNewRecord();
    		rollupRec.setFieldValue("fld-cd1d454672c84bce8103a4267507ca03", month);
    		rollupRec.setFieldValue("fld-9eeeff7120db401b830ccec4e06f2bc3", rollups[month]);
    	}
    	
    	document.saveAllChanges();	
    
    	Utils.copyTextToClipboard(lines.join("\n"));
    }
    
    Aggregate_By_Date();
    

    Change to match the form name and field IDs but that should populate a new form with the records in the right format. Then you can use the chart feature on the Mac to visualise it.

    Major changes in the full script, the formatted date is done by getting each value out:

    		// format the date for use in the rollup.
    		var formattedDate = purchase_date.getFullYear() + "-" + purchase_date.getMonth() + "-" + purchase_date.getDay();
    

    This produces a format that is easier to sort on but not zero padded, doing so would require grabbing the sprintf implementation via the Script Manager so I’ve left it out for simplicity sake.

    Next big change is our loop, I’ve incorporated the above pasteboard change but the core is as follows:

    	// Get the rollups form.
    	var rollupsForm = document.getFormNamed("Rollups");
    
    	// Delete previous roll up records.
    	rollupsForm.getRecords().forEach(rollupRec => rollupsForm.deleteRecord(rollupRec));
    	
    	var lines = [];
    	
    	// log to console the aggregated values.
    	for (var month in rollups) {
    		var line = month + "," + rollups[month]
    		console.log(line);
    		lines.push(line);
    
    		var rollupRec = rollupsForm.addNewRecord();
    		rollupRec.setFieldValue("fld-cd1d454672c84bce8103a4267507ca03", month);
    		rollupRec.setFieldValue("fld-9eeeff7120db401b830ccec4e06f2bc3", rollups[month]);
    	}
    	
    	document.saveAllChanges();	
    

    We’re getting a form named “Rollups”, we’re going to clean it out each script run and then as we go through the rollups, we’ll add a new record and set a text field and a number field. We need to tell Tap Forms to save the changes as well which is the last line of that snippet. As with previously, replace the field ID’s with your own.

    When doing this roll up, if in your main form you add a new script field (I called mine “Purchase Date Simplified”) and put this content in it:

    function Purchase_Date_Simplified() {
    		var purchase_date = record.getFieldValue('fld-ccbd9a8f51d34246bebfb31aa4e397dd');
    		if (!purchase_date) {
    			return;
    		}
    		
    		// format the date for use in the rollup.
    		return purchase_date.getFullYear() + "-" + purchase_date.getMonth() + "-" + purchase_date.getDay();
    }
    
    Purchase_Date_Simplified();
    

    Then in your rollup form you can create a new “Link to Form” field, link it to the original form, then you set it to the JOIN type and you can match the text field to the script field. Then you can easily go from a roll up record to see which items were in the rollup.

    #45196
    Mark Robbins
    Participant

    Thanks, Daniel.
    Your examples helped me get through the record looping, and things appear to be working as desired now. I ended up incorporating the original Fetch_Bgg_Information instead of calling it with form.RunScriptNamed(). When I get time for additional testing, I might pull it back out.

    #45195
    Daniel Leu
    Participant

    I treat form.runScriptNamed() like an include in other languages to define constants and functions. So at the beginning of my script, I’d have form.runScriptNamed(‘Fetch_Bgg_Information’); but without any local function calls.

    Then in the loop over records, you would call whatever function you have and provide the current record id as an argument. Maybe something like this:

    for (thisRecord in records) {
       // go to the record and run the function;
       console.log(“BGGid: ” + records[thisRecord].getFieldValue(field_bggid));
       console.log(“Index: ” + thisRecord);
       Fetch_Bgg_Information(records[thisRecord].getId());
    }

    And then in you Fetch_Bgg_Information script you would have the function

    function Fetch_Bgg_Information(myRecordId){
       let myRecord = form.getRecordWithId(myRecordId);
       .....
    }

    Or you could just define let myRecord = records[thisRecord] in the loop and then access myRecord in your function Fetch_Bgg_Information() without using an argument. I prefer to use function call arguments, specially when the function is defined in a different script.

    Cheers, Daniel

    ---
    See https://lab.danielleu.com/tapformspro/ for scripts and tips&tricks

Viewing 15 results - 1,201 through 1,215 (of 2,989 total)