function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Peter KempPeter Kemp 

SObjects null field attributes

 Hi,

 

I'm trying to replicate data from Salesforce to a local MySQL database, I can pull the salesforce objects successfully and they each look like this:

 

[0]=> object(SObject)#11 (3) { ["type"]=> string(7) "Account" ["fields"]=> object(stdClass)#213 (4) { ["IsDeleted"]=> string(5) "false" ["MasterRecordId"]=> string(0) "" ["Name"]=> string(4) "Acme" ["Type"]=> string(8) "Prospect" }

["Id"]=> string(18) "0012000000162QEAAY" }

You can clearly see that there is a 'fields' object inside the sObject.  However, when I create an sObject variable to deal with thisI can no longer access the fields

 

for ($i = 0; $i < $record_count; $i++) //foreach ($records as $r) { $r = new SObject($records[$i]); $pass_this['id'] = $r->Id; foreach ($r->fields as $key => $value) { $pass_this[$key] = addslashes($r->fields->$key); }

 You can see that the fields obecjt is now empty?! It cannot parse the foreach statement and it gives the following error

 

 

object(SObject)#211 (3) { ["type"]=> string(7) "Account" ["fields"]=> NULL ["Id"]=> string(1) "0" } Warning: Invalid argument supplied for foreach()

 I am using version 13 of the php toolkit.  Any idea where I am going wrong?

 

Thanks

 

Pete

 

 

Best Answer chosen by Admin (Salesforce Developers) 
dwm189dwm189

Your query from sales force returns SObjects already, when you call:

 

$r = new SObject($records[$i]);


You really trying to construct a new SObject with an SObject.  Instead of trying to make a new one replace your line with the following:

 

$r = $records[$i];

 I think that should clear up the null fields.

 

 David

All Answers

dwm189dwm189

Your query from sales force returns SObjects already, when you call:

 

$r = new SObject($records[$i]);


You really trying to construct a new SObject with an SObject.  Instead of trying to make a new one replace your line with the following:

 

$r = $records[$i];

 I think that should clear up the null fields.

 

 David

This was selected as the best answer
Park Walker (TAGL)Park Walker (TAGL)

You may be interested in looking at the function described in this post which will create an array of new objects from the result of your query. While it was designed to help with relationship queries, it works equally well on simple ones.

 

Park 

Peter KempPeter Kemp

Thanks for the help.  This is some code from mike simonds excellent salesforce to MySQL replication tool.  It seems that the API v13 differs in a way that broke the original code and I need to spend more time getting my head around php objects and SF sObjects.

 

Check out the tool here:

http://www.mikesimonds.com/salesforce-php-mysql-database-replication-tool-t47.html

 

 

Thanks

 

Pete