The Power of Salesforce and Google Drive Integration with Lightning Web Components

06 Feb, 2024 | 6 minutes read

In today’s fast-paced business environment, efficiency and collaboration are key components of success. For organizations leveraging Salesforce as their Customer Relationship Management (CRM) platform and relying on Google Drive for document management, the integration between these two powerful tools can significantly enhance their workflow and productivity. In this blog post, we’ll explore the seamless integration between Salesforce and Google Drive, and how Lightning Web Components (LWC) in Salesforce can be leveraged to interact with documents.

Salesforce and Google Drive Integration

Salesforce is a leading CRM solution that helps organizations manage customer relationships, streamline sales processes, and enhance collaboration. Google Drive, on the other hand, is a cloud-based file storage and synchronization service that facilitates document sharing and collaboration. Integrating these two platforms brings about several benefits including centralized data management, increased productivity, and real-time collaboration.

1. Setting Up Salesforce and Google Drive Integration

Named Credentials for Google Drive Integration

Named Credentials provide a secure and easy way to authenticate to an external service, such as Google Drive, without exposing sensitive information like usernames and passwords. To set up named credentials for our Google Drive app, we must:

Create an Authentication Provider in Salesforce

Authentication Provider

An Authentication Provider in Salesforce acts as the bridge between your Salesforce organization and an external service, facilitating secure and standardized authentication. To set up an Authentication Provider we need to:

Navigate to Authentication Providers:

  • In Salesforce Setup, go to Setup > Authentication Providers.

Create a New Authentication Provider:

  • Click on New Authentication Provider.

Configure the Authentication Provider:

  • Provider Type: Google
  • Name: Google Drive Authentication (or your preferred name)
  • URL Suffix: GoogleDriveAuth (this becomes part of the callback URL)
  • Consumer Key and Consumer Secret: Use the values obtained when setting up the Google Cloud Platform project and credentials.
  • Authorize Endpoint URL: https://accounts.google.com/o/oauth2/auth
  • Token Endpoint URL: https://accounts.google.com/o/oauth2/token
  • User Info Endpoint URL: https://www.googleapis.com/oauth2/v1/userinfo
  • Default Scopes: “openid profile email”

Set the Registration Handler:

  • For simplicity, you can use the default Default Salesforce Registration Handler. This handler creates a new user in Salesforce when a user authenticates for the first time.

Callback URL:

  • The Callback URL is automatically generated based on your Salesforce instance URL and the URL Suffix you provided. Note it for later use.

Save and Retrieve the Salesforce Certificate:

  • Save the Authentication Provider configuration.
  • Click on the name of the newly created Authentication Provider.
  • In the “Certificate and Key Management” section, download the Salesforce Certificate.

Configure Named Credentials in Salesforce

This can be achieved by going to Setup -> Named Credentials and creating a new External Credential and Named Credential for Google Drive.

To set up the External Credential, you need to provide the following information:

  • Label: GD ExternalCredential (or your preferred label)
  • Name: GD_ExternalCredential (API names are auto-generated)
  • Authentication Protocol: OAuth 2.0
  • Authentication Flow Type: Browser Flow
  • Authentication Provider: a provider that is created in the previous step (Google Drive Authentication).
  • Scope: openid profile email (these are the minimal scopes required for basic Google Drive interactions)
Authentication Provider - Scope

To set up the Named Credential, you need to provide the following information: 

  • Label: GD credential (or your preferred label)
  • Name: GD_credential (API names are auto-generated)
  • URL: https://www.googleapis.com (this is the base URL for Google Drive API)
  • External Credential: the credential that was created earlier (GDExternal_Credential)      
Authentication Provider - Authentication

2. Apex Methods for Google Drive Interaction

With the Named Credential set up, you can now create Apex methods to interact with the Google Drive API securely. Salesforce provides Apex support for HTTP requests, allowing you to make requests to external APIs like Google Drive.

Here is an example of Apex Class (GoogleDriveIntegrationController.cls) :

Apex Class

In our current context, we employ these credentials to ensure secure authorization for retrieving all files from Google Drive and generating links within our application. Additionally, we invoke a method that generates files/folders in Google Drive if they do not already exist. These methods are called and utilized within Lightning Web Components (LWC).

Lightning Web Components in Salesforce

Introduction to Lightning Web Components (LWC)

Lightning Web Components (LWC) are a modern and agile framework for building components in the Salesforce ecosystem, useful in salesforce integration. They offer a more efficient way to develop applications with enhanced performance and better code maintainability.

Leveraging LWC for Document Interaction

1. Listing Files and Folders:

