Created
April 30, 2014 17:58
-
-
Save wadewegner/9b9fbe55b491ffe62c81 to your computer and use it in GitHub Desktop.
Code used to run the Salesforce Bulk API to perform an UPDATE using Python
This file contains hidden or 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
import requests | |
import xml.etree.ElementTree as ET | |
def login(userName, password): | |
request = u"""<?xml version="1.0" encoding="utf-8" ?> | |
<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"> | |
<env:Body> | |
<n1:login xmlns:n1="urn:partner.soap.sforce.com"> | |
<n1:username>""" + userName + """</n1:username> | |
<n1:password>""" + password + """</n1:password> | |
</n1:login> | |
</env:Body> | |
</env:Envelope>""" | |
encoded_request = request.encode('utf-8') | |
url = "https://login.salesforce.com/services/Soap/u/30.0" | |
headers = {"Content-Type": "text/xml; charset=UTF-8", | |
"SOAPAction": "login"} | |
response = requests.post(url=url, | |
headers = headers, | |
data = encoded_request, | |
verify=False) | |
return unicode(response.text) | |
def createJob(instance, sessionId, operation, object, contentType): | |
request = u"""<?xml version="1.0" encoding="UTF-8"?> | |
<jobInfo xmlns="http://www.force.com/2009/06/asyncapi/dataload"> | |
<operation>""" + operation + """</operation> | |
<object>"""+ object + """</object> | |
<contentType>""" + contentType + """</contentType> | |
</jobInfo>""" | |
encoded_request = request.encode('utf-8') | |
url = "https://" + instance + ".salesforce.com/services/async/30.0/job" | |
headers = {"X-SFDC-Session": sessionId, | |
"Content-Type": "application/xml; charset=UTF-8"} | |
response = requests.post(url=url, | |
headers = headers, | |
data = encoded_request, | |
verify=False) | |
return unicode(response.text) | |
def addBatch(instance, sessionId, jobId, objects): | |
request = u"""<?xml version="1.0" encoding="UTF-8"?> | |
<sObjects xmlns="http://www.force.com/2009/06/asyncapi/dataload"> | |
""" + objects + """ | |
</sObjects>""" | |
encoded_request = request.encode('utf-8') | |
url = "https://" + instance + ".salesforce.com/services/async/30.0/job/" + jobId + "/batch" | |
headers = {"X-SFDC-Session": sessionId, | |
"Content-Type": "application/xml; charset=UTF-8"} | |
response = requests.post(url=url, | |
headers = headers, | |
data = encoded_request, | |
verify=False) | |
return unicode(response.text) | |
def closeJob(instance, sessionId, jobId): | |
request = u"""<?xml version="1.0" encoding="UTF-8"?> | |
<jobInfo xmlns="http://www.force.com/2009/06/asyncapi/dataload"> | |
<state>Closed</state> | |
</jobInfo>""" | |
encoded_request = request.encode('utf-8') | |
url = "https://" + instance + ".salesforce.com/services/async/30.0/job/" + jobId | |
headers = {"X-SFDC-Session": sessionId, | |
"Content-Type": "application/xml; charset=UTF-8"} | |
response = requests.post(url=url, | |
headers = headers, | |
data = encoded_request, | |
verify=False) | |
return unicode(response.text) |
This file contains hidden or 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
import requests | |
import xml.etree.ElementTree as ET | |
import async | |
from simple_salesforce import Salesforce | |
def createObjectXml(objectXml, userName, password, securityToken): | |
sf = Salesforce(username=userName, password=password, security_token=securityToken) | |
bylines = sf.query_all("SELECT Id, Name, Title__c, Published_Link__c FROM Byline__c WHERE Published_Link__c != ''") | |
records = bylines["records"] | |
counter = 0 | |
errors = '' | |
for record in records: | |
counter = counter + 1 | |
id = record["Id"] | |
name = record["Name"] | |
title = record["Title__c"] | |
publishedLink = record["Published_Link__c"] | |
twitterCount = 0 | |
facebookShares = 0 | |
linkedInCount = 0 | |
try: | |
twitterJson = requests.get('http://cdn.api.twitter.com/1/urls/count.json?callback=?&url=' + publishedLink).json() | |
facebookJson = requests.get('http://graph.facebook.com/' + publishedLink).json() | |
linkedInJson = requests.get('http://www.linkedin.com/countserv/count/share?url=' + publishedLink + '&format=json').json() | |
except ValueError: | |
errors = errors + name + " " | |
continue | |
if 'count' in twitterJson: | |
twitterCount = twitterJson["count"] | |
if 'shares' in facebookJson: | |
facebookShares = facebookJson["shares"] | |
if 'count' in linkedInJson: | |
linkedInCount = linkedInJson["count"] | |
data = {"Twitter_Mentions__c": twitterCount, "Facebook_Likes__c": facebookShares, "LinkedIn_Shares__c": linkedInCount} | |
print "%s - %s: %d tweets / %d shares / %d linked" % (name, title, twitterCount, facebookShares, linkedInCount) | |
objectXml = objectXml + u"""<sObject> | |
<Id>""" + id + """</Id> | |
<Twitter_Mentions__c>""" + str(twitterCount) + """</Twitter_Mentions__c> | |
<Facebook_Likes__c>""" + str(facebookShares) + """</Facebook_Likes__c> | |
<LinkedIn_Shares__c>""" + str(linkedInCount) + """</LinkedIn_Shares__c> | |
</sObject>""" | |
print "Errors: " + errors | |
return objectXml | |
userName = "" | |
password = "" | |
securityToken = "" | |
instance = "" | |
print ">> Login" | |
loginXmlResponse = async.login(userName, password + securityToken) | |
loginXmlRoot = ET.fromstring(loginXmlResponse) | |
sessionId = loginXmlRoot[0][0][0][4].text | |
print sessionId | |
print ">> Create Job" | |
jobXmlResponse = async.createJob(instance, sessionId, "update", "Byline__c", "XML") | |
jobXmlRoot = ET.fromstring(jobXmlResponse) | |
jobId = jobXmlRoot[0].text | |
print jobId | |
print ">> Add Batch" | |
objectXml = "" | |
objectXml = createObjectXml(objectXml, userName, password, securityToken) | |
batchXmlResponse = async.addBatch(instance, sessionId, jobId, objectXml) | |
print ">>> Close Job" | |
closeXmlResponse = async.closeJob(instance, sessionId, jobId) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment