Wrapper methods for salesforce-python-toolkit

Just include this file to work on the Salesforce API’s.

Pre-requisite: Must have salesforce-python-toolkit installed on server.

#!/etc/bin/python
import re
from sforce.enterprise import SforceEnterpriseClient

def _getSfClient():
    sfClient = SforceEnterpriseClient(wsdl_file)
    sfClient.login(dict_sf_credentials['login'], dict_sf_credentials['password'], dict_sf_credentials['token'])
    return sfClient

##############################################################
# sf_query(sfClient, strQuery)
# Description: This function searches for salesforce objects.
# Note: You can get isDeleted in the search results but cannot filter using isDeleted
# returns: result object from the query
#
def sf_query(sfClient, strQuery):
    result = sfClient.query(strQuery)
    records = getattr(result, 'records', [])
    return records

##############################################################
# sf_queryAll(sfClient, strQuery)
# Description: This function filters records by isDeleted flag
# True/False. Otherwise it is same as query
# Note: You can get isDeleted in the search results but cannot filter using isDeleted
# returns: result object from the query
#
def sf_queryAll(sfClient, strQuery):
    result = sfClient.queryAll(strQuery)
    records = getattr(result, 'records', [])
    return records

##############################################################
# sf_queryMore(sfClient, strQuery, batchSize = 1000)
# Description: This function is keeps querying until all records are returned. 
# returns: result object from the query
#
def sf_queryMore(sfClient, strQuery, batchSize = 1000):
    queryOptions = sfClient.generateHeader('QueryOptions')
    queryOptions.batchSize = batchSize
    sfClient.setQueryOptions(queryOptions)
    records = []
    result = sfClient.query(strQuery)
    records.extend(result.records)
    done = result.done
    while (not done):
        result = sfClient.queryMore(result.queryLocator)
        records.extend(result.records)
        done = result.done
    return records

##############################################################
# sf_update(sfClient, objType, objId, dictData)
# Description: This function updates the data for the objectId. 
# returns: True/False
#
def sf_update(sfClient, objType, objId, dictData = None):
    obj = sfClient.generateObject(objType)
    obj['Id'] = objId
    for key in dictData:
        obj[key] = dictData[key]
    result = sfClient.update(obj)
    if(result.success == True):
        return True
    else:
        return False

##############################################################
# sf_delete(sfClient, objId)
# Description: This function deletes the specified objectId
# returns: True/False
#
def sf_delete(sfClient, objId):
    result = sfClient.delete(objId)
    return result
    
##############################################################
# sf_undelete(sfClient, objId)
# Description: This function undeletes the specified objectId
# returns: True/False
#
def sf_undelete(sfClient, objId):
    result = sfClient.undelete(objId)
    return result

##############################################################
# sf_sendEmail(sfClient, toAddress, subject, plainTextBody)
# Description: This function sends out emails
# returns: True/False
#
def sf_sendEmail(sfClient, toAddress, subject, plainTextBody):
    email = sfClient.generateObject('SingleEmailMessage')
    email.toAddresses = toAddress
    email.subject = subject
    email.plainTextBody = plainTextBody
    result = sfClient.sendEmail([email])
    return result

##############################################################
# sf_sendMassEmail(sfClient, recipientId, templateId)
# Description: This function sends out emails
# returns: True/False
#
def sf_sendMassEmail(sfClient, recipientId, templateId):
    email = sfClient.generateObject('MassEmailMessage')
    email.targetObjectIds = ((recipientId))
    email.templateId = templateId
    result = sfClient.sendEmail([email])
    return result
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s