Sample code for 30+ languages & platforms
Lua

SugarCRM Filtering Records

See more SugarCRM Examples

Export records and filter.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

local rest = chilkat.Rest.new()

local success = rest:connect("your.site.domain", 443, true, true)
if not success then
    print(rest.last_error_text)
    return
end

rest:add_header("Cache-Control", "no-cache")
rest:add_header("OAuth-Token", "<access_token>")

-- The following code creates the JSON request body.
-- The JSON created by this code is shown below.
local jsonReq = chilkat.JsonObject.new()
jsonReq:update_string("filter[0].$or[0].name.$starts", "A")
jsonReq:update_string("filter[0].$or[1].name.$starts", "b")
jsonReq:update_number("max_num", "2")
jsonReq:update_number("offset", "0")
jsonReq:update_string("fields", "id")
jsonReq:update_string("order_by", "date_entered")
jsonReq:update_bool("favorites", false)
jsonReq:update_bool("my_items", false)

-- The JSON request body created by the above code:

-- {
--   "filter": [
--     {
--       "$or": [
--         {
--           "name": {
--             "$starts": "A"
--           }
--         },
--         {
--           "name": {
--             "$starts": "b"
--           }
--         }
--       ]
--     }
--   ],
--   "max_num": 2,
--   "offset": 0,
--   "fields": "id",
--   "order_by": "date_entered",
--   "favorites": false,
--   "my_items": false
-- }

local sbReq = chilkat.StringBuilder.new()
jsonReq:emit_sb(sbReq)

rest:add_header("Content-Type", "application/json")

local sbJson = chilkat.StringBuilder.new()
success = rest:full_request_sb("POST", "/rest/v10/Accounts/filter", sbReq, sbJson)
if not success then
    print(rest.last_error_text)
    return
end

if rest.response_status_code ~= 200 then
    print("Received error response code: " .. rest.response_status_code)
    print("Response body:")
    print(sbJson:get_as_string())
    return
end

local json = chilkat.JsonObject.new()
json:load_sb(sbJson)

-- The following code parses the JSON response.
-- A sample JSON response is shown below the sample code.
local id = ""
local date_modified = ""
local v_module = ""

local next_offset = json:int_of("next_offset")
local i = 0
local count_i = json:size_of_array("records")
while i < count_i do
    json.i = i
    id = json:string_of("records[i].id")
    date_modified = json:string_of("records[i].date_modified")
    v_module = json:string_of("records[i]._module")
    i = i + 1
end

-- A sample JSON response body that is parsed by the above code:

-- {
--   "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"
--     }
--   ]
-- }

print("Example Completed.")