One item that I have always hated when I am writing scripts that deal with large data sets or records was to have to write out each line of code in the scripts for each individual field. So over the past week I was working on the php eval() statement to make my backup scripts much smaller in code, there by saving time writing each script for each object that I wanted backups for.
Here is an example of what I am talking about
Here is query from the Account Object in Salesforce:
PHP Code:
$query = "Select Id, Name, Type, ParentId, BillingStreet, BillingCity,
BillingState, BillingPostalCode, BillingCountry, ShippingStreet, ShippingCity,
ShippingState, ShippingPostalCode, ShippingCountry, Phone, Fax, AccountNumber,
Website, Sic, Industry, AnnualRevenue, NumberOfEmployees, Ownership,
TickerSymbol, Description, Rating, Site, OwnerId, CreatedDate, CreatedById,
LastModifiedDate, LastModifiedById, SystemModstamp, LastActivityDate, SA_ID__c,
FY_CY_Wireless__c, FY_CY_Total_Design__c FROM Account";
Then the way that I had to access each field was to create a local array:
PHP Code:
foreach ($accounts as $r)
{
$r = new SObject($r);
$pass_this['id'] = $r->Id;
$pass_this['name'] = addslashes($r->fields->Name);
$pass_this['type'] = addslashes($r->fields->Type);
$pass_this['parentid'] = addslashes($r->fields->ParentId);
$pass_this['billingstreet'] = addslashes($r->fields->BillingStreet);
......
}
And the more individual fields that I wanted to add to the SOQL statement, I had to write code for each field. So what does PHP's eval() statement do. It gives you the ability to create a dynamic array. So no matter how big your SOQL statement is the annoyance of having to write a new line of code is now forgotten and not needed.
So once you return from a function that iterates through all your Salesforce Object data and returns it in an array, let's use $records as an example, you can just use the eval() statement to dynamically create your array:
Here is the full code that inserts records into a MySQL database:
PHP Code:
$soql = "Select Id, Name, BillingCity, BillingState From Account";
//Processes the query to get account information from Salesforce
$records = get_records($client, $soql);
if ($records)
{
echo '<p>There are currently ' . count($records) . ' accounts in Your Organization\'s salesforce</p>';
$i = 0;
//Loops through all records which come back from get_accounts function and
//stores them into an array $pass_this
foreach ($records as $r)
{
$r = new SObject($r);
$pass_this2['id'] = $r->Id;
foreach ($r->fields as $key=>$value)
{
//THIS IS WHERE EACH FIELD THAT YOU REQUEST IS
//BUILT NO MATTER WHAT THE SIZE OF YOUR QUERY MAY BE
eval("\$pass_this2['$key'] = addslashes(\$r->fields->$key);");
}
$i++;
$fields = implode(",", array_keys($pass_this2));
$values = implode("','",array_values($pass_this2));
$sql = $db->execute("INSERT INTO sforce_account (".$fields.") VALUES ('".$values."')");
if (!$sql)
{
echo "Error performing query: " . $db->ErrorMsg() . "<br />";
}
}
$record_count = $i;
echo '<p>There were ' .$record_count. ' accounts inserted into you MySQL database</p>';
}
So again if your SOQL statement is as simple as querying Salesforce via the API for 4 fields or 30, this will save you lots of time.
if you have any questions please feel free to contact me
~Mike
Bookmarks