0
votes

Im trying to connect data in google sheet to salesforce

function onEdit(e){
  SendtoSalesforce();

}

function SendtoSalesforce() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var row = sheet.getLastRow();
  var ldap = sheet.getRange(row,2).getValue();
  var firstname = sheet.getRange(row,5).getValue();
  var lastname = sheet.getRange(row,6).getValue();
  var company = sheet.getRange(row,3).getValue();
  var companyurl = sheet.getRange(row,4).getValue();
  var email = sheet.getRange(row,8).getValue();
  var title = sheet.getRange(row,7).getValue();
  var resp = UrlFetchApp
      .fetch(
          'https://webto.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8',
          {
            method: 'post',
            payload: {
               'oid' : 'xxxxxxxxx',     //salesforce organization id 
              'Ldap_c' : ldap,
              'first_name' : firstname,            //lead field firstname 
              'last_name' : lastname,                    //lead field name last name
              'email' : email,                         //lead field name email
              'company' : company, 
              'Website' : companyurl,
              'Contact_Title_Role__c': title
}
          });

  Logger.log(resp.getContentText());
}

Error You do not have permission to call UrlFetchApp.fetch. Required permissions: https://www.googleapis.com/auth/script.external_request at SendtoSalesforce(Code:16) at onEdit(Code:2)

1
If you want to run the script by the OnEdit event trigger, how about trying Installable Triggers? - Tanaike
To build on @Tanaike 's suggestion, using simple triggers (i.e. having them declared in the code itself) runs with different permissions, your onEdit() will not have permissions for some tools (like UrlFetchApp.fetch). Installable Triggers should work correctly. - AMolina

1 Answers

0
votes

I just opened the related documentation for UrlFetchApp.

It contains the following paragraph:

Scripts that use this method require authorization with one or more of the following scopes: https://www.googleapis.com/auth/script.external_request

You might need to configure a scope in oauthScopes as shown here.

I hope this helps!