function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Aurélien LavalAurélien Laval 

Storing Data Offline with Salesforce Mobile SDK SmartStore

Hello, I develop en an Android application with the Salesforce Mobile SDK to store datas offline in using smartstore.

I saw the Mobile SDK Workbook but it's not said how store manually datas with smartstore.

Per example, I want when a user populate a form and submit it, store form's informations in the mobile with smartstore and I don't how do it.

In this time, I tried this :

function Track(){
    this.Id = null;
    this.Name = null;
    this.Price__c = null;
    this.Album__c = null;
    this.Album__r = null;
}

function Album(){
    this.Id = null;
    this.Name = null;
}

var track = new Track();
track.Id = $j('#idInput').val();
track.Name = $j('#nameInput').val();
track.Price__c = $j('#priceInput').val();
track.Album__c = $j('#albumIdInput').val();
track.Album__r = new Album();
track.Album__r.Id = $j('#albumIdInput').val();
track.Album__r.Name = "A name";

var myTable = [];
myTable.push(track);

addOfflineTracks(JSON.stringify(myTable), successAdd, errorAdd);

 

I get an error :

http://nsa33.casimages.com/img/2013/08/12/130812124002327909.png

 

 

I guess than I don't give the good datas to store with smartstore because when datas are loaded from a Salesforce controller, a list is given to store and I use a table.

How can I use a list en Javascript please?

 

Sorry if I use a bad language.

Best Answer chosen by Admin (Salesforce Developers) 
Aurélien LavalAurélien Laval

I have an issue for my problem, this my code :

var indexesTracks = [
   {path:"Name",type:"string"},
   {path:"Id",type:"string"},
   {path:"Album__c",type:"string"}
];

smartStore.registerSoup(TRACKS_SOUP_NAME, indexesTracks, onSuccessRegSoup, onErrorRegSoup);

function hasSmartstore() {
    var smartStoreDefined = false;
    if (cordova && cordova.require) {
        var smartStore = cordova.require("salesforce/plugin/smartstore");
        if ((typeof smartStore !== "undefined") && (smartStore !== null)) {
            console.log("SmartStore plugin found and loaded");
            smartStoreDefined = true;
        }
    }
    
    return smartStoreDefined;
}

function addOfflineTracks(entries, success, error) {
   if (hasSmartstore()) {
      var smartStore = cordova.require("salesforce/plugin/smartstore");
      smartStore.upsertSoupEntries(TRACKS_SOUP_NAME,entries, success, error);
   }
}

var trackJSON = [{
   "Name" : "Name",
   "Id" : "Id",
   "Album__c" : "1234567890123456"
}];
var successAdd = function(message){
   alert("Great : " + JSON.stringify(message));
   console.log(JSON.stringify(message));
}
                
var errorAdd = function(error){
  alert("Pas bon : " + JSON.stringify(error));
  console.log(error);
}

addOfflineTracks(trackJSON, successAdd, errorAdd);

 

All Answers

cwall_sfdccwall_sfdc

The error seems to say that there's something wrong with your Visualforce remoting call.  Can you provide your remoting Javascript source and the Apex controller method source?

 

Also, you may consider posting to the Mobile forum for quesitons about SmartStore.

Aurélien LavalAurélien Laval

I have an issue for my problem, this my code :

var indexesTracks = [
   {path:"Name",type:"string"},
   {path:"Id",type:"string"},
   {path:"Album__c",type:"string"}
];

smartStore.registerSoup(TRACKS_SOUP_NAME, indexesTracks, onSuccessRegSoup, onErrorRegSoup);

function hasSmartstore() {
    var smartStoreDefined = false;
    if (cordova && cordova.require) {
        var smartStore = cordova.require("salesforce/plugin/smartstore");
        if ((typeof smartStore !== "undefined") && (smartStore !== null)) {
            console.log("SmartStore plugin found and loaded");
            smartStoreDefined = true;
        }
    }
    
    return smartStoreDefined;
}

function addOfflineTracks(entries, success, error) {
   if (hasSmartstore()) {
      var smartStore = cordova.require("salesforce/plugin/smartstore");
      smartStore.upsertSoupEntries(TRACKS_SOUP_NAME,entries, success, error);
   }
}

var trackJSON = [{
   "Name" : "Name",
   "Id" : "Id",
   "Album__c" : "1234567890123456"
}];
var successAdd = function(message){
   alert("Great : " + JSON.stringify(message));
   console.log(JSON.stringify(message));
}
                
var errorAdd = function(error){
  alert("Pas bon : " + JSON.stringify(error));
  console.log(error);
}

addOfflineTracks(trackJSON, successAdd, errorAdd);

 

This was selected as the best answer