
Currently we are carrying out a piece of integration work between Drupal and Salesforce. Drupal is responsible for pulling the data from Salesforce and this achieved by calling the SOQL api via a webservice. The data is returned in a SOAP format. This article assumes you are using this salesforce client
http://sourceforge.net/project/showfiles.php?group_id=96634&package_id=166314
SOQL stands for sforce object query language and is the mechanism for retrieving data from salesforce's database. SOQL has a number of interesting limitations when compared with a standard query language such as SQL. It doesn't support complex queries meaning that table joins can not be performed, the maximum length of any query is 10000 characters and the number of records returned in a single query is limited to at most 2000 records. The actual number of records returned depends on nature of the fields being returned. This is done to ensure that SOAP messages don’t become large.
We need to create a new Salesforce object and pass in location of partner.wsdl.xml (the WSDL file we are using) and then log onto the system:
$file = $filelocation.‘partner.wsdl.xml'
$conn = new salesforce($file);
$loginResult = $conn->login($username, $password);
SOQL queries are similar in structure to SQL:
$soql = 'SELECT c.Id, c.Career_Interest__c FROM Contact c WHERE Id = '$sfId'"; A query is trigger in the following maner:
$result = $conn->query($soql);Once the data has been returned the data needs to be manipulated to make it easier to process. The first test is to see whether we have in fact retrieved any records. The records returned can be found here:
$records = $ result ['result']['records'];Once we have determined if records are present the way we treat the data that has been returned depends on whether we expect a single row or multiple rows to be returned.
In case of a single returned value we always want to make sure that we only process the first record:
$records = $records [0]->values;The values returned by the query can then be easily accessed as we have an array to process:
$id = $records[‘Id’];If we expect multiple records to be returned we iterate through the returned object and process all the items. Returned values are accessed via the values attribute of the returned object:
foreach ($records as $record) {
$id = $record->values[‘Id’];
}Given the limit that salesforce places on the number of records that can be returned it certain cases it maybe necessary to perform multiple queries to obtain every record. Whether a query has finish can be determined by using the following logic
if (isset($result['result']['done']) && $ result['result']['done'] == 'false' && isset($result['result']['queryLocator'])) {
$result = $conn->queryMore($r['result']['queryLocator']);
}So getting data from salesforce isn't hard you just need to understand the nature of the data you are retrieving.
2 Comments
Hi Marc, good post and clear
Hi Marc,
good post and clear API view.
Can you please tell me How I can add a contact information from my local serve to Salesforce contact object on a single request from salesforce account?
and
How can I parse a XML file which is store in my local server folder from salesforce account?
Please help me out.
Thanks,
salesforuse
Hey Marc, I've been using
Hey Marc,
I've been using NuSoap for quite a while (not on drupal, just on my site). While I've had no issue with inserts/updates/deletes (anything where I want to do something to salesforce), I can't seem to figure out how to get my SOQL SELECT query to run (anything where I want to get information from salesforce).
Everything works up to:
$result = $conn->query($soql);
At this point I don't receive an error, but nothing is returned on the next line (even a simple echo 'hello world';)
Any thoughts?
Post new comment