logo
Integrate Google Form with Salesforce using Google Apps Script

Jayesh Jain

Mar 8, 2023

3 min read

Integrate Google Form with Salesforce using Google Apps Script

Let’s see how we can send Google Form submission data directly into Salesforce using Google Apps Script. Google Forms is a powerful tool for collecting data and surveys, and integrating it with Salesforce allows you to automatically store leads, contacts, and more.


Step 1: Create App in Salesforce

First, create a Salesforce Connected App:

  • Navigate: Setup → App → App Manager → New Connected App
  • Fill in required details and enable OAuth settings.
Create App in Salesforce

This app will provide the client ID and secret needed for authentication in Google Apps Script.


Step 2: Create a new Google Form

  1. Create your Google Form and add required fields.
  2. Click the three-dot menu → Script Editor.
Create Google Form
  1. Paste the following sample Apps Script code:
1//Authorize & get Token 2const auth_url = "https://login.salesforce.com/services/oauth2/authorize"; 3const token_url = "https://login.salesforce.com/services/oauth2/token"; 4 5function onSubmit() { 6 try { 7 var form = FormApp.getActiveForm(); 8 var allResponses = form.getResponses(); 9 var latestResponse = allResponses[allResponses.length - 1]; 10 var response = latestResponse.getItemResponses(); 11 var payload = {}; 12 for (var i = 0; i < response.length; i++) { 13 var question = response[i].getItem().getTitle(); 14 var answer = response[i].getResponse(); 15 payload[question] = answer; 16 } 17 18 const salesforceTokenData = getSalesforceToken(); 19 const salesforce_payload = Utilities.jsonStringify({ 20 'FirstName': payload.FirstName, 21 'LastName': payload.LastName, 22 'Phone': payload.Phone, 23 'Email': payload.Email, 24 'Description': payload.Description 25 }); 26 27 var contentType = "application/json; charset=utf-8"; 28 var feedUrl = salesforceTokenData.instance_url + "/services/data/v48.0/sobjects/Contact" + "?_HttpMethod=POST"; 29 var response = UrlFetchApp.fetch(feedUrl, { 30 method: "POST", 31 headers: { "Authorization": "Bearer " + salesforceTokenData.access_token }, 32 payload: salesforce_payload, 33 contentType: contentType 34 }); 35 36 } catch (err) { 37 Logger.log("Error submitting data to Salesforce: " + err); 38 throw err; 39 } 40} 41 42function getSalesforceToken() { 43 const grant_type = 'password'; 44 const client_id = '<client id>'; 45 const client_secret = '<client secret>'; 46 const salesforce_username = encodeURI('<salesforce login user>'); 47 const salesforce_password = encodeURI('<password+security token>'); 48 49 var payload = { 50 'grant_type': grant_type, 51 'client_id': client_id, 52 'client_secret': client_secret, 53 'username': salesforce_username, 54 'password': salesforce_password 55 }; 56 57 var options = { 58 'method': 'post', 59 'payload': payload 60 }; 61 62 var results = UrlFetchApp.fetch(token_url, options); 63 var data = JSON.parse(results.getContentText()); 64 return { 'access_token': data.access_token, 'instance_url': data.instance_url }; 65}

Note: Always include mandatory fields in your form (First Name, Last Name, Email) for Salesforce Contact object validation.


Step 3: Create Trigger in Google Apps Script

To ensure the onSubmit() runs when the form is submitted:

  • In Script Editor → Edit → Current Project’s Triggers → Add Trigger
  • Select onSubmit, event type: On form submit
Google App Script Trigger

Step 4: Test Integration

  • Submit a test form entry.
  • Check Salesforce Contact object to confirm the record is created.
Salesforce Contact

Benefits of Direct Integration

  • Eliminate manual data entry
  • Seamless lead capture from Google Forms
  • Customizable via Apps Script for complex workflows
  • Can extend integration to e-commerce, ERP, or other systems

Conclusion

Integrating Google Forms with Salesforce via Google Apps Script is simple and powerful. For advanced or custom scenarios, Tirnav Solutions can help with Salesforce Apex, LWC, Aura, VisualForce, Triggers, and Workflow automation.

Share this article

Inspired by This Blog?

JJ

Jayesh Jain

Jayesh Jain is the CEO of Tirnav Solutions and a dedicated business leader defined by his love for three pillars: Technology, Sales, and Marketing. He specializes in converting complex IT problems into streamlined solutions while passionately ensuring that these innovations are effectively sold and marketed to create maximum business impact.

Need Custom Salesforce Integration?

Reach out to Tirnav Solutions for advanced Google Forms → Salesforce integration solutions.

Let’s Talk

Related Posts

[email protected]
+919860571681
Download Brochure
logo
logo
GoogleClutch