Sharing some ideas and advice on using REST API filter conditionals

Target Audience

I am skipping authentication and the basics of REST. The target audience is the developer who wants to get more out of the API. This page is a good starting point for the REST API and covers the basics skipped in this post. There is plenty to learn about here. Take your time and make use of SugarClub to get advice.

Introduction

I am sharing some of my ideas and advice on using the filter endpoint for the SugarCRM REST API (a.k.a. Web Services).

The focus is on the HTTP POST method as this is the method I prefer when using filters. I find the JSON used to be more readable and that helps me get the results I need.

The idea for the example is a project that needs a list of Leads that have a phone number. The example makes use of the OR conditional and an example of the filter/count endpoint is included.

I am using the operator "$not_empty" here as it is a shortcut to get NOT NULL OR NOT "" results.

The filter endpoint for the REST API maps to the PHP filter API so this guide is a good resource.

The filter endpoint supports GET and POST. The first example is for GET to show the difference in syntax.

Examples

The example below shows a filter using GET and uses the count endpoint to keep it simple.

myinstancename.sugarondemand.com/MODULENAME/count

URL encoding of the parameters is required so the actual request would look like the one below.

https://myinstancename.sugarondemand.com/rest/v11/Leads/filter/count?filter%5B0%5D%5Bphone_work%5D%5B%24not_empty%5D%3D%0A

The count endpoint does not need the list of fields to return or the other parameters required by the filter endpoint.


Next, we have the POST examples.

The filters search for Leads that have something filled in for phone_work or for phone_mobile.

1) Get a count of records.

HTTP method: POST

URL: https://myinstancename.sugarondemand.com/rest/v11/Leads/filter/count

Body (JSON):

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{
"filter": [
{
"$or": [
{
"phone_work": {
"$not_empty": ""
}
},
{
"phone_mobile": {
"$not_empty": ""
}
}
]
}
]
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

2) Get actual records, extra parameters are included to collect only the needed fields and to support pagination.

HTTP method: POST

URL: https://myinstancename.sugarondemand.com/rest/v11/Leads/filter

BODY ( JSON):

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
{
"filter": [
{
"$or": [
{
"phone_work": {
"$not_empty": ""
}
},
{
"phone_mobile": {
"$not_empty": ""
}
}
]
}
],
"max_num": 2,
"offset": 0,
"fields": "id,first_name,last_name,phone_work,phone_mobile,date_entered",
"order_by": "date_entered",
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX


Please share any questions as a reply to this discussion.

Parents
  • Hi  

    That is very interesting. What would be the best call to get a specific account record with a unique customer ID (icc_customernr)? This syntax still gives me additional records.

    GET https://myinstancename.com/Accounts/?icc_customernr="0000200716"&deleted=false&fields=icc_customernr,id,name

    Best,
    Steffen

  • Hi Steffen,

    Here is how you can filter your based upon your unique customer id.

    https://myinstancename.com/rest/v11_2/Accounts/filter?filter[0][icc_customernr]=0000200716&fields=icc_customernr,id,name&max_num=1

    OR

    GET https://myinstancename.com/rest/v11_2/Accounts/filter

    Fullscreen
    1
    2
    3
    4
    5
    6
    7
    8
    9
    {
    "filter": [
    {
    "icc_customernr": "0000200716"
    }
    ],
    "fields": "icc_customernr,id,name",
    "max_num": 1
    }
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

  • Thanks   - 

    myinstancename.com/.../filter

    this works perfectly - except that the request does not return the account name from the "name" field. Any ideas?

    Best,
    Steffen

  • Try omitting the fields parameter and all the fields will be returned.

    That might reveal the field name you are looking for.

  • Thank you,  !

    With this request body I get exactly the desired result.

    Fullscreen
    1
    2
    3
    4
    5
    6
    7
    8
    9
    {
    "filter":[
    {
    "icc_kna1_kunnr":{
    "$equals":"0000105304"
    }
    }
    ]
    }
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Reply Children
No Data