3

I'm trying to build a form using Google Apps script that send form data (text+file Attachment) to a Google Spreadsheet and then this Spreadsheet has a Google Apps Script that send form data to Salesforce as a lead, via a web-to-lead POST method.

Part 1) HTML Form to Google Spreadsheet (this works)

A Google Apps script web app (Code.gs + Form.html + Thanks.html) has an HTML form with text fields and a file upload mechanism. On submit this form adds a new row to the Google Spreadsheet with form data and the URL of the uploaded file.

As per this Tutorial: Form and file upload with htmlService and app script not working

Part 2) Google Spreadsheet to Salesforce using Web-to-Lead POST method (Not working)

In the above mentioned Google Spreadsheet, the following code.gs is adding based on a Google Developers' post (http://googleappsdeveloper.blogspot.sg/2012/03/integrating-google-docs-with.html):

It essentially takes the row data from the Spreadsheet and pushes it to the Web to Lead service which creates a new lead in Salesforce:

Code.gs (of the spreadsheet)

    function SendtoSalesforce() {
      var sheet = SpreadsheetApp.openById("--Spreadsheet ID--").getActiveSheet();
      var row = sheet.getLastRow();
      var firstname = sheet.getRange(row, 2).getValue();
      var lastname = sheet.getRange(row, 3).getValue();
      var email = sheet.getRange(row, 4).getValue();
      var company = sheet.getRange(row, 5).getValue();
      var description = sheet.getRange(row, 6).getValue();
      var resp = UrlFetchApp
          .fetch(
              'https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8',
              {
                method: 'post',
                payload: {
                  'oid' : '--SFDC Org ID--',
                  'first_name' : firstname,
                  'last_name' : lastname,
                  'email' : email,
                  'company' : company,
                  'description' : description,
                  'external' : '1'
                }
              });
      Logger.log(resp.getContentText());
    }

I then added a trigger for "SendtoSalesforce" that fires on a "form submit" event.

Issue The Leads get captured inside the Google Spreadsheet but don't get passed to Salesforce. It's only after I manually run the above Code.gs script manually that the data is passed through to Salesforce successfully.

I would appreciate some help in achieving this transfer from Google Scripts to Salesforce automatically after each form submit event.

I tried adding a Time based trigger in combination with On edit, on change, on open trigger but they behave erratically and resubmit the entire sheet's rows every minute/hour leading to duplicates in Salesforce.

I would appreciate some help in getting the code to run each time automatically, avoiding duplications in SFDC.

Community
  • 1
  • 1
RDMD
  • 45
  • 1
  • 7
  • 1. please clean up code explanation duplication. 2. clearly explain why part two isnt working. 3. clearly explain the issue about duplicates. 4. dont include the code for the 1st part, just explain that the webapp adds a new row to the spreadsheet. – Zig Mandel Aug 25 '15 at 02:18
  • @ZigMandel Cleaned up the post as requested! Thanks – RDMD Aug 25 '15 at 02:33

1 Answers1

1

you are misunderstanding the documentation in several areas:

  1. onFormSubmit triggers only for entries submitted throught google forms, not a random post from a webapp.

  2. the other triggers (onEdit onChange) happen only while a user inputs data through the web sheets page, not by api calls.

your solution is to modify your webapp so that it also calls your code that processes the new row added.

alternatively (and more robust in general) is to create a recurring time trigger (say every 10 minutes) that scans the sheet starting from the last scanned row and processes the new rows. use script properties to save the last processed row.

Zig Mandel
  • 19,571
  • 5
  • 26
  • 36
  • Thanks! I'd used Time triggers but they didn't have any script properties. I'm not hands on with the code as I've pieced it from 2 separate scripts, do you have any sample code on how to approach the proposed solution of the time trigger? – RDMD Aug 25 '15 at 03:10
  • look at the docs for both triggers and storage (properties service). make a separate question including your code and what didnt work. – Zig Mandel Aug 25 '15 at 03:15
  • I couldn't make much sense of the documentation as I don't write code, mate. Whatever I've gotten so far is by integrating code from 2 different sources. Anyway, thanks for the pointers. – RDMD Aug 25 '15 at 03:31
  • sorry but imagine if we started helping non coders, this service would stop existing by no longer being useful to coders. the question and answer must both be for coders. – Zig Mandel Aug 25 '15 at 03:34
  • I see. Although based on my through search of most common forums online, this would be a huge addition to the common pool of people looking for this solution. Not to sound self-serving, but StackOverflow, Salesforce and Google's forums are littered with requests that could be solved by resolving this. Problems solved = custom HTML form to spreadsheet, file upload to spreadsheet, spreadsheet via POST method to any other destination. – RDMD Aug 25 '15 at 03:35
  • No because it just becomes an unsustainable free coding service. since non coders cant yet adapt a solution to their needs, specific answers wouldn't help to solve a similar but not identical problem (like it just happened to you) – Zig Mandel Aug 25 '15 at 03:37
  • IMHO, this solves several individual problems and 1 integrated solution problem at one shot. – RDMD Aug 25 '15 at 03:39
  • not so. your issue is very specific to your webapp. people can write a webapp like that in many ways. and you need minimal coding knowledge to adapt something to your needs. in your case its basically a single function call you are missing somewhere, which calls the 2nd operation after the 1st one happens. – Zig Mandel Aug 25 '15 at 03:41
  • I respect that you're a Google Dev Expert and hence have more experience in these matters. But I don't agree that this is very specific case. I don't know if I resolve this but when I do, I'll be sure to put up a post and open up code that helps people sort out these generic issues people face everyday. Much like the brilliant Google Dev post itself that made it simple for a non coder like me to come this far. – RDMD Aug 25 '15 at 03:45
  • you can keep asking if you get stuck, but do try a few things with the new info you have and post a separate q like suggested above. find how the 1st code works, where it finally adds that row then try alpending your 2nd code there. – Zig Mandel Aug 25 '15 at 03:51