Populating a text field with a calculated value

I am new to Kintone and working on a JavaScript customization that will convert latitude written in a text field in degrees-minutes-seconds (DMS) format into decimal format.

I have the formula written to convert from DMS to decimal, but I don't know how to get the result to populate in a separate text field named "Latitude - Formula." Here is what I have so far:

(function() {
  'use strict';
  kintone.events.on('app.record.edit.change.latitude', function(event) {

    // Split the DMS string into parts
    var parts = event.value.split(/[^\d\w.]+/);
    
    // Extract degrees, minutes, seconds, and hemisphere
    var degrees = parseFloat(parts[0]);
    var minutes = parseFloat(parts[1]);
    var seconds = parseFloat(parts[2]);
    var hemisphere = (parts[3] || 'N').toUpperCase(); // Assuming default hemisphere as 'N' if not specified
    
    // Convert degrees, minutes, and seconds to decimal format
    var decimal = degrees + (minutes / 60) + (seconds / 3600);

    // Adjust the decimal latitude based on hemisphere
    if (hemisphere === 'S') {
      decimal *= -1; // Southern hemisphere is negative
    }
    //Return the decimal latitude
    return decimal;
  })();

The goal is to have the user type out the coordinates they need in DMS format (as shown below) and, either when they save their changes or as they are editing (if possible), convert the value they typed in and populate the field "Latitude - Formula" with the same latitude in decimal format so the field "Map Coordinates" can generate a Google Maps link to pull up a view of that location.

I know the problem is with the penultimate line "return decimal;" but I don't know how to write it such that "Latitude - Formula" is updated. Please let me know what I need to do.

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