All in Cloud : Firebase — Salesforce integration using Cloud Functions
Here are some excerpts from my latest integration journey…
What Were We Doing?
Breathing life to yet another innovation. A mobile app that deals with customers, assets and lot more
Technology Stack!!
Firebase for many obvious reasons like ready made backend, database, Authentication and all that you need from an app backend.
Needless to say we started off with android native app
Salesforce yes sounds big but its not as big as the business case. We wanted to have contacts, customers, service requests etc. painlessly flowing from Firebase to Salesforce in real time.
Slack for customer service and team chats
Turnkey — Firebase Cloud Functions
This one feature of Firebase makes the platform truly backend for any use case.
Events as they call it is at the heart of cloud functions. Every change that happens to Firestore database is emitted as an event to this framework.
Without depending on a third party Salesforce app, we quickly build few cloud functions to push the data to Salesforce as it gets created/updated/deleted in Firestore.
Cloud function is nothing but a piece of Node code which can access firebase backend features.
We chose to leverage Salesforce secure REST API from the firebase cloud functions to create contacts, leads, cases etc.
And pushing service requests to Slack channel for the service reps act on immediately. Thanks firebase slack integration plugin
With very less investment, time we could build top class infrastructure for a startup Idea.
Here is the sample code for the cloud function to create a contact in Salesforce
var sfoptions = {
‘method’: ‘POST’,
‘hostname’: ‘dev.edu.my.salesforce.com’,
‘path’: ‘/services/apexrest/ContactCreate’,
‘headers’: {
‘Content-Type’: ‘application/json’,
‘Authorization’: ‘Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx’,
},
‘maxRedirects’: 20
};
exports.createContact = functions.firestore
.document(‘users/{uid}’)
.onCreate((snap, context) => {
var req = https.request(sfoptions, function (res) {
var chunks = [];
res.on(“data”, function (chunk) {
chunks.push(chunk);
});
res.on(“end”, function (chunk) {
var body = Buffer.concat(chunks);
console.log(body.toString());
});
res.on(“error”, function (error) {
console.error(error);
});
});
const uid = context.params.uid;
const user = snap.data();
var postData = JSON.stringify({“FirstName”:user.name === null ? ‘NotSet’ : user.name,
“LastName”: user.name === null ? ‘NotSet’ : user.name,”MobilePhone”:user.mobile});
req.write(postData);
return req.end();
});
Happy learning…keep learning