Populating a text field with a calculated value

Hello @kevinrotenkolber
I just revised your script, this would do the trick.
I added additional events for testing purpose, but feel free to edit it.

(function() {
    'use strict';

    // Function to convert DMS to decimal
    function convertDmsToDecimal(latitudeValue) {
        if (!latitudeValue) return null;
        var parts = latitudeValue.split(/[^\d\w.]+/);
        if (parts.length < 3 || parts.length > 4) return null;
        var degrees = parseFloat(parts[0]);
        var minutes = parseFloat(parts[1]);
        var seconds = parseFloat(parts[2]);
        var hemisphere = parts.length === 4 ? parts[3].toUpperCase() : 'N';
        var decimal = degrees + (minutes / 60) + (seconds / 3600);
        if (hemisphere === 'S') decimal *= -1;
        return decimal;
    }

    // Handles both record creation and editing scenarios for Latitude field changes
    var changeEvents = [
        'app.record.create.change.Latitude',
        'app.record.edit.change.Latitude'
    ];
    kintone.events.on(changeEvents, function(event) {
        var decimal = convertDmsToDecimal(event.record.Latitude.value);
        if (decimal !== null) {
            event.record.LatitudeFormula.value = decimal.toString();
        }
        return event;
    });

    // Handles both record creation and editing scenarios for submitting records
    var submitEvents = [
        'app.record.create.submit',
        'app.record.edit.submit'
    ];
    kintone.events.on(submitEvents, function(event) {
        var decimal = convertDmsToDecimal(event.record.Latitude.value);
        if (decimal !== null) {
            event.record.LatitudeFormula.value = decimal.toString();
        }
        return event;
    });
})();

2 Likes