Jump to content

Query and API limit


Nomisdk

Recommended Posts

Hi

I have the following function pulling data from a API.
Sadly there is a limit at the API, that i can only pull 500 entries.
I cant query the API for a total number for entires.

What i need help for is, how i can make it query the API and save the data, untill it has all entires.
 

public function loadAccounts() {
	$startid = -1;
	$xml = '
	
		<queryxml>
		<entity>Account</entity>
		<query>
		<field>id<expression op="greaterthan">'. $startid .'</expression></field>
		</query>
		</queryxml>
	
		';
	$query = new query ( $xml );
	$result = $this->client->query ( $query );		
	$this->accounts = Array ();
	$laccounts = $result->queryResult->EntityResults->Entity;
	
	foreach ( $laccounts as $account ) {
		$this->accounts [$account->id] = $account;
		}

Can someone help me setting the code up, so it will re-quey untill it has all entries?

Edited by Nomisdk
Link to comment
Share on other sites

There's not enough information to give a complete answer.

 

If you can specify an ORDER BY, so you get the Accounts in sequence you can just put that code in a loop until you get an indication that nothing was found. See my comments (#MAD) in the code below. Since I don't know the API, I can only guess at how to determine the exit point.

 

public function loadAccounts() {
	$startid = -1;

	#MAD Initialize the collection of accounts first
	$this->accounts = Array ();

	while(true) {	#MAD Until we issue a break;
		#MAD You need to check the API for an ORDER BY command - Here's my GUESS
		$xml = '
		
			<queryxml>
			<entity>Account</entity>
			<query>
			<field>id<expression op="greaterthan">'. $startid .'</expression></field>

		<order direction="ascending">id</order>

			</query>
			</queryxml>
		
			';
		$query = new query ( $xml );
		$result = $this->client->query ( $query );
	#MAD If the API returns FALSE when nothing is found ...
		if (!$result) break;

	#MAD OR test Entity to see if it has children?
		if (!$result->queryResult->EntityResults->Entity->haschildren) break;

		$laccounts = $result->queryResult->EntityResults->Entity;
	#MAD OR if $laccounts is an Array 
		if (empty($laccounts)) break;
		
		foreach ( $laccounts as $account ) {
			$this->accounts [$account->id] = $account;
			$startid = $account; #MAD So we get the LAST one returned and can issue a query for next
		}
	}
Link to comment
Share on other sites

Hi David

I always get the same order returned from the API, so I dont think that the "<order direction="ascending">id</order>" is needed.

The following is stated in the API User guide:

query()
This API call executes a query against Autotask and returns an array of matching entities. The queries are built
using the QueryXML format and will return a maximum of 500 records at once, sorted by their id value.
(https://www.autotask.net/help/content/Userguides/T_WebServicesAPIv1_5.pdf)

I will try testing your code later today :)
Edited by Nomisdk
Link to comment
Share on other sites

Ok it works :)

But i get the following notice:
Notice: Undefined property: stdClass::$Entity in /var/www/Autotask/class.autotask.php on line 114

Line 114 is:

#MAD OR test Entity to see if it has children?
if (!$result->queryResult->EntityResults->Entity) break;

How to fix this?
Link to comment
Share on other sites

In that doc you linked, under the API Best Practices heading is an entry "Use Multiple Queries to Retrieve Over 500 Records" that tells you how to do your original task.

 

 

For the following, use isset() to check if that property is set?

Notice: Undefined property: stdClass::$Entity in /var/www/Autotask/class.autotask.php on line 114

Line 114 is:
#MAD OR test Entity to see if it has children?
if (!$result->queryResult->EntityResults->Entity) break;

How to fix this?
 
 
Edited by boompa
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.