Sample code for 30+ languages & platforms
Zig

SugarCRM Create a Record List

See more SugarCRM Examples

Create a record list in Sugar consisting of a set of ids.

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("records[0]", "f16760a4-3a52-f77d-1522-5703ca28925f") catch {};
    json_req.updateString("records[1]", "ec409fbb-2b22-4f32-7fa1-5703caf78dc3") catch {};

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

    // {
    //   "records": [
    //     "f16760a4-3a52-f77d-1522-5703ca28925f",
    //     "ec409fbb-2b22-4f32-7fa1-5703caf78dc3"
    //   ]
    // }

    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/record_list", 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 str_val: [:0]const u8 = "";

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

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

    // {
    //   "id": "ef963176-4845-bc55-b03e-570430b4173c",
    //   "assigned_user_id": "1",
    //   "module_name": "Accounts",
    //   "records": [
    //     "f16760a4-3a52-f77d-1522-5703ca28925f",
    //     "ec409fbb-2b22-4f32-7fa1-5703caf78dc3"
    //   ],
    //   "date_modified": "2016-04-05 21:39:19"
    // }

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