+ Start a Discussion
CortexCortex 

Querying for Custom Field

When I query a custom field from a custom object it does not return the custom field to me, I do however get the non-custom fields when I run the query.
 
I try the exact same query in sforce Explorer and it works exactly as it should. 
 
Why am I unable to retrieve this data?
 
Query: SELECT Id, Custom_Field__c FROM custom_object__c
 
Results:  Just "Id" (No error is issued).
 
-Cortex
CortexCortex
It seems that it doesn't return anything but the Id regardless of what I query for.   I tried querying for everything in the object, and all I got back was the Id.
SuperfellSuperfell
What tool are you using ? is the actual API response wrong, or is the tool you're using not parsing it correctly ?
CortexCortex
It's not parsing it correctly (I'm guessing), cause if I echo ($sforce->__last_response), I can see that the response does contain all the fields.  I'm guessing there is some sort of error with sForce's parser, cause I am just setting a variable equal to the response of $sforce->query().
 
This seems to be an intermittent problem, cause I had this problem with another account of mine, but it miraculously went away all of a sudden.
CortexCortex
Something else interesting to note is that this only seems to happen with Custom Objects and Custom Fields.  Standard Fields in standard objects work exactly as they should (meaning I get back what I queried).  At the same time custom fields in a standard object are giving me the problem as well.
 
-Cortex
CortexCortex
Another interesting point.  The "retrieve" api call ($sforce->retrieve()) is doing the exact same thing.  I ask for four fields (1 id, 1 standard field and 2 custom fields) and all I get back is the Id.
 
Very strange....
 
-Cortex
Tran ManTran Man
Cortex, it looks like you are using the PHP toolkit, right?  If so, are you using the partner WSDL?
CortexCortex
I am not using the php toolkit (I am using my own code, which is somewhat similar to the php toolkit), and I am using the enterprise wsdl.
Tran ManTran Man
Any reason why you wouldn't wnat to use the PHP toolkit?
CortexCortex
What's very strange is I use the exact some code for another salesforce account of mine and this problem does not exist (or at least hasn't in the past few days).  Like I said in one of my previous posts, I had this problem the other day with my other account, but the problem went away without my help.
CortexCortex
Mainly because I wanted to learn how it works, so I wrote my own.  I don't really have any problem with using the Toolkit, it just looked over complicated for my purposes.
CortexCortex

I wanted to learn how it actually worked, so I wrote my own and also the Toolkit looked a little too complicated for my purposes.  In addition I had some problems with it when I first investigated, although I couldn't tell you what they were now, and frankly just didn't trust the toolkit, so I wrote my own.

Tran ManTran Man
This is exactly the reason why the PHP toolkit was created.  ;-)  To make it easy to generate requests and parse out the responses. 

I would highly recommend that you try it.
CortexCortex

Ok, this is VERY interesting.  I tried doing the same thing using the PHP toolkit and got the same problem.  Any thoughts now?

CortexCortex
This is very peculiar, I'm ran this query (using php toolkit):
 
SELECT Id, FirstName, LastName, SystemModstamp FROM Contact
 
And the result look like this:
 
object(stdClass)[9]
  public 'type' => 'Contact' (length=7)
  public 'Id' => 
    array
      0 => '0035000000NdhphAAB' (length=18)
      1 => '0035000000NdhphAAB' (length=18)
  public 'any' => '<sf:FirstName>Babara</sf:FirstName><sf:LastName>Levy</sf:LastName><sf:SystemModstamp>2007-01-23T23:59:06.000Z</sf:SystemModstamp>' (length=129)
Tran ManTran Man
Two things:

* Make sure you use the partner wsdl
* For each result obj, instantiate an SObject.

$myobj = new SObject($objinstance);

THen you can access the fields as so:

echo $myobj->Id;
echo $myobj->.FirstName;

CortexCortex

Interesting.  I got it to work with the PHP toolkit, but I noticed an odd thing.  If I query for all the fields in my custom object I get this result:

 

object(SObject)[6]
  public 'Id' => 'a0050000006htC6AAI' (length=18)
  public 'type' => 'Custom__c' (length=10)
  public 'fields' => 
    object(SimpleXMLElement)[34]
      'Array' (length=5)
