Beatbox/Python - Having issues updating multiple records in Salesforce

2.5k Views Asked by At

I've got beatbox working well with an SFDC developer account. I'm pushing to Salesforce to create custom objects but it's updating one at a time. Is there a way to push 200 changes at once to limit the amount of api calls i'm making?

The script I run downloads a batch of leads and then processes them through new_Alert() and then uploads them one by one so I'm hitting my api limits pretty quickly.

Below are the two scripts I'm running, I've cut out most of the 'work' to make it a bit more easy to read.

def new_Alert(id1,login,pw):
    svc = beatbox.Client()
    svc.login(login, pw)  
        object_dict = {
        'type': 'new_Alert',
        'Lead__c':  str(id1) }
        results = svc.create(object_dict)
        print results


def pushAlerts(login,pw):
    sf = beatbox._tPartnerNS
    svc = beatbox.Client() 
    svc.login(login, pw) 
    qr = svc.query("select Id, OwnerId from Lead")

    for rec in qr[sf.records:]:
        type = rec[0]
        id1 = rec[1]
        owner = rec[2]
        new_Alert(type,id1,login)
    while (str(qr[sf.done]) == 'false'):
        qr = svc.queryMore(str(qr[sf.queryLocator]))
        for rec in qr[sf.records:]:
            type = rec[0]
            id1 = rec[1]
            new_Alert(type,id1,login)
1

There are 1 best solutions below

1
On

the create function can take a list of upto 200 records (dictionaries), you just to accumulate them up and send a batch once you've collected 200.

here's an example of creating 2 account records at once and checking the result.

def create(self):
    a = { 'type': 'Account',
        'Name': 'New Account',
        'Website': 'http://www.pocketsoap.com/' }
    b = { 'type' : 'Account',
        'Name' : 'Another account',
        'Website' : 'https://github.com/superfell/Beatbox' }

    sr = svc.create([a,b])

    self.printresult(sr[0])
    self.printresult(sr[1])

def printresult(self, sr):
    if str(sr[sf.success]) == 'true':
        print "id " + str(sr[sf.id])
    else:
        print "error " + str(sr[sf.errors][sf.statusCode]) + ":" + str(sr[sf.errors][sf.message])