How to set Location through script

Tagged: ,

Viewing 3 reply threads
  • Author
    Posts
  • June 12, 2025 at 3:06 AM #52446

    Tap forms Forum
    Participant

    How do I save to a location field?

    For a test I tried getting the value from a filled in location field and setting a test-location field to it, but that doesn’t work. Is this a bug, or am I using the wrong commands?

    function Test_Script() {
    	var scouting_location = record.getFieldValue('fld-b625c89aa5664db4933aa9ecc8bedcde');
    var temp_location_id = 'fld-6b6551dc87e9420aaedb2887d525cff3';
        console.log (scouting_location);
    record.setFieldValue(temp_location_id, scouting_location);
    }
    
    Test_Script();

    In the end I’m hoping to use it to retrieve a location of a photo:

    function Scouting_Script() {
        var locationFieldID = form.getFieldNamed('Scouting Location').getId();
        var photoFieldID = form.getFieldNamed('Scouting Photo').getId();
        var media = record.fetchMediaForField(photoFieldID);
    
        if (!media || media.length === 0) {
            return "No photo found";
        }
    
        var gpsData = media[0].getGpsData();
    
        if (gpsData && gpsData.Latitude && gpsData.Longitude) {
            var lat = gpsData.Latitude;
            var lon = gpsData.Longitude;
            var locationString = lat + " " + lon;
            record.setFieldValue(locationFieldID, locationString);
            document.saveAllChangesAndRefresh();
            return locationString;
        } else {
            return "No GPS data found in photo";
        }
    }
    
    Scouting_Script();
    June 12, 2025 at 9:15 PM #52453

    Brendan
    Keymaster

    You should fetch the location field value and then just set the latitude and longitude on it:

    let locationValue = record.getFieldValue(locationFieldID);
     locationValue.longitude = lon;
     locationValue.latitude = lat;

    There’s also locationTitle and locationSubTitle fields on a Location field value. The locationSubTitle field ends up displaying in the Address field. And I just realized that I will be renaming that to locationAddress in the next build.

    June 13, 2025 at 12:24 PM #52457

    Tap forms Forum
    Participant

    Thanks for your reply Brendan!

    Unfortunately I still don’t get it…

    I now wrote this (with the help of ChatGPT), but it still doesn’t work. Any pointers would be greatly appreciated

    function Copy_Location_Script() {
        var source_location_id = form.getFieldNamed("Source Location").getId();
        var target_location_id = form.getFieldNamed("Target Location").getId();
    
    let sourceValue = record.getFieldValue(source_location_id);
    
        if (!sourceValue || !sourceValue.latitude || !sourceValue.longitude) {
            return "Source location is not set.";
        }
    
        let lon = sourceValue.longitude;
        let lat = sourceValue.latitude;
    
        // Fetch current value of target field
        let locationValue = record.getFieldValue(target_location_id);
         locationValue.longitude = lon;
         locationValue.latitude = lat;
    
        // Save updated value back to record
        record.setFieldValue(target_location_id, locationValue);
        
        // Return debug info
        return "Target location set to: " + JSON.stringify(record.getFieldValue(target_location_id));
    }
    
    Copy_Location_Script();
    June 13, 2025 at 11:50 PM #52462

    Brendan
    Keymaster

    Ah whoops. Just looked into my code and you need to use locationValue.lon = lon and locationValue.lat = lat

    But I just realized that’s silly because I have reading the value with latitude and longitude, but writing the value as lat and lon. I’ve just fixed that for the next update so it’ll be latitude and longitude for reading and writing.

    So just try lat and lon for now.

    June 13, 2025 at 11:51 PM #52463

    Brendan
    Keymaster

    And don’t forget to call form.saveAllChanges(); to commit the value to the database.

    June 14, 2025 at 2:45 AM #52467

    Tap forms Forum
    Participant

    Ahhhh Thank you!

Viewing 3 reply threads

You must be logged in to reply to this topic.