But, if I remove any one of the fields that I'm querying for, I get the result I'd like to see.
There's something screwy with this.
CortexCortex
Oh wait, that's not entirely true.  It seems there are certain fields I need to remove in order to get all the fields returned.  If I remove Id or SystemModstamp I'll get what I want back, otherwise...nada.
Tran ManTran Man
Interesting.  If you can put together a short reproducer, I can have a look.  FYI, I'm working on the next version of the PHP toolkit and I want to fix any outstanding bugs in the current PHP toolkit.
CortexCortex

Here's some code to reproduce the problem:

 

<?php

require_once ('soapclient/SforcePartnerClient.php');

try {
  $sforce = new SforcePartnerClient();
  $soap_client = $sforce->createConnection("partner.wsdl.xml");
  $sforce_login = $sforce->login("username", "password");

  $login_result = $sforce_login->userInfo;
  echo $login_result->userFullName.', your session id is '.$sforce_login->sessionId;
} catch (Exception $e) {
  echo $e->faultstring;
}

    $soql = "
        SELECT
          AccountId,
          AssistantName,
          AssistantPhone,
          Birthdate,
          CreatedById,
          CreatedDate,
          Department,
          Id,
          FirstName,
          LastName,
          SystemModstamp
        FROM
          Contact
    ";

    try {
      $result = $sforce->query($soql);
    } catch (Exception $e) {
      var_dump($e);
    }

foreach($result->records as $record) {
  var_dump($record);
  $myobj = new sObject($record);
  var_dump($myobj);
}

?>

CortexCortex
If you remove "FirstName, "LastName" and "SystemModStamp" from the query you will see that it works.  It seems to happen when $sObject->any is long and becomes an array.  Which would explain why I am getting "array" for the "fields" object item in one of the previous posts of mine.
 
Thanks.
CortexCortex
I believe I have actually figured out the root of my problem, it was because the wsdl file was cached on my system and for whatever reason wasn't getting re-read.  So, I added an ini_set to my code to turn off caching and the problem hasn't resurfaced yet.
 
-Chris
Tran ManTran Man
Great.  That would be great to add to the wiki. 

https://wiki.apexdevnet.com/index.php/PHP_1.0.x_Toolkit


veewhyveewhy
I am having the same problem, except the problem seems to manifest itself when I query for fields whose string representation evaluates to zero characters.  I am using the SforcePartnerClient::retrieve() method to pull Lead objects whose Id's I already know beforehand.

So, I came up with the following test case:

Lead field values (set via SalesForce end-user UI):

    Id = [auto-assigned]
    FirstName = "Joe"
    LastName = "Blow"
    Email = "" (0-length)


An offending (offensive?) snippet of code [minus proper exception handling for brevity's sake]:

function getLeadFromSalesForce( $sfObjectID ) {

    $mySforceConnection = new SforcePartnerClient();
    $mySoapClient = $mySforceConnection->createConnection(SALESFORCE_PARTNER_WSDL_PATH);
    $mylogin = $mySforceConnection->login($SFLogin, $SFPassword);

    $flds = 'Id, FirstName, LastName, Email';
    $response = $mySforceConnection->retrieve($flds, 'Lead', array($sfObjectID));

    if(is_null($response)) {
        return false;
    } else {
        $sObj = new SObject($response);
        return $sObj;
    }
}


At this point, if I var_export(), var_dump(), or print_r() the return value of the function, I see the following where the "empty" field values are supposed to be; this is true for every field requested in the call to SforcePartnerClient::retrieve() that otherwise appears as blank in the SalesForce UI:
SimpleXMLElement::__set_state(array(
)),
There always seems to be a line break/newline, and the value is always an object of type SimpleXMLElement.

What's up with this?  Why can't I just get a NULL value or a 0-length string back?  My ultimate goal is to persist these "string" values in the user's session, but the presence of the SimpleXMLElement objects in the results irreperably corrupts my session data (since one isn't supposed to store objects inside the $_SESSION superglobal unless they're serialized)!!

I'd rather not make the edits to the PHP Toolkit myself just to get what I need returned by the SforcePartnerClient object instance; how else can I make this happen without having to iterate through the entire field list and call something like is_object() on every dang field?

Thanks,
.e