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
chuckdubdubchuckdubdub 

Getting individual field value from a queryResult()

Here's my query that I am using with php4 nusoap:

    $queryResult = $sfdc->query("select id, firstname, lastname, phone, description, fax from lead where id = '$id'");
    print_r("\nquery by id: \n\n");
    print_r($queryResult);

How do I just grab the value of "description" and assign it to a variable?  I'm guessing I need to loop through $queryResult as an array somehow? 

I tried setting a variable to $queryResult['description'] but to no avail!

Thanks!

Chuck

Park Walker (TAGL)Park Walker (TAGL)
The query result is an object. One of the fields is 'records'. The field will contain a single record or an array of records. You can test the count to see which. I usually convert a single record to an array of one to make the final processing easier. You then loop through the result array.

If you know that you are only going to get one record back you can just access the fields as $result->records->fieldname.

Code:
$result = $sfdc->query($query);
if ($result->size == 1) $recs = array($result->records);
else $recs = $result->records;

Note that this is for nuSoap. The PHP5 soap library returns a different structure ($result->result->records).
Park
 

  
chuckdubdubchuckdubdub

Great, thanks for these tips.  So if I'm doing this:

if ($queryResult->size == 1) $recs = array($queryResult->records->description);
// else $recs = $queryResult ->records;
 
 echo print_r($recs);

The value I'm getting for $recs is "1", but I want to get the actual value of "description".  Am I doing this right?

Thanks for your patience as I noodle through this!

 

ClaiborneClaiborne

Code:

// create Contact
$contact = new sObject('Contact', 
                       null, 
                       array(
                           'FirstName' => 'TestFirstName', 
                           'LastName' => 'TestLastName',
                           'Phone' => '555-555-1212',
                           'Fax' => '444-444-1212'
                       )                           
                      );
$createResult = $sfdc->create($contact);
print_r("\ncreate one: \n\n");
print_r($createResult);

// query Contact by ID
$id = $createResult['id'];
$queryResult = $sfdc->query("select id, FirstName, LastName, Phone, Fax from contact where id = '$id'");
if ($queryResult['size'] == "1") {
    $record = $queryResult['records'];
    $values = $record->values;
    $firstname = $values['FirstName'];
    $lastname = $values['LastName'];
    $phone = $values['Phone'];
    $fax = $values['Fax'];
}
print_r("\nquery by id: \n\n");
print_r($queryResult);
    
// delete contact
$deleteResult = $sfdc->delete($createResult['id']);
$id = $deleteResult['id'];
print_r("\ndelete one ($id): \n\n");
print_r($deleteResult);


 
This is an example of working code to parse out a query result. It expands on Ryan Choi's example code.

The key is that objects can be referenced as object->element but arrays must use array['element']. Also, the result is somewhat structured.

It helps to use a PHP development tool such as Nusphere or Zend. Then you can step through your PHP code and see the variable values and structure as you go.

chuckdubdubchuckdubdub

Awesome, this did it for me, thanks so much!

 

if ($queryResult['size'] == "1") {
    $record = $queryResult['records'];
    $values = $record->values;
    $firstname = $values['FirstName'];
    $lastname = $values['LastName'];
    $phone = $values['Phone'];
    $fax = $values['Fax'];
}

Pradeep_NavatarPradeep_Navatar

Find below a sample code which displays the ten most viewed articles in the 'phone' category as an HMTL list of links. Phone is in the 'products' category group.

 

<apex:outputPanel layout="block">
<ul>
<knowledge:articleList articleVar="article"
categories="products:phone"
sortBy="mostViewed"
pageSize="10"
>
<li><a href="{!URLFOR($Action.KnowledgeArticle.View,
article.id)}">{!article.title}</a></li>
</knowledge:articleList>
</ul>
</apex:outputPanel>