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
Josh VJosh V 

Fatal error: Call to a member function query() on a non-object

Hey all,

 

I'm having a weird issue with the query function of the toolkit.  I have almost the exact same code used in a different page, but for some reason it won't work on my current project....

 

Here's the code:

 

$query = "SELECT AccountId, OwnerId, Site_City__c FROM Opportunity WHERE Id = '0068000000XuflG' LIMIT 1";

$response = $mySforceConnection->query($query);

foreach ($response->records as $data){
  foreach($fields as $var){
    $FullVar = "$varname_$var";
    $$FullVar =	$data->$var; 	
  }
}

 The set of foreaches are something that I'm trying to test at the bottom, but I never get that far down.  Instead I get the PHP error:

 

Fatal error: Call to a member function query() on a non-object in /path/to/public_html/openhouse/query_test.php on line 3

 

I have almost exactly the same code in another page (located in the same directory), and it works fine.  The only difference is the query, which is structured the exact same, only it queries more fields...

 

The other code that works:

$query = "SELECT AccountId, OwnerId, Site_City__c, Site_Street__c, Site_State__c, Site_Zip__c, Model_Name__c, A7_Est_Comp_Date__c FROM Opportunity WHERE Id = '$OppID' LIMIT 1";

 

Here is the entire page of code:

<?php

  require('/path/to/sf_modules/credentials.php');
	
	function SFQuery($fields=array() , $object , $filter , $varname , $limit=1){
		
	  foreach($fields as $field){
		  $field_line .= $field.", ";
	  }
	  
	  // Removes extra comma and space from the end of the fields
	  $field_line = substr($field_line, 0, -2);
			  
	  $query = "SELECT $field_line FROM $object WHERE $filter LIMIT $limit";
	  
	  $response = $mySforceConnection->query($query);
	  
	  foreach ($response->records as $data){
		foreach($fields as $var){
		  $FullVar = "$varname_$var";
		  $$FullVar =		$data->$var; 	
		}
	  }
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php  SFQuery(array("AccountId","OwnerId","Site_City__c"),"Opportunity","Id = '0068000000XuflG'","TestVar",1); 

echo "$TestVar_AccountId, $TestVar_OwnerId, $TestVar_Site_City__c";

?>
</body>
</html>

 


 

 

Does anyone have any ideas about what could be wrong?

 

Josh

 

 


Best Answer chosen by Admin (Salesforce Developers) 
Josh VJosh V

To anyone else that comes across this problem in the future:

 

To solve this problem, you must require_once the SforceClient from within the class.  I had mine required before i defined the class which caused the problem.

All Answers

Josh VJosh V

So I've narrowed it down to the function.  Something about the fact that it is being called within a function is throwing it off...

Josh VJosh V

To anyone else that comes across this problem in the future:

 

To solve this problem, you must require_once the SforceClient from within the class.  I had mine required before i defined the class which caused the problem.

This was selected as the best answer