First, load the salesforcer
package and login.
suppressWarnings(suppressMessages(library(dplyr)))
library(salesforcer)
sf_auth()
For really large inserts, updates, deletes, upserts, queries you can just add “api_type” = “Bulk” to most functions to get the benefits of using the Bulk API instead of the SOAP or REST APIs. Here is the difference in using the REST API vs. the Bulk API to do an insert:
n <- 2
new_contacts <- tibble(FirstName = rep("Test", n),
LastName = paste0("Contact-Create-", 1:n))
# REST
rest_created_records <- sf_create(new_contacts, object_name="Contact", api_type="REST")
rest_created_records
#> # A tibble: 2 x 3
#> id success errors
#> <chr> <lgl> <list>
#> 1 0036A00000SnwvsQAB TRUE <list [0]>
#> 2 0036A00000SnwvtQAB TRUE <list [0]>
# Bulk
bulk_created_records <- sf_create(new_contacts, object_name="Contact", api_type="Bulk 1.0")
bulk_created_records
#> # A tibble: 2 x 4
#> Id Success Created Error
#> <chr> <chr> <chr> <lgl>
#> 1 0036A00000SnwvjQAB true true NA
#> 2 0036A00000SnwvkQAB true true NA
There are some differences in the way each API returns response information; however, the end result is exactly the same for these two calls. Also, note that this package utilizes the Bulk 2.0 API for most bulk calls except for bulk queries since Salesforce has not yet implemented it in 2.0.
Here is a simple workflow of adding, querying, and deleting records using the Bulk API.
# just add api_type="Bulk" to most calls!
# create bulk
object <- "Contact"
n <- 2
new_contacts <- tibble(FirstName = rep("Test", n),
LastName = paste0("Contact-Create-", 1:n))
created_records <- sf_create(new_contacts, object_name=object, api_type="Bulk 1.0")
created_records
#> # A tibble: 2 x 4
#> Id Success Created Error
#> <chr> <chr> <chr> <lgl>
#> 1 0036A00000SnwvxQAB true true NA
#> 2 0036A00000SnwvyQAB true true NA
# query bulk
my_soql <- sprintf("SELECT Id,
FirstName,
LastName
FROM Contact
WHERE Id in ('%s')",
paste0(created_records$Id , collapse="','"))
queried_records <- sf_query(my_soql, object_name=object, api_type="Bulk 1.0")
queried_records
#> # A tibble: 2 x 3
#> Id FirstName LastName
#> <chr> <chr> <chr>
#> 1 0036A00000SnwvxQAB Test Contact-Create-1
#> 2 0036A00000SnwvyQAB Test Contact-Create-2
# delete bulk
deleted_records <- sf_delete(queried_records$Id, object_name=object, api_type="Bulk 1.0")
deleted_records
#> # A tibble: 2 x 4
#> Id Success Created Error
#> <chr> <chr> <chr> <lgl>
#> 1 0036A00000SnwvxQAB true false NA
#> 2 0036A00000SnwvyQAB true false NA