The Lightning Web Component can be designed to display a comprehensive list of files and folders from Google Drive associated with a specific record in Salesforce.

Users can easily navigate through the document structure and locate the files they need.

<template>
 <lightning-card title="Google Drive Documents">
   <div class="slds-p-around_medium lgc-bg">
    <div class="files_title">Files</div>
     <template for:each={SFData.SFfiles} for:item="item">
      <div class="slds-tree__item" key={item.Id}>                
       <span class="slds-has-flexi-truncate">
       <lightning-icon icon-name="doctype:link"></lightning-icon>
  <lightning-formatted-url value={item.File__c} label={item.Name} target={item.File__c} >
        </lightning-formatted-url>    
       </span>          
      </div>
     </template> 
   </div> 
  </div>        
 </lightning-card>
</template>

2. Uploading New Documents:

The Lightning Web Component should also provide a user-friendly interface for uploading new documents to Google Drive directly from Salesforce.

This functionality enhances user convenience and ensures that all relevant documents are stored in a centralized location.

div slot="actions">
<lightning-file-upload
       label="File Upload"
       name="fileUploader"
       record-id={myRecordId}
       onuploadfinished={handleUploadFinished}
       multiple>
      </lightning-file-upload>
</div>

3. Controller Logic:

The Lightning Web Component should be backed by a controller that interacts with the Salesforce and Google Drive APIs to fetch existing documents, list folders, and facilitate document uploads.

import { api, track, wire } from 'lwc';

import getGoogleDriveDocuments from '@salesforce/apex/GoogleDriveIntegrationController.getGoogleDriveFiles';

export default class GoogleDriveIntegration extends LightningElement {
    
    @api recordId;

    @track dataResult = {};
    SFData = {};
        
    @wire(getGoogleDriveDocuments,{folderId:'$recordId'})
    wiredDocuments(result) {
        this.dataResult = result;
        if ( this.dataResult.data) { 
            let parsedData = JSON.parse(JSON.stringify(this.dataResult));
            this.SFData = parsedData.data;
        } else if (error) {
            // Handle error
        }
    }

    // Handle document upload
    handleUploadFinished(event) {
        const uploadedFiles = event.detail.files;
        // Process uploaded files as needed
    }
}

Benefits of Salesforce/Google Drive Integration and LWC

Creating a Lightning Web Component (LWC) to show and interact with documents, especially when integrating with external services like Google Drive, offers several benefits for Salesforce developers and end-users alike:

1. Seamless User Experience:

LWC allows for the creation of modern, responsive, and interactive user interfaces. Users can seamlessly view and interact with documents directly within the Salesforce interface, providing a unified and user-friendly experience.

2. Efficient Document Management:

Users can preview and manage documents without leaving the Salesforce platform, reducing the need to switch between applications. This streamlines document-related workflows and enhances overall efficiency.

3. Real-Time Data Synchronization:

LWC can be designed to dynamically fetch and display real-time document data. Changes made to documents in external services like Google Drive are instantly reflected within the Salesforce component, ensuring users always have the latest information at their fingertips.

4. Integration Capabilities

LWC can seamlessly integrate with external APIs, enabling the incorporation of third-party services like Google Drive. This integration ensures that Salesforce users can leverage the capabilities of external document management systems without leaving the CRM platform.

5. Customization and Extensibility:

LWC provides a modular and extensible framework, allowing developers to customize the document component based on specific organizational requirements. Developers can enhance the component with additional features, such as document versioning, custom metadata display, or integration with other external systems.

6. Enhanced Security Controls:

Salesforce offers robust security features, and LWC can leverage these capabilities to ensure secure document access. Developers can implement access controls, permissions, and encryption to protect sensitive information.

7. Improved Performance:

LWC’s optimized rendering engine and client-side processing contribute to improved performance. Users can experience faster document loading and interactions, enhancing overall responsiveness.

8. Mobile Responsiveness:

LWC components are designed to be responsive, making them suitable for use on various devices, including desktops, tablets, and mobile phones. This ensures that users can access and interact with documents on the go.

Conclusion:

Based on all the relevant data, we can conclude that the ability to harness the full potential of the leading platforms like Salesforce and Google Drive is a strategic advantage in the business competitive landscape. Embracing the power of integration and modern development frameworks lets businesses stay ahead in this dynamic world.

The integration between Salesforce and Google Drive, combined with the capabilities of Lightning Web Components, empowers organizations to streamline their business processes, enhance collaboration, and boost overall productivity. By easily connecting customer data and associated documents within Salesforce, teams can make informed decisions faster and provide a more cohesive and efficient experience for both internal and external stakeholders.