Sample code for 30+ languages & platforms
Zig

SugarCRM Filtering Records

See more SugarCRM Examples

Export records and filter.

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    const rest = try chilkat.Rest.init();
    defer rest.deinit();

    rest.connect("your.site.domain", 443, true, true) catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    rest.addHeader("Cache-Control", "no-cache") catch {};
    rest.addHeader("OAuth-Token", "<access_token>") catch {};

    // The following code creates the JSON request body.
    // The JSON created by this code is shown below.
    const json_req = try chilkat.JsonObject.init();
    defer json_req.deinit();
    json_req.updateString("filter[0].$or[0].name.$starts", "A") catch {};
    json_req.updateString("filter[0].$or[1].name.$starts", "b") catch {};
    json_req.updateNumber("max_num", "2") catch {};
    json_req.updateNumber("offset", "0") catch {};
    json_req.updateString("fields", "id") catch {};
    json_req.updateString("order_by", "date_entered") catch {};
    json_req.updateBool("favorites", false) catch {};
    json_req.updateBool("my_items", false) catch {};

    // 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
    // }

    const sb_req = try chilkat.StringBuilder.init();
    defer sb_req.deinit();
    json_req.emitSb(sb_req) catch {};

    rest.addHeader("Content-Type", "application/json") catch {};

    const sb_json = try chilkat.StringBuilder.init();
    defer sb_json.deinit();
    rest.fullRequestSb("POST", "/rest/v10/Accounts/filter", sb_req, sb_json) catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    if (rest.getResponseStatusCode() != 200) {
        std.debug.print("Received error response code: {d}\n", .{rest.getResponseStatusCode()});
        std.debug.print("Response body:\n", .{});
        std.debug.print("{s}\n", .{try sb_json.getAsString(alloc)});
        return;
    }

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.loadSb(sb_json) catch {};

    // The following code parses the JSON response.
    // A sample JSON response is shown below the sample code.
    var i: i32 = 0;
    var id: [:0]const u8 = "";
    var date_modified: [:0]const u8 = "";
    var v_module: [:0]const u8 = "";

    _ = json.intOf("next_offset");
    i = 0;
    const count_i = json.sizeOfArray("records");
    while (i < count_i) {
        json.setI(i);
        id = try json.stringOf(alloc, "records[i].id");
        date_modified = try json.stringOf(alloc, "records[i].date_modified");
        v_module = try json.stringOf(alloc, "records[i]._module");
        i = i + 1;
    }

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

    std.debug.print("Example Completed.\n", .{});
}