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
Peter KayePeter Kaye 

Loading a table record count into a php variable.

I am struggling with SOQL code with the php toolkit to retrieve the total number of records in a custom object table.  I have copied the apporach which is working for an ordinary SELECT query which retrieves multiple records.

Here is is the code that fails to fill the variable  $numRecs
 
$query = "SELECT COUNT() FROM Service3__c";
$response = $mySforceConnection->query($query);

foreach ($response->records as $record) {
  $numRecs = $record->Count();
}

echo $numRecs;

What have I got wrong ?  Thanks.
Best Answer chosen by Peter Kaye
SandhyaSandhya (Salesforce Developers) 
Hi Peter,

Try below code.

    $cnt = 0;

if ($response->size > 0) {

    $sObject1 = new SObject($response->records[0]); 

    foreach ($response->records as $record1) {
         
        $cnt++;

    }

    }

    echo $cnt;

}

And also I have seen like

 
echo "Size of records:  ".$response ->size."\n";

Hope this helps you!

Please mark it as solved if this helps you so that it will make available for other as a proper solution.

Thanks and Regards
Sandhya

 

All Answers

SandhyaSandhya (Salesforce Developers) 
Hi Peter,

Try below code.

    $cnt = 0;

if ($response->size > 0) {

    $sObject1 = new SObject($response->records[0]); 

    foreach ($response->records as $record1) {
         
        $cnt++;

    }

    }

    echo $cnt;

}

And also I have seen like

 
echo "Size of records:  ".$response ->size."\n";

Hope this helps you!

Please mark it as solved if this helps you so that it will make available for other as a proper solution.

Thanks and Regards
Sandhya

 
This was selected as the best answer
Peter KayePeter Kaye
Thanks Sandya - both pieces of code work but I really like the second,  $response->size .  It's shorter and likely to be more efficient especially where thousands of records are involved.  Thanks for your help on this.