Update CARTO table from Fulcrum

This example demonstrates how to build a SQL statement to create or update records in CARTO. When the Fulcrum record is saved, a POST request is sent to the CARTO SQL API. While you can easily sync your Fulcrum app to CARTO via Data Shares and Synced Tables, or write custom webhooks, this method instantly updates your CARTO table without having to wait for a scheduled CARTO sync or even a Fulcrum sync. NOTE: The LATITUDE and LONGITUDE functions will only return values if a record's geometry type is set to Point, and there is a location set. Unless you have enabled Lines and Polygons for your app, the Point type will be the only available geometry type for records in the app.

var username = 'fulcrum',
  api_key = 'your-carto-api-key',
  type;

ON('new-record', function(event) {
  type = 'create';
});

ON('edit-record', function(event) {
  type = 'update';
});

ON('save-record', function(event) {
  if (type === 'create') {
    query = 'INSERT INTO fulcrum_points_table (fulcrum_id, name, number, color,  the_geom) VALUES ($$' + RECORDID() + '$$, $$' + $name + '$$, ' + $number + ', $$' + STATUS() + '$$, ST_SetSRID(ST_Point(' + LONGITUDE() + ', ' + LATITUDE() + '),4326))';
    postToCarto(query);
  } else if (type === 'update') {
    query = 'UPDATE fulcrum_points_table SET name=$$' + $name + '$$, number=' + $number + ', color=$$' + STATUS() + '$$, the_geom=ST_SetSRID(ST_Point(' + LONGITUDE() + ', ' + LATITUDE() + '),4326) WHERE fulcrum_id=$$' + RECORDID() + '$$';
    postToCarto(query);
  }
});

function postToCarto(query) {
  var options = {
    url: 'https://' + username + '.carto.com/api/v2/sql?q=' + encodeURIComponent(query) + '&api_key=' + api_key,
    method: 'POST'
  };

  REQUEST(options, function(error, response, body) {
    if (error) {
      ALERT('Error with request: ' + INSPECT(error));
    } else {
      ALERT('This record has been successfully posted to CARTO!');
    }
  });
}