API Client
Retrieve leads in specific order

When using Model::getList() method, you may want to retrieve the records in specific order. Use 'order' option for this purpose.

Soring in ascending order

$model = $client->model('Lead');
$fields = ['first_name', 'last_name'];
$result = $model->getList(['fields' => $fields, 'order' => 'last_name'], 0, 3); // fetch no more than 3 records, starting from the beginning
printf("There are %d leads in total\n", $result->totalResults());
echo json_encode($result->getRecords(), JSON_PRETTY_PRINT), "\n";

Output:

1 There are 38 leads in total
2 [
3  {
4  "first_name": "Alton",
5  "last_name": "Arriaga",
6  "id": "4da808b9-c6df-2407-9b0f-5aa798878d1d",
7  "name": "Alton Arriaga",
8  "salutation": null,
9  "_display": "Alton Arriaga"
10  },
11  {
12  "first_name": "Wilda",
13  "last_name": "Bakke",
14  "id": "438b96a9-2603-2854-681f-5aa798cdf157",
15  "name": "Wilda Bakke",
16  "salutation": null,
17  "_display": "Wilda Bakke"
18  },
19  {
20  "first_name": "Craig",
21  "last_name": "Bleau",
22  "id": "4b561e06-c3b2-c1f3-0dc3-5aa798663683",
23  "name": "Craig Bleau",
24  "salutation": null,
25  "_display": "Craig Bleau"
26  }
27 ]

Soring in descending order

$model = $client->model('Lead');
$fields = ['first_name', 'last_name'];
$result = $model->getList(['fields' => $fields, 'order' => 'last_name desc'], 0, 3); // fetch no more than 3 records, starting from the beginning
printf("There are %d leads in total\n", $result->totalResults());
echo json_encode($result->getRecords(), JSON_PRETTY_PRINT), "\n";

Output:

1 [
2  {
3  "first_name": "Angelo",
4  "last_name": "Winston",
5  "id": "dc3c817d-49e1-a0d5-ecbf-5aa798bf8354",
6  "name": "Angelo Winston",
7  "salutation": null,
8  "_display": "Angelo Winston"
9  },
10  {
11  "first_name": "Michael",
12  "last_name": "Whitehead",
13  "id": "d341cb27-683b-3d9f-9554-5aa798b475f0",
14  "name": "Michael Whitehead",
15  "salutation": null,
16  "_display": "Michael Whitehead"
17  },
18  {
19  "first_name": "Maximo",
20  "last_name": "Spradlin",
21  "id": "43950469-288d-6250-713f-5aa798bd0dd1",
22  "name": "Maximo Spradlin",
23  "salutation": null,
24  "_display": "Maximo Spradlin"
25  }
26 ]