Wednesday, July 8, 2015

Initialize New Fields Fast

Since it is raining today riding my motorcycle is not a good idea. Instead, I solved a simple problem for our Tokyo office.

We already keep both an English and Kanji version of contact first and last names. My Japanese friends wanted new fields for Kanji Title and Department.  They also wanted the new fields initialized based on the current Title and Department fields (but just for accounts in Japan). The new fields are called LocalTitle__c and LocalDepartment__c.

How would you solve this data initialization problem? DataLoader? My Python/SQLForce solution took me 6 minutes and that included testing the script on a sandbox.

import SQLForce 
session = SQLForce.Session("production")  
contactSQL = "SELECT id, title, department FROM Contact WHERE  account.marketCountry__c='Japan' AND ((title<>null and localTitle__c=null) or (department<>null and localDepartment__c=null)) 
updateRecs = [] 
for rec in session.selectRecords(contactSQL):
updateRecs.append( [ rec.id, rec.title, rec.department ]
 if 0 != len(updateRecs):
session.update("Contact", ["localTitle__c", "localDepartment__c" ], updateRecs)
In my case the script updated around 3000 records and took 15 seconds to run. Naturally, the SQLForce library took care of buffering updates into the required packet sizes.

Friday, June 12, 2015

SQLForce Gave Me Time to Ride My Motorcycle.

This morning I was asked to change the "Type" of around 400 Salesforce Campaigns from one type to another.  The type was simply wrong.

No sweat. The following short Python did the job with just a few minutes work on my part, saving my next few hours for a bit of motorcycle riding.
========================================

import SQLForce

if __name__ == '__main__':
    session = SQLForce.Session("production")
   
    updateRecs = []
    campaignSOQL = "SELECT id, owner.alias FROM Campaign WHERE owner.alias='mnakano' AND type='Event-Tradeshow/Conference'"
    for rec in session.selectRecords( campaignSOQL ):
        updateRecs.append( [rec.id, 'Training'])
   
    if len(updateRecs) > 0:
        print("Updating " + str(len(updateRecs)) + " campaign records")
        session.update("Campaign", ["Type"], updateRecs)
    else:
        print("No records to update")

==========================================
I will never understand why so many people put up with DataLoader to do mass updates. Python with SQLForce is free and is so much faster.

You can get the SQLForce Python module for free at www.capstorm.com