Blog  |  Support  |  Forums

Search Results for 'form.getRecords'

Tap Forms – Organizer Database App for Mac, iPhone, and iPad Forums Search Search Results for 'form.getRecords'

Viewing 6 results - 136 through 141 (of 141 total)
  • Author
    Search Results
  • #33476
    Brendan
    Keymaster

    Hi Jose,

    You can’t get the specific selected records if you’ve multi-selected some records. But you can loop through record in a form or a saved search and do something with those. And you can get the currently selected record (singular).

    To get all the records in a form:

    var records = form.getRecords();

    To get all the records in the currently selected Saved Search:

    var records = search.getRecords();

    To get the current record just reference record.

    But I suppose it could be useful to be able to get an array of the currently selected records instead of just one.

    #33432
    Brendan
    Keymaster

    form.selectRecord(someRecord) is for telling Tap Forms to select a record. It’s useful when you use JavaScript to add a record to your form.

    For deleting a record, you need to get the record first before you can delete it.

    If you want to delete a single record in a loop, you have to first get the records from the form. Then loop through them, check the record to see if it’s the right one to delete, then call the function to delete it.

    var records = form.getRecords();
    var field_id = 'fld-....';
    for (var index = 0, count = records.length; index < count; index++){
        var aRecord = records[index];
        var field_value = aRecord.getFieldValue(field_id);
        if (field_value == 'some value') {
            form.deleteRecord(aRecord);
        }
    }
    
    form.saveAllChanges();

    Something like the above should work but it has to be modified for your own form of course.

    #32071
    Ryan M
    Participant

    Thought I’d play around and see if I could come up with a different approach to the same mileage tracker. This would probably be closer to a fuel log rather than a simple mileage tracker.

    The inputs would be:

    – odometer value
    – Price per gallon
    – Gallons filled

    A script would calculate your mileage since last fill up as well as your miles per gallon.

    The script uses the default created date field to sort all records by their created date. This assumes that you’re entering in your mileage log around the time you filled up.

    Here’s the script for those interested

    
    var odometer_id = 'fld-9674e316bfed408ca6710ce81b72bf05';
    var vehicle_id = 'fld-64b0b17a635f49b8b40867e45f8d24db';
    var date_created_id = 'fld-2097149a2eeb4d4e892f13b62de6b5ea';
    
    // sort records in ascending order by date value
    var dateSorter = (a, b) => 
      a.getFieldValue(date_created_id) - b.getFieldValue(date_created_id)
      
    
    var run = () => {
      var vehicle = record.getFieldValue(vehicle_id);
      var odometer = record.getFieldValue(odometer_id);
    
      var records = form.getRecords()
        .filter(r =>  r.getFieldValue(vehicle_id) === vehicle)
        .sort(odometerSorter)
    
      var odometerValues = records.map(r => r.getFieldValue(odometer_id));
      var currentRecordIndex = odometerValues.indexOf(odometer);
      var previousRecordIndex = currentRecordIndex === 0 ? 0 : currentRecordIndex - 1
                     
      var mileageValue = odometerValues[currentRecordIndex] - odometerValues[previousRecordIndex]
    
      return mileageValue;
    }
    
    run();
    
    
    Attachments:
    You must be logged in to view attached files.
    #31484
    Andy Rawlins
    Participant

    In the script editor I selected the rating field and copied the ID into your script like this:

    var rating_id = 'fld-0f294ce98c3043e0a9873bfe1933276e;;
    var records = form.getRecords();
    for (var index = 0, count = records.length; index < count; index++){
    var aRecord = records[index];
    var rating = aRecord.getFieldValue(rating_id);
    aRecord.setFieldValue(rating_id, rating * 2);
    }
    form.saveAllChanges();

    I didn’t see anything happen at all. Looking at it now, I left off the single quotes, have some extra characters at the beginning and an extra semicolon at the end. All these things appear in the email version of the script but not here on the website. I guess Mail has garbled it a bit. I have some other databases to do so will try it on them :)

    Many thanks

    Andy

    #31452
    Brendan
    Keymaster

    Hi Andy,

    You could definitely write a Form Script to do this. Or you could also use the Advanced Find & Replace function to do this. I’d make a backup of your document first whatever you do though.

    With the Find & Replace method you could start with searching the Rating field for 5, then set the value to 10. Then search for 4 and set the value to 8, then search for 3 and set it to 6, then 2 setting it to 4, then 1 setting it to 2.

    You have to do it in reverse as described above because if you go the other way, you’ll end up changing values you already changed.

    Or here’s a Form Script you can use for it:

    var rating_id = 'fld-3baff54c8e164de9b9ad3f0fbb040bc6';
    var records = form.getRecords();
    for (var index = 0, count = records.length; index < count; index++){
    	var aRecord = records[index];
    	var rating = aRecord.getFieldValue(rating_id);
    	aRecord.setFieldValue(rating_id, rating * 2);
    }
    form.saveAllChanges();

    You need to use your own rating field ID though (e.g. ‘fld-…..’). The above is specifically for my test form.

    But make sure you run it only 1 time otherwise you’ll double the rating values again.

    #30952
    Brendan
    Keymaster

    Sure, you could update a record in a Field Script. An example of that might be selecting something from a Pick List that triggers a script to run and it updates a different field in that same record.

    You could also have a Saved Search that fetches the records you want to update and run the script just on those records. You would use search.getRecords() instead of form.getRecords(). Or you can ask the form for a specific with a provided name. E.g. var search = form.getSearchNamed('Action & Adventure'); Then do search.getRecords();.

Viewing 6 results - 136 through 141 (of 141 total)
 
Apple, the Apple logo, iPad, iPhone, and iPod touch are trademarks of Apple Inc., registered in the U.S. and other countries. App Store is a service mark of Apple Inc.