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 'form.getRecords'

Tap Forms Database Pro for Mac, iPhone, iPad and Apple Watch Forums Search Search Results for 'form.getRecords'

Viewing 4 results - 151 through 154 (of 154 total)
  • Author
    Search Results
  • #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 4 results - 151 through 154 (of 154 total)