Google Drive Integration with Salesforce
Salesforce to Google Drive Integration extends the power of G Drive to your CRM. It enables real-time synchronization and complex business process optimization. Our Salesforce CRM Integration Services allows users to spend more time building relationships with customers instead of wasting time on manual tasks. It allows team collaboration in the context of the customer by attaching Sheets, Docs and Slides to an opportunity, account or customer contact in Salesforce. Salesforce to G Drive integration is the need of various industries as Salesforce analyses data and generates reports.
Overview
Our organization heavily relies on Salesforce for managing customer data and generating various reports to monitor sales, customer interactions, and business performance. As a salesforce integration service consultant in USA, we have also been using Google Drive as a centralized platform for document storage and collaboration. To streamline our operations and improve efficiency, we need to integrate Salesforce Reports directly with Google Drive.
Problem Statement
Currently, our sales and marketing teams manually download Salesforce Reports from the system and upload them to Google Drive. This process is time-consuming, prone to human errors, and can lead to data discrepancies. As a prominent Salesforce Consultant in USA, we seek to automate this process by developing a seamless integration between Salesforce and Google Drive.
Google Drive Integration Steps
- create project in google cloud console
- create Credentials
- Install Google Drive library
- create configurations
- create remote site settings
- create new folder in your google drive for store file.
- Http callout for google drive integration
Step 1: Create a Google Cloud Project
first navigate this URL for create project in google cloud console
https://console.cloud.google.com
After redirect this URL you need to login with your Gmail Account
A Google Cloud project is required to use Google Workspace APIs and build Google Workspace add-ons or apps. This project forms the basis for creating, enabling, and using all Google Cloud services, including managing APIs, enabling billing, adding and removing collaborators, and managing permissions.
To create a Google Cloud project:
- In the Google Cloud console, go to Menu > IAM & Admin> Create a Project.
- In the Project Namefield, enter a descriptive name for your project.
Step 2: Create Access Credentials
In the Google Cloud console, go to Menu > APIs & Services > Credentials.
Click Create Credentials > OAuth client ID.
In the Name field, type a name for the credential. This name is only shown in the Google Cloud console.
Add authorized URIs related to your app:
- Client-side apps (JavaScript)–Under Authorized JavaScript origins, click Add URI. Then, enter a URI to use for browser requests. This identifies the domains from which your application can send API requests to the OAuth 2.0 server.
- Server-side apps (Java, Python, and more)–Under Authorized redirect URIs, click Add URI. Then, enter an endpoint URI to which the OAuth 2.0 server can send responses.
- Click Create. The OAuth client created screen appears, showing your new Client ID and Client secret.
- Click OK. The newly created credential appears under OAuth 2.0 Client IDs.
Step 3: Install Google Drive Library
Click on library option
In serach bar find google drive API
Click on Google drive API, then click Enable button
Step 4: Create Configurations
Click on Quick find box -> Search custom setting -> New
then create custom fields given by image
Step 5: Create Remote Site Settings
Click on Quick find box -> Search remote site settings -> New remote site
You need to create two custom setting and add this URL
Step 6: Create New Folder in Your Google Drive for Store File
Go to your Google Drive -> click on New -> New Folder
Step 7: HTTP Callout for Google Drive Integration
First time authorize your Gmail account using redirect URL
String key = ‘use your client id here’;
String uri= ‘use your authorize URL here’;
String authuri = ”;
authuri = ‘https://accounts.google.com/o/oauth2/auth?’+
‘client_id=’+key+
‘&response_type=code’+
‘&scope=https://www.googleapis.com/auth/drive’+
‘&redirect_uri=’+uri+
‘&state=security_token%3D138r5719ru3e1%26url%3Dhttps://oa2cb.example.com/myHome&’+
‘&login_hint= jsmith@example.com&’+
‘access_type=offline’+
‘&prompt=consent’;
HTTP callout for first time get access token
HttpRequest req = new HttpRequest();
req.setMethod(‘POST’);
req.setEndpoint(‘https://accounts.google.com/o/oauth2/token’);
req.setHeader(‘content-type’, ‘application/x-www-form-urlencoded’);
//String messageBody =’GoogleSheetIntegration’+’&grant_type=client_credentials’;
String messageBody = ‘code=’+code+’&client_id=’+clientId+’&client_secret=’+clientsecretId+’&redirect_uri=’+redirectURL+’&grant_type=authorization_code’;
req.setHeader(‘Content-length’, String.valueOf(messageBody.length()));
req.setBody(messageBody);
req.setTimeout(60*1000);
system.debug(‘Access token request–‘+req);
Http h = new Http();
HttpResponse res = h.send(req);
HTTP callout for Refresh access token
Strrng clientId = ‘use your client id’ ;
Strrng clientSecretId = ‘use your client Secret id’;
String refreshToken = ‘use refresh token it is given when first time get access token’;
String bodyRequest = ‘client_id=’ + clientId;
bodyRequest += ‘&client_secret=’ + clientSecretId;
bodyRequest += ‘&refresh_token=’ + refreshToken;
bodyRequest += ‘&grant_type=refresh_token’;
Http http = new Http();
HttpRequest req = new HttpRequest();
req.setMethod(‘POST’);
req.setEndpoint(‘https://accounts.google.com/o/oauth2/token’)
req.setHeader(‘Content-Type’, ‘application/x-www-form-urlencoded’);
req.setBody(bodyRequest);
// Send the HTTP request
HttpResponse res = http.send(req);
HTTP callout for Export file in Google drive
Report report = [SELECT Id,DeveloperName,Name FROM Report where DeveloperName = ‘New_Contacts_Accounts_Report_1_UmO’];
ApexPages.PageReference reportPage = new ApexPages.PageReference(‘/’+report.Id+’?excel=1&isdtp=p1′);
Messaging.EmailFileAttachment csvAttachment = new Messaging.EmailFileAttachment();
csvAttachment.setFileName(‘Report.xls’);
csvAttachment.setBody(reportPage.getContent());
csvAttachment.setContentType(‘application/vnd.ms-excel’);
String fileName = ‘GoogleDriveReportForAccount.xls’;
Blob fileData = reportPage.getContent();
String parentFolderId = ‘XXXXXXXXXXXXXXXXXXXXXXXX’; // use your google drive folder Id here
HTTP callout for create excel file
String endpointUrl = ‘https://www.googleapis.com/drive/v3/files’;
HttpRequest request = new HttpRequest();
request.setEndpoint(endpointUrl);
request.setMethod(‘POST’);
request.setHeader(‘Content-Type’, ‘application/json’);
request.setHeader(‘Authorization’, ‘Bearer ‘ + accessToken);
Map<String, Object> requestBody = new Map<String, Object>();
requestBody.put(‘name’, fileName);
requestBody.put(‘mimeType’, ‘application/vnd.ms-excel’);
requestBody.put(‘parents’, new List{parentFolderId}); //
String requestBodyJson = JSON.serialize(requestBody);
request.setBody(requestBodyJson);
HttpResponse response = new Http().send(request);
HTTP callout for upload file in your google drive
Note: fileId available in create file HTTP callout response
String endpointUrl = ‘https://www.googleapis.com/upload/drive/v3/files/’ + fileId + ‘?uploadType=media’;
HttpRequest request = new HttpRequest();
request.setEndpoint(endpointUrl);
request.setMethod(‘PATCH’);
request.setHeader(‘Content-Type’, ‘application/vnd.ms-excel’);
request.setHeader(‘Authorization’, ‘Bearer ‘ + accessToken);
request.setBodyAsBlob(fileData);
HttpResponse response = new Http().send(request);
related blog