Last active
July 2, 2022 15:28
-
-
Save lbrenman/0cd009bde0624448bb1a to your computer and use it in GitHub Desktop.
Appcelerator Arrow Post Block to add GPS coordinates to the Salesforce Account
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Arrow = require("arrow"); | |
var Model = Arrow.Model.reduce("appc.salesforce/Account","Account",{ | |
"fields": { | |
"Name": { | |
"type": "string", | |
"description": "Account Name", | |
"readonly": false, | |
"maxlength": 255, | |
"required": true, | |
"optional": false, | |
"writeonly": false | |
}, | |
"Type": { | |
"type": "string", | |
"description": "Account Type", | |
"readonly": false, | |
"maxlength": 40, | |
"required": false, | |
"optional": true, | |
"writeonly": false | |
}, | |
"BillingStreet": { | |
"type": "string", | |
"description": "Billing Street", | |
"readonly": false, | |
"maxlength": 255, | |
"required": false, | |
"optional": true, | |
"writeonly": false | |
}, | |
"Phone": { | |
"type": "string", | |
"description": "Account Phone", | |
"readonly": false, | |
"maxlength": 40, | |
"required": false, | |
"optional": true, | |
"writeonly": false | |
}, | |
"Website": { | |
"type": "string", | |
"description": "Website", | |
"readonly": false, | |
"maxlength": 255, | |
"required": false, | |
"optional": true, | |
"writeonly": false | |
}, | |
"lat": { | |
"type": "String", | |
"custom": true | |
}, | |
"lon": { | |
"type": "String", | |
"custom": true | |
} | |
}, | |
"after": "addgps", | |
"actions": [ | |
"create", | |
"read", | |
"update", | |
"delete", | |
"deleteAll" | |
], | |
"singular": "Account", | |
"plural": "Accounts" | |
}); | |
module.exports = Model; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Arrow = require('arrow'); | |
var request = require('request'); | |
var googleMapAPIKey = "<Your API Key>"; | |
var baseURL = "https://maps.googleapis.com/maps/api/geocode/json?address="; | |
var PostBlock = Arrow.Block.extend({ | |
name: 'addgps', | |
description: 'add lat, long to the retreived data', | |
action: function(req, resp, next) { | |
// req.log.info("Post Example Block executed"); | |
var body = JSON.parse(resp.body); | |
var data = body[body.key]; | |
var dataLen = data.length; | |
var replies = 0; | |
// console.log("data = "+JSON.stringify(data)); | |
// console.log("dataLen = "+dataLen); | |
if(dataLen){ //findAll | |
data.forEach(function (_row, _index) { | |
var url=baseURL+encodeURIComponent(prepareAddress(_row.BillingStreet))+"&key="+googleMapAPIKey; | |
request(url, function (error, response, body) { | |
if (!error && response.statusCode == 200) { | |
var res = JSON.parse(body); | |
_row.lat = res.results[0].geometry.location.lat; | |
_row.lon = res.results[0].geometry.location.lng; | |
replies++; | |
if(replies == dataLen) { | |
setReply(); | |
} | |
} else { | |
console.log("google maps geocode request error"); | |
replies++; | |
if(replies == dataLen) { | |
setReply(); | |
} | |
} | |
}); | |
}); | |
} else { //findOne | |
var url=baseURL+encodeURIComponent(prepareAddress(data.BillingStreet))+"&key="+googleMapAPIKey; | |
request(url, function (error, response, body) { | |
if (!error && response.statusCode == 200) { | |
var res = JSON.parse(body); | |
data.lat = res.results[0].geometry.location.lat; | |
data.lon = res.results[0].geometry.location.lng; | |
setReply(); | |
} else { | |
console.log("google maps geocode request error"); | |
setReply(); | |
} | |
}); | |
// console.log("data = "+JSON.stringify(data)); | |
} | |
function prepareAddress(str) { | |
return str.replace(/(\r\n|\n|\r)/gm,","); | |
} | |
function setReply() { | |
console.log("post block addgps executed"); | |
resp.success(body[body.key], next); | |
} | |
} | |
}); | |
module.exports = PostBlock; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"success": true, | |
"request-id": "cfe6321b-a23d-40d1-b6a9-28feba4fa310", | |
"key": "accounts", | |
"accounts": [ | |
{ | |
"id": "001i000000PscewAAB", | |
"Name": "GenePoint", | |
"Type": "Customer - Channel", | |
"BillingStreet": "345 Shoreline Park\nMountain View, CA 94043\nUSA", | |
"Phone": "(650) 867-3450", | |
"Website": "www.genepoint.com", | |
"lat": 37.423958, | |
"lon": -122.0721391 | |
}, | |
{ | |
"id": "001i000000PscexAAB", | |
"Name": "United Oil & Gas, UK", | |
"Type": "Customer - Direct", | |
"BillingStreet": "Kings Park, 17th Avenue, Team Valley Trading Estate,\nGateshead, Tyne and Wear NE26 3HS\nUnited Kingdom", | |
"Phone": "+44 191 4956203", | |
"Website": "http://www.uos.com", | |
"lat": 55.05473019999999, | |
"lon": -1.4665574 | |
}, | |
{ | |
"id": "001i000000PsceyAAB", | |
"Name": "United Oil & Gas, Singapore", | |
"Type": "Customer - Direct", | |
"BillingStreet": "9 Tagore Lane\nSingapore, Singapore 787472\nSingapore", | |
"Phone": "(650) 450-8810", | |
"Website": "http://www.uos.com", | |
"lat": 1.384019, | |
"lon": 103.826043 | |
}, | |
{ | |
"id": "001i000000PscezAAB", | |
"Name": "Edge Communications", | |
"Type": "Customer - Direct", | |
"BillingStreet": "312 Constitution Place\nAustin, TX 78767\nUSA", | |
"Phone": "(512) 757-6000", | |
"Website": "http://edgecomm.com", | |
"lat": 29.6024873, | |
"lon": -95.54378609999999 | |
}, | |
{ | |
"id": "001i000000Pscf0AAB", | |
"Name": "Burlington Textiles Corp of America", | |
"Type": "Customer - Direct", | |
"BillingStreet": "525 S. Lexington Ave\r\nBurlington\r\nNC\r\n27215\r\nUSA", | |
"Phone": "(336) 222-7000", | |
"Website": "www.burlington.com", | |
"lat": 36.0905435, | |
"lon": -79.43733 | |
}, | |
{ | |
"id": "001i000000Pscf1AAB", | |
"Name": "Pyramid Construction Inc.", | |
"Type": "Customer - Channel", | |
"BillingStreet": "2 Place Jussieu\r\nParis\r\n75251\r\nFrance", | |
"Phone": "(014) 427-4427", | |
"Website": "www.pyramid.com", | |
"lat": 48.8460258, | |
"lon": 2.3552132 | |
}, | |
{ | |
"id": "001i000000Pscf2AAB", | |
"Name": "Dickenson plc", | |
"Type": "Customer - Channel", | |
"BillingStreet": "1301 Hoch Drive\r\nLawrence\r\nKS\r\n66045\r\nUSA", | |
"Phone": "(785) 241-6200", | |
"Website": "dickenson-consulting.com", | |
"lat": 38.957344, | |
"lon": -95.250569 | |
}, | |
{ | |
"id": "001i000000Pscf3AAB", | |
"Name": "Grand Hotels & Resorts Ltd", | |
"Type": "Customer - Direct", | |
"BillingStreet": "2334 N. Michigan Avenue, Suite 1500\nChicago, IL 60601, USA", | |
"Phone": "(312) 596-1000", | |
"Website": "www.grandhotels.com", | |
"lat": 41.850015, | |
"lon": -87.624203 | |
}, | |
{ | |
"id": "001i000000Pscf4AAB", | |
"Name": "Express Logistics and Transport", | |
"Type": "Customer - Channel", | |
"BillingStreet": "620 SW 5th Avenue Suite 400\nPortland, Oregon 97204\nUnited States", | |
"Phone": "(503) 421-7800", | |
"Website": "www.expressl&t.net", | |
"lat": 45.5190932, | |
"lon": -122.6771044 | |
}, | |
{ | |
"id": "001i000000Pscf5AAB", | |
"Name": "University of Arizona", | |
"Type": "Customer - Direct", | |
"BillingStreet": "888 N Euclid \nHallis Center, Room 501\nTucson, AZ 85721\nUnited States", | |
"Phone": "(520) 773-9050", | |
"Website": "www.universityofarizona.com", | |
"lat": 32.2332841, | |
"lon": -110.9488008 | |
}, | |
{ | |
"id": "001i000000Pscf6AAB", | |
"Name": "United Oil & Gas Corp.", | |
"Type": "Customer - Direct", | |
"BillingStreet": "1301 Avenue of the Americas \nNew York, NY 10019\nUSA", | |
"Phone": "(212) 842-5500", | |
"Website": "http://www.uos.com", | |
"lat": 40.7616971, | |
"lon": -73.9801263 | |
}, | |
{ | |
"id": "001i000000Pscf7AAB", | |
"Name": "sForce", | |
"BillingStreet": "The Landmark @ One Market\r\nSan Francisco\r\nCA\r\n94087\r\nUSA", | |
"Phone": "(415) 901-7000", | |
"Website": "www.sforce.com", | |
"lat": 37.79396, | |
"lon": -122.39496 | |
} | |
] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"success": true, | |
"request-id": "cfe6321b-a23d-40d1-b6a9-28feba4fa310", | |
"key": "accounts", | |
"accounts": [ | |
{ | |
"id": "001i000000PscewAAB", | |
"Name": "GenePoint", | |
"Type": "Customer - Channel", | |
"BillingStreet": "345 Shoreline Park\nMountain View, CA 94043\nUSA", | |
"Phone": "(650) 867-3450", | |
"Website": "www.genepoint.com" | |
}, | |
{ | |
"id": "001i000000PscexAAB", | |
"Name": "United Oil & Gas, UK", | |
"Type": "Customer - Direct", | |
"BillingStreet": "Kings Park, 17th Avenue, Team Valley Trading Estate,\nGateshead, Tyne and Wear NE26 3HS\nUnited Kingdom", | |
"Phone": "+44 191 4956203", | |
"Website": "http://www.uos.com" | |
}, | |
{ | |
"id": "001i000000PsceyAAB", | |
"Name": "United Oil & Gas, Singapore", | |
"Type": "Customer - Direct", | |
"BillingStreet": "9 Tagore Lane\nSingapore, Singapore 787472\nSingapore", | |
"Phone": "(650) 450-8810", | |
"Website": "http://www.uos.com" | |
}, | |
{ | |
"id": "001i000000PscezAAB", | |
"Name": "Edge Communications", | |
"Type": "Customer - Direct", | |
"BillingStreet": "312 Constitution Place\nAustin, TX 78767\nUSA", | |
"Phone": "(512) 757-6000", | |
"Website": "http://edgecomm.com" | |
}, | |
{ | |
"id": "001i000000Pscf0AAB", | |
"Name": "Burlington Textiles Corp of America", | |
"Type": "Customer - Direct", | |
"BillingStreet": "525 S. Lexington Ave\r\nBurlington\r\nNC\r\n27215\r\nUSA", | |
"Phone": "(336) 222-7000", | |
"Website": "www.burlington.com" | |
}, | |
{ | |
"id": "001i000000Pscf1AAB", | |
"Name": "Pyramid Construction Inc.", | |
"Type": "Customer - Channel", | |
"BillingStreet": "2 Place Jussieu\r\nParis\r\n75251\r\nFrance", | |
"Phone": "(014) 427-4427", | |
"Website": "www.pyramid.com" | |
}, | |
{ | |
"id": "001i000000Pscf2AAB", | |
"Name": "Dickenson plc", | |
"Type": "Customer - Channel", | |
"BillingStreet": "1301 Hoch Drive\r\nLawrence\r\nKS\r\n66045\r\nUSA", | |
"Phone": "(785) 241-6200", | |
"Website": "dickenson-consulting.com" | |
}, | |
{ | |
"id": "001i000000Pscf3AAB", | |
"Name": "Grand Hotels & Resorts Ltd", | |
"Type": "Customer - Direct", | |
"BillingStreet": "2334 N. Michigan Avenue, Suite 1500\nChicago, IL 60601, USA", | |
"Phone": "(312) 596-1000", | |
"Website": "www.grandhotels.com" | |
}, | |
{ | |
"id": "001i000000Pscf4AAB", | |
"Name": "Express Logistics and Transport", | |
"Type": "Customer - Channel", | |
"BillingStreet": "620 SW 5th Avenue Suite 400\nPortland, Oregon 97204\nUnited States", | |
"Phone": "(503) 421-7800", | |
"Website": "www.expressl&t.net" | |
}, | |
{ | |
"id": "001i000000Pscf5AAB", | |
"Name": "University of Arizona", | |
"Type": "Customer - Direct", | |
"BillingStreet": "888 N Euclid \nHallis Center, Room 501\nTucson, AZ 85721\nUnited States", | |
"Phone": "(520) 773-9050", | |
"Website": "www.universityofarizona.com" | |
}, | |
{ | |
"id": "001i000000Pscf6AAB", | |
"Name": "United Oil & Gas Corp.", | |
"Type": "Customer - Direct", | |
"BillingStreet": "1301 Avenue of the Americas \nNew York, NY 10019\nUSA", | |
"Phone": "(212) 842-5500", | |
"Website": "http://www.uos.com" | |
}, | |
{ | |
"id": "001i000000Pscf7AAB", | |
"Name": "sForce", | |
"BillingStreet": "The Landmark @ One Market\r\nSan Francisco\r\nCA\r\n94087\r\nUSA", | |
"Phone": "(415) 901-7000", | |
"Website": "www.sforce.com" | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment