Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SalesForce.com: Retrieve custom fields via PHP

Tags:

php

salesforce

I have a simple custom contact object (with API name Contact__c) that I've created in my SalesForce DE site that has a single field (for testing connectivity) of Full_Name__c.

I am then trying to retrieve all of the contacts, specifically this field via PHP:

try {
  $mySforceConnection = new SforcePartnerClient();
  $mySoapClient       = $mySforceConnection->createConnection(API_PATH . '/soapclient/partner.wsdl');
  $mylogin            = $mySforceConnection->login(API_USER, API_PASS . API_SECURITY_TOKEN);

  $query = 'SELECT C.Id, C.Full_Name__c
          FROM Contact__c C'; 
  $result = $mySforceConnection->query($query);
  $sObject = new SObject($result->records[0]);
  print_r($sObject);
} catch(Exception $e) {
  print_r($e);
}

I've downloaded the latest partner.wdsl (although as a partner WSDL, it is loosely typed and does not need to be downloaded with the creation/addition of custom objects and/or updated fields, correct?). I've verified that the user can connect and see the custom fields via the ForceExplorer. But when I run the above code, it connects but returns just the following:

SObject Object ( [type] => Contact__c [fields] => [Id] => a )

I am not getting any errors, invalid field error, etc, but for the life of me can't figure out why this isn't working.

I saw this example here, but it seems to be specific to Enterprise vs Partner, and the need to download the latest enterprise.wsdl every-time you change custom fields.

Any pointers?

like image 318
Unpossible Avatar asked Apr 20 '11 17:04

Unpossible


1 Answers

Figured it out I believe, the problem related to how I was parsing the data that was being returned. Instead of feeding the returned data into an SObject, I am now just accessing it directly:

try {
  $mySforceConnection = new SforcePartnerClient();
  $mySoapClient       = $mySforceConnection->createConnection(API_PATH . '/soapclient/partner.wsdl');
  $mylogin            = $mySforceConnection->login(API_USER, API_PASS . API_SECURITY_TOKEN);

  $query = 'SELECT C.Id, C.Full_Name__c
            FROM Contact__c C'; 
  $result = $mySforceConnection->query($query);

  for($i = 0; $i < count($result->records); $i++) {
    print_r($result->records[$i]->fields->Full_Name__c); 
  }
} catch(Exception $e) {
  print_r($e);
}
like image 51
Unpossible Avatar answered Oct 05 '22 05:10

Unpossible