Create Salesforce Contacts from Quip Contacts

Quoting Wikipedia: "Quip is a collaborative productivity software suite for mobile and the Web. It allows groups of people to create and edit documents and spreadsheets as a group, typically for business purposes"

Motivated by Quip Apis mentioned at here I started exploring use cases where we can leverage the apis to solve a particular problem. One use case which prevailed on all others was to Create Salesforce Contacts from Quip Contacts

Use Case: Assuming there is a sales document which was shared with some key stake holders who may or may not be associated with Salesforce org. These contacts may make sense to associate with a lead or  opportunity or account. As a Salesforce Administrator I should be able to import contacts from Quip documents to Salesforce orgs. 

In order to do this my plan was to create a lightweight utility which can easily be used. As a part of my study I figured out that Quip came with 2 kind of api wrappers one was written in python and another one in nodejs. The node.js library does a rest api call in the backend. 

I chose nodejs because I had some experience with Jsforce which is a Salesforce nodejs library. 

Before starting I played around with the nodejs api for quip contacts. Interestingly I got following response 



Note the API not only sends the name but also sends a weightage factor called affinity. 

Interestingly this affinity seems to be accurate as it somehow recommends who are the users which might be more related to this document in comparison with others. Somehow I feel this is a potent entity which can be used in many use cases to filter out those users who might not be interested in the document.

After getting comfortable with Quip Api it was time for importing Quip contacts and bridging the gap by creating those contacts in salesforce. For this I used Jsforce which gives an easy access to play around with SOQL queries

Following is the nodejs script which does this 


var quipaccessToken = "<<Quip Access Token>>";
var loginUrl = "<<Salesforce Login Url>>";
var loginUserName = "<<Salesforce login username>>";
var loginPassword = "<<Salesforce login password>>";
var quip = require('./quip.js');
var client = new quip.Client({accessToken: quipaccessToken});

importQuipContactsAndCreateSalesforceContacts(client);

/*
Get Quip Contacts and create Salesforce contacts 
*/
function importQuipContactsAndCreateSalesforceContacts(client) {
    /*     
    Get contacts with affinity     Result: name, id, affinity     
    */    
    client.getContacts(function (err, user) {
        if (err != null) {
            console.log("err: " + err);
        }

        for (var i = 0, len = user.length; i < len; i++) {
            nameArr = user[i].name.split(/\s+/);
            console.log("FirstName: "+nameArr[0]+" LastName: "+ nameArr[1]);

            //Verify if Salesforce Contact exists and then Create it            
            verifyAndCreateSalesforceContact(nameArr[0], nameArr[1]);
        }

    });
}


/*
Create a Contact in salesforce 
*/
function createContactsInSalesforce(firstname, lastname){
    var jsforce = require('jsforce');
    var conn = new jsforce.Connection({
        loginUrl: loginUrl    });
    conn.login(loginUserName, loginPassword, function(err, res) {
        if (err) {
            return console.error(err);
        }

        //Verify if the user exists. If the user is not there then insert it.        
        console.log("Inside Create");
        conn.sobject("Contact").create({FirstName: firstname, LastName: lastname}, 
        function (err, ret) {
            if (err || !ret.success) {
                return console.error(err, ret);
            }

            console.log("Created record id: " + ret.id);
        });
    });
}

/*
Verify if the contact exists 
*/
function verifyAndCreateSalesforceContact(firstName, lastName){
    var jsforce = require('jsforce');
    var conn = new jsforce.Connection({
        loginUrl: loginUrl    });
    conn.login(loginUserName, loginPassword, function(err, res) {
        if (err) {
            return console.error(err);
        }
        conn.sobject("Contact")
            .find(
                {
                    FirstName: firstName,
                    LastName: lastName
                },
                {
                    Id: 1,
                    Name: 1,
                    CreatedDate: 1                }
            )
            .execute(function (err, records) {
                if (err) {
                    return console.error(err);
                }
                if(records.length === 0){
                    createContactsInSalesforce(firstName, lastName);
                }
            })
    });
}

Note here we are getting Quip Access token from this link

The above script imports QuipContacts in importQuipContactsAndCreateSalesforceContacts function.
In verifyAndCreateSalesforceContact we verify if the contact is existing in salesforce or not.
In createContactsInSalesforce we create Salesforce contacts.

The app gives following response
Created record id: 003R0000001WuFEIA0
Created record id: 003R0000001WuF4IAK
Created record id: 003R0000001WuFOIA0
...

where it responds back with the newly created userId in Salesforce.

Hope this information helps. I feel the design for the above script can be improved, 
but here the intent was to quickly come up with a utility for import contacts. Let me know 
if you have any feedback for this.

Comments

Popular posts from this blog

Firebase authentication with Ionic creator

Big Data - SWOT Analysis

LINKEDIN api call using NODE.JS OAUTH module