Browse files

Salesforce query done

  • Loading branch information...
1 parent 0ac4ab9 commit 957444b3c3f46b265e42e80522cc740780d124ae Amir Nathoo committed Jul 11, 2012
Showing with 69 additions and 9 deletions.
  1. +3 −1 js/main.js
  2. +66 −8 js/salesforce.js
View
4 js/main.js
@@ -1,5 +1,7 @@
var state = {
- token: ""
+ token: "",
+ identity: null,
+ opportunities: null
}
forge.tabbar.addButton({
View
74 js/salesforce.js
@@ -1,23 +1,81 @@
+//TODO Implement refresh token otherwise this will stop working a while after initial oauth
+forge.enableDebug();
+
var salesforce = {
consumer_key: "3MVG9y6x0357HledXTja.viKSYW0gEMrDKbD6pf.AoDYMgQdxNAUAMdC6ra2TdamileUqZWSodRyqTdgJIAZH",
consumer_secret: "1325734187136715755",
login: function() {
forge.tabs.openWithOptions({
- url: "https://login.salesforce.com/services/oauth2/authorize?client_id=" + salesforce.consumer_key + "&response_type=token&redirect_uri="+encodeURIComponent("https://login.salesforce.com/services/oauth2/success"),
+ url: "https://login.salesforce.com/services/oauth2/authorize?client_id=" + salesforce.consumer_key + "&display=touch&response_type=token&redirect_uri="+encodeURIComponent("https://login.salesforce.com/services/oauth2/success"),
pattern: "https://login.salesforce.com/services/oauth2/succ*",
title: "Salesforce Login"
}, function(data) {
- state.token = data.url.split('#')[1].split('&')[0];
- forge.prefs.set('token', state.token);
+ state.token = decodeURIComponent(data.url.split('#access_token=')[1].split('&')[0]);
+ //forge.prefs.set('token', state.token); TODO: implement refresh token functionlity, for now need to login each time
+ salesforce.getIdentity(decodeURIComponent(data.url.split('&id=')[1].split('&')[0]));
+ });
+ },
+
+ getIdentity: function(url) {
+ forge.request.ajax({
+ url: url,
+ type: "POST",
+ data: {
+ "version": "latest",
+ "format": "json",
+ "oauth_token": state.token
+ },
+ headers: {
+ "Authorization": "OAuth "+ state.token
+ },
+ success: function (data) {
+ if (typeof data == "string") {
+ data = JSON.parse(data);
+ }
+ state.identity = data;
+ //forge.prefs.set('token', JSON.stringify(state.identity)); TODO: implement refresh token functionlity, for now need to login each time
+ salesforce.getOpportunities();
+ },
+ error: function(data) {
+ forge.logging.log('Error getting identity');
+ forge.logging.log(data);
+ }
+ });
+ },
+
+ getOpportunities: function() {
+ forge.request.ajax({
+ url : state.identity.urls.query.replace("{version}", "24.0") + "?q=" + encodeURI("SELECT Name FROM Opportunity where StageName='Prospecting' or StageName='Qualification'"),
+ headers : {
+ 'Authorization' : 'OAuth ' + state.token
+ },
+ contentType: "application/json",
+ success : function(response) {
+ if (typeof response == "string") {
+ response = JSON.parse(response);
+ }
+ if(response && response.records) {
+ var data = response.records;
+ }
+ state.opportunities = data;
+ },
+ error: function(data) {
+ forge.logging.log('Error getting opportunities');
+ forge.logging.log(data);
+ }
});
}
}
forge.prefs.get('token', function(token) {
- if (token) {
- state.token = token;
- } else {
- salesforce.login();
- }
+ forge.prefs.get('identity', function(identity) {
+ if (token && identity) {
+ state.token = token;
+ state.identity = JSON.parse(identity);
+ salesforce.getOpportunities();
+ } else {
+ salesforce.login();
+ }
+ });
});

0 comments on commit 957444b

Please sign in to comment.