How to save a JS Var to a field

  • 1
  • Question
  • Updated 2 weeks ago
  • Answered
Hi everyone, 

I have been trying to get some javascript to grab the gps geolocation of the user when a js snippet is run and add this to a couple of fields (longitude / latitude ).

My issue is saving the var to the fields - 

This is my js ( I think this works ok)...

navigator.geolocation.getCurrentPosition(function(position) {  
  var gps = (position.coords.latitude+position.coords.longitude);  
  window.GlobalVar = gps;
  continueSomeProcess();
});

Does anyone have any ideas how to get this to work?
Photo of Matt Small

Matt Small

  • 100 Points 100 badge 2x thumb
  • frustrated

Posted 2 weeks ago

  • 1
Photo of Mark DeSimone

Mark DeSimone, Official Rep

  • 11,670 Points 10k badge 2x thumb
Hi Matt. It sounds like you may be able to use the updateRow API for this:

https://docs.skuid.com/latest/en/skuid/api/skuid_model_model.html#skuid.model.Model.updateRow

I would recommend reading through this document top to bottom for a great introduction on using Javascript with Skuid:
https://docs.skuid.com/latest/en/skuid/javascript/skuid-javascript.html#updating-a-row

I've linked to the part of the document that mentions the updateRow API so you can see an example.
Photo of Zach McElrath

Zach McElrath, Employee

  • 49,576 Points 20k badge 2x thumb
One common pattern in Skuid Pages is to have a Ui-Only Model called "Vars" or "PageVars", which has a single row in it (which gets created on page load using "Create default row if model has none". Then you can save data to various fields in that Model from your code, e.g. like this:

navigator.geolocation.getCurrentPosition(function(position) {  
var PageVars = skuid.$M("PageVars");
  PageVars.updateRow(PageVars.getFirstRow(), {
"Latitude": position.coords.latitude,
     "Longitude": position.coords.longitude
});
  continueSomeProcess();
});

Then, anywhere in Skuid, you can grab the value of the Latitude / Longitude in the Action Framework, or using Merge Syntax, or you can display the Latitude / Longitude in a Field Editor --- whatever you want.
(Edited)
Photo of Matt Small

Matt Small

  • 100 Points 100 badge 2x thumb
Thanks for the suggestions guys. 

I will be updating two fields on the model (which are fields on the SF object)... Latitude__c and Longitude__c.
The object / model is called "ThisWeeksRosters."

I came up with the following but it still doesn't seem to work...

navigator.geolocation.getCurrentPosition(function(position) {  
  var PageVars = skuid.$M("PageVars");
  $.each(ThisWeekRostersModel.data,function(i,row){     
    ThisWeekRostersModel.updateRow(row,{
     "Latitude": position.coords.latitude,
     "Longitude": position.coords.longitude  });
  continueSomeProcess();
  ThisWeekRostersModel.save();
});
Photo of Ben Hubbard

Ben Hubbard, Employee

  • 12,530 Points 10k badge 2x thumb
Your code is using "Latitude" and "Longitude", but in your comment you say the fields are called "Latitude__c" and "Longitude__c". Can you try that code again but use the api names of the fields?
Photo of Zach McElrath

Zach McElrath, Employee

  • 49,576 Points 20k badge 2x thumb
Echo what Ben says --- also i don't see where "ThisWeekRostersModel" is defined.