Problem: How to create leads from google form responses?
We have a google form and we want to create leads from google form responses.
Solution: -To create lead from google form, follow these steps:-
1. Open google Sheet which contains the responses of google form.
2. Click on the Tools tab and then click on the script editor.
3. New Screen opens like this..
Write a code to insert data in the salesforce.
Here is the sample code that inserts leads from the google sheet. In this code, we are using HTML that is generated from the web to lead functionality.
In this code, we are fetching the data of the last row entered in google responses sheet.
function SendtoSalesforce() { var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); var row = sheet.getLastRow(); var firstname = sheet.getRange(row,2).getValue(); var city = sheet.getRange(row,3).getValue(); var company = sheet.getRange(row,4).getValue(); var email = sheet.getRange(row,6).getValue(); var resp = UrlFetchApp .fetch( 'https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8', { method: 'post', payload: { 'oid' : 'xxxxxxxxxxx ', //salesforce organization id 'first_name' : firstname, //lead field firstname 'last_name' : city, //lead field name last name 'email' : email, //lead field name email 'company' : company, //lead field name company 'city' : city, //lead field name city 'state' : 'testcity' ////lead field name city //we can add number of fields we want and assign value according to requirements } }); Logger.log(resp.getContentText()); }
4. Click on save button or Press ctrl+s to save the code.
5. Click On the Run Button to execute the script. You can execute the script by clicking on the run button or using run tab.
6. The Leads are created successfully in salesforce.You can test in your salesforce account.
After running this code , i m getting the error https://webto.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8 adddress not found.
Thanks for sharing this code. I could see this could be very useful. One question I have that perhaps you would care to answer. Does this code automatically trigger when a row is added or do you have to run it manually? I have noticed in researching this type of task the previous people who documented their efforts were unable to create a trigger to send the data to Salesforce automatically. So, if this is manual, can the code be altered to trigger upon adding a row or is that not possible? Thanks for the feedback!
Very interesting and useful update. Does this trigger automatically? If not, what would need to be done to trigger the script when a new row is added? Thanks for your feedback.
The code can trigger automatically if it is bound to the edit event of the sheet. Check this link and the onEdit(e) call:
https://developers.google.com/apps-script/guides/triggers
Hope it helps.