Hi
I haven't done a relationship query before so i have a dumb question.
I am querying Contact object and want to get Account Name as well.
I don't know how...using the code below...to access the Account Name value.
HELP
PHP Code:<?php
require_once("c:/php/sforce/soapclient/SforcePartnerClient.php");
require_once ('c:/php/sforce/soapclient/SforceHeaderOptions.php');
require_once ('c:/php/sforce/userAuth.php');
$conn= new SforcePartnerClient();
$conn->createconnection("partner.wsdl.xml");
$mylogin=$conn->login("username","password");
$query="Select Contact.Id,Contacts.FirstName,Contact.Contacts_ID__c,Contact.org_code__c,Contact.Account.Name from Contact where contacts_id__c='21666'";
$response=$conn->query($query);
foreach ($response->records as $result) {
$name=$result->fields->FirstName
$sforce_id=$result->Id;
$updateFields = array(
'Id'=>$sforce_id,
);
$sObjectCustom = new SObject();
$sObjectCustom->type = 'Contact';
$sObjectCustom->fields = $updateFields;
$createResponse = $conn->update(array($sObjectCustom));
}
?>
what version off php are you using? I understand that you have to be running php 5.3 or greater to get the relationship queries to work. I have been able to get this to work in the past using lowerr versions. Let me dig that code up and see if I can paste it. I know that in the past that if I have queries that I am not sure are going to work, i have used the salesforce query tool to run my queries to make sure they work and then just plug them into the code.
~Mike
hi mike
yes, i have a version that allows me to do relationship queries
my question is a simplistic one
how do i extract the actual account name from the results so i can use that value to update a table in SQL Server?
eg. my returned object looks like
SObject Object ( [type] => Contact [fields] => stdClass Object ( [Contacts_ID__c] => someOrgCode [Org_Code__c] => MB [1] => SObject Object ( [type] => Account [fields] => stdClass Object ( [Name] => Some organization ) ) ) [Id] => salesforceId )
thanks
tam
I will do my best to have some code that will allow this some time Monday
~Mike
hi mike
thanks
any help with this is appreciated
tam
Hi Mike
Just wondering if you are able to offer any resources on this?
Thanks
Tammy
Sorry Tammy I have been hammered at work. I will do my best to post something here on Monday. Again sorry for the delay
Okay I was able to get this to work:
1) I have a login script to salesforce which establishes the connection via the API
the I use require_once() in the script where I place the queryPHP Code:<?php
require_once ('/users/msimonds/public_html/includes/soapclient_new/SforcePartnerClient.php');
require_once ('/users/msimonds/public_html/includes/soapclient_new/SforceHeaderOptions.php');
// Login to salesforce.com
$login = "you@youremail.com";
$password = "password";
$wsdl = "/users/msimonds/public_html/includes/soapclient_new/partner.wsdl.xml";
$client = new SforcePartnerClient();
$client->createConnection($wsdl);
try
{
$loginResult = $client->login($login, $password);
if ($loginResult->passwordExpired)
{
$client = null;
}
}
catch (exception $e)
{
$client = null;
echo '<pre>'.print_r($e,true).'</pre>';
}
?>
This worked for me:PHP Code:<?php
ini_set("soap.wsdl_cache_enabled", "0");
require_once ('/users/msimonds/public_html/includes/simonds.inc');
$soql = "Select a.Id, a.Name, (Select Name From Contacts) from Account a where a.name = 'Pioneer' ";
$records = get_records($client, $soql);
foreach ($records as $result)
{
echo '<pre>' . print_r($result, true) . '</pre>';
exit;
}
function get_records(&$connection, &$query)
{
$queryOptions = new QueryOptions(2000);
$response = new QueryResult($connection->query($query));
// if the size is zero, where done
if ($response->size > 0)
{
$records = $response->records;
// Cycles through additional responses if the number of records
// exceeds the batch size
while (!$response->done)
{
set_time_limit(100);
$response = $connection->queryMore($response->queryLocator);
$records = array_merge($products, $response->records);
}
}
return $records;
}
?>
Hope this helps!!Code:SObject Object ( [type] => Account [fields] => stdClass Object ( [Name] => Pioneer ) [Id] => 0015000000OKR1cAAH [queryResult] => Array ( [0] => QueryResult Object ( [queryLocator] => [done] => 1 [records] => Array ( [0] => SObject Object ( [type] => Contact [fields] => stdClass Object ( [Name] => Todd Golay ) ) ) [size] => 1 ) ) )
Make sure you check the paths if you use this script(s)
~Mike
Hi Mike
No apology required. I appreciate you taking the time to try to help.
I actually can already see the information as you display it.
My dumb question is not how do I write out the sobject but rather, how to i populate a variable with just the name of the person.
eg.
$soql = "Select a.Id, a.Name,a.Country (Select Name From Contacts) from Account a where a.Country = 'Canada'";
$records = get_records($conn, $soql);
foreach ($records as $result)
{
populate a variable with Account Name
populate a variable with Name of Contact
exit;
}
Hope this makes sense
Tam
So you have to modify the original foreach and add some sub-foreach statements:
Look at the new code:
let me know if you understand that TamPHP Code:<?php
ini_set("soap.wsdl_cache_enabled", "0");
require_once ('/users/msimonds/public_html/includes/simonds.inc');
$soql = "Select a.Id, a.Name, (Select Name From Contacts) from Account a where a.name = 'Pioneer' ";
$records = get_records($client, $soql);
foreach ($records as $result)
{
foreach ($result->fields as $r)
{
//echo '<pre>' . print_r($r, true) . '</pre>';
$account_name = $r;
echo $account_name . '<br />';
}
foreach($result->queryResult as $data)
{
foreach($data->records as $more_data)
{
//echo '<pre>' . print_r($more_data, true) . '</pre>';
$contact_name = $more_data->fields->Name;
echo $contact_name;
}
}
}
function get_records(&$connection, &$query)
{
$queryOptions = new QueryOptions(2000);
$response = new QueryResult($connection->query($query));
// if the size is zero, where done
if ($response->size > 0)
{
$records = $response->records;
// Cycles through additional responses if the number of records
// exceeds the batch size
while (!$response->done)
{
set_time_limit(100);
$response = $connection->queryMore($response->queryLocator);
$records = array_merge($products, $response->records);
}
}
return $records;
}
?>
BTW, Are you going to DreamForce this year?
~Mike
Bookmarks