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
ThrantorThrantor 

Can't access custom contact field in Opportunity using the API.

I've created a Opportunity Contact field for Opportunity which is a lookup on the Contact table.

 

From salesforce I can see the contact and work with it and everything looks happy.

 

But when trying to access the contacts information using PHP I get nothing. I'm using the following code:

 

	$queryOpportunity = "Select AccountId, Amount, Paid__c, Opportunity_Contact__c from Opportunity WHERE Id = '$id'" ;
	$OppResponse = $mySforceConnection->query(($queryOpportunity));
	foreach ($OppResponse->records as $OppRecord) {
		$subtotal = $OppRecord->Amount ;
		$email = $OppRecord->Opportunity_Contact__c->Email ;
		var_dump($OppRecord) ;
		var_dump($OppRecord->Opportunity_Contact__c) ;

 

 

And I can't get any of the information for the contact. Help.

 

Also, Instead of grabbing the AccountID and doing a seperate query on account is there anyway to just grab the account information with this query as well?

Best Answer chosen by Admin (Salesforce Developers) 
ptepperptepper

I've had problems retrieving certain custom fields when using the wrong version of the SOAP API. I think it's up to 18.0 now. Look through the files in the Toolkit, if I remember correctly, I think you just need to change it in the WSDL XML file, in your case probably the partner WSDL file to   https://www.salesforce.com/services/Soap/u/18.0

 

You can do the kind of query you're talking about - here's an example of one of these nested queries:

 

Select a.Name, (Select FirstName, LastName From Contacts), (Select Subject From Events), (Select Amount, Name, TotalOpportunityQuantity, Type From Opportunities) from Account a

 

You should check out the Apex Explorer, it makes testing queries easier. You would also need to change the SOAP version in Apex Explorer, under Tools > Options.

 

best,

-paul

All Answers

ptepperptepper

I've had problems retrieving certain custom fields when using the wrong version of the SOAP API. I think it's up to 18.0 now. Look through the files in the Toolkit, if I remember correctly, I think you just need to change it in the WSDL XML file, in your case probably the partner WSDL file to   https://www.salesforce.com/services/Soap/u/18.0

 

You can do the kind of query you're talking about - here's an example of one of these nested queries:

 

Select a.Name, (Select FirstName, LastName From Contacts), (Select Subject From Events), (Select Amount, Name, TotalOpportunityQuantity, Type From Opportunities) from Account a

 

You should check out the Apex Explorer, it makes testing queries easier. You would also need to change the SOAP version in Apex Explorer, under Tools > Options.

 

best,

-paul

This was selected as the best answer
ThrantorThrantor

Thank you. You're response helped GREATLY. I ended up going with this as a query:

 

	$queryOpportunity = "Select o.AccountId, o.Account.BillingCity, o.Account.BillingCountry, o.Account.BillingPostalCode, o.Account.BillingState, o.Account.BillingStreet, o.Account.Name, o.Amount, o.Opportunity_Contact__c, o.Opportunity_Contact__r.Email, o.Paid__c from Opportunity o WHERE Id = '$id'" ;

 

 

Although, I did have to add another line to the line I already had in the enterprise.xml:

 

                        <element name="Opportunity_Contact__c" nillable="true" minOccurs="0" type="tns:ID"/>
                        <element name="Opportunity_Contact__r" nillable="true" minOccurs="0" type="ens:Contact"/>

 

After that... I'm getting valid data back. WOOT!

 

pmmennegpmmenneg

New to the API, having the same problem... just so I understand, I am using the partner wsdl, and have no problem connecting.  Created a custom 'Kcontact' object that contains a number of fields, name__C, email__c, just for testing purposes.

 

Running the following query, I only get a response from the Id field, none of the custom fields...

 

 

$query = 'SELECT C.Id, C.name__c, C.email__c, C.city__c, C.region__c, C.country__c
          FROM Contact__c C';

$result = $mySforceConnection->query($query);
$sObject = new SObject($result->records[0]);
print_r($sObject);

 

And here is the response:

 

 

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

 

 

So I will need to manually add name__c, email__c, etc to the partner wsdl XML file?  I added these via the website and re-downloaded the partner wsdl file from SF, but still getting nothing back for my custom columns.

 

I am using 13.1 of the PHP toolkit, fyi.

 

Thanks for any clarification!