How to Filter a List of Records
Overview
An example in bash script demonstrating how to filter records using the v11 /<module>/filter
REST POST
endpoints.
Filtering Records
Authenticating
First, you will need to authenticate to the Sugar API. An example is shown below:
curl -X POST -H Cache-Control:no-cache -H "Content-Type: application/json" -d '{
"grant_type":"password",
"client_id":"sugar",
"client_secret":"",
"username":"admin",
"password":"password",
"platform":"custom_api"
}' https://{site_url}/rest/v11/oauth2/token
More information on authenticating can be found in the How to Authenticate and Log Out example and /oauth2/logout endpoint documentation.
Filtering Records
Next we can filter the records we want to return using the /<module>/filter
endpoint with a POST
request.
curl -s -X POST -H OAuth-Token:{access_token} -H "Content-Type: application/json" -H Cache-Control:no-cache -d '{
"filter":[
{
"$or":[
{
"name":{
"$starts":"A"
}
},
{
"name":{
"$starts":"b"
}
}
]
}
],
"max_num":2,
"offset":0,
"fields":"id",
"order_by":"date_entered",
"favorites":false,
"my_items":false
}' https://{site_url}/rest/v11/Accounts/filter
More information on the filter API can be found in the /<module>/filter documentation.
Note : The /<module>/filter
endpoint can be called using a GET
request as well, though long URL requests can have issues so the POST
request is recommended.
Response
The data received from the server is shown below:
{
"next_offset":2,
"records":[
{
"id":"f16760a4-3a52-f77d-1522-5703ca28925f",
"date_modified":"2016-04-05T10:23:28-04:00",
"_acl":{
"fields":{
}
},
"_module":"Accounts"
},
{
"id":"ec409fbb-2b22-4f32-7fa1-5703caf78dc3",
"date_modified":"2016-04-05T10:23:28-04:00",
"_acl":{
"fields":{
}
},
"_module":"Accounts"
}
]
}