Last active
December 13, 2018 03:53
-
-
Save glamp/bba94c326399aa7873cb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(RForcecom) | |
sfSessionCredentials <- NULL | |
connectToSalesForce <- function() { | |
if (! is.null(sfSessionCredentials)) { | |
return | |
} | |
# grab the credentials from Environment Variables | |
username <- Sys.getenv("SF_USERNAME") # "your salesforce username" | |
password <- Sys.getenv("SF_PASSWORD") # "password..." | |
securityToken <- Sys.getenv("SF_SECURITYTOKEN") # "security token..." | |
passWithToken <- paste0(password, securityToken) # need this for RForcecom | |
instanceURL <- Sys.getenv("SF_URL") # "https://na17.salesforce.com/ or whatever sandbox is" | |
passWithToken <- paste0(password, securityToken) # need this for RForcecom | |
apiVersion <- "27.0" | |
sfSessionCredentials <<- rforcecom.login(username, passWithToken, instanceURL, apiVersion) | |
} | |
fetchRecordById <- function(sfObj, fields, Id) { | |
# this will only reconnect to Salesforce if something is wrong with the session | |
tryCatch({ | |
rforcecom.retrieve(sfSessionCredentials, sfObj, fields=fields, id=Id) | |
}, error = function(e) { | |
print(e) | |
sfSessionCredentials <<- NULL | |
connectToSalesForce() | |
rforcecom.retrieve(sfSessionCredentials, sfObj, fields=fields, id=Id) | |
}) | |
} | |
# fields <- c("Id", "Name", "Linkedin__c", "Landing_page_url__c", "Days_since_created__c") | |
# fetchRecordById("Lead", fields, "00Qo000000JZ5cx") | |
# Id Name Landing_Page_URL__c Days_Since_Created__c | |
# 1 00Qo000000JZ5cxEAD Tommy Wheydon www.yhat.com/signup 1.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is just a placeholder for my actual lead scoring model | |
leadScorer <- function(data) { | |
runif(nrow(data)) | |
} | |
source("./salesforce.R") | |
model.require <- function() { | |
library(plyr) | |
} | |
# expected input | |
# single record: | |
# { "LeadId": ["00Qo000000JZ5cx"] } | |
# multiple records: | |
# { "LeadId": ["00Qo000000JZ5cx", "00Qo000000JZ5cx", ... ] } | |
model.predict <- function(data) { | |
ddply(data, .(LeadId), function(lead) { | |
fields <- c("Id", "Name", "Linkedin__c", "Landing_page_url__c", "Landing_page_type__c", | |
"Days_since_created__c") | |
record <- fetchRecordById("Lead", fields, lead$LeadId) | |
record$score <- leadScorer(record) | |
record | |
}) | |
} | |
testcase <- data.frame( | |
LeadId="00Qo000000JZ5cx" | |
) | |
model.predict(testcase) | |
# LeadId Id Name Landing_Page_URL__c Landing_Page_Type__c | |
# 1 00Qo000000JZ2xi 00Qo000000JZ2xiEAD Tom Dee www.yhat.com/posts/rodeo-native.html Internal | |
# Days_Since_Created__c score | |
# 1 3.0 0.6727 | |
testcase <- data.frame( | |
LeadId=c("00Qo000000JZ5cx", "00Qo000000JZ2xi") | |
) | |
model.predict(testcase) | |
# LeadId Id Name Landing_Page_URL__c Landing_Page_Type__c | |
# 1 00Qo000000JZ2xi 00Qo000000JZ2xiEAD Tom Dee www.yhat.com/posts/rodeo-native.html Internal | |
# 2 00Qo000000JZ5cx 00Qo000000JZ5cxEAD Gertrud Phillips www.yhat.com/signup Internal | |
# Days_Since_Created__c score | |
# 1 3.0 0.6727 | |
# 2 1.0 0.7209 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for sharing!