Sample code for 30+ languages & platforms
Zig Requires Chilkat v11.0.0+

SugarCRM Create a Record

See more SugarCRM Examples

Submit a record to the Sugar instance using the / endpoint. In this example we are going to create an Account record with a Name of 'Test Record' and an email of 'test@sugar.com'.

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();

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    const http = try chilkat.Http.init();
    defer http.deinit();

    // Implements the following CURL command:

    // curl -X POST -H OAuth-Token:<access_token> -H Cache-Control:no-cache -d '{
    //    "name":"Test Record",
    //    "email1":"test@sugar.com"
    // }' http://<site_url>/rest/v10/Accounts

    // Use this online tool to generate code from sample JSON:
    // Generate Code to Create JSON

    // The following JSON is sent in the request body.

    // {
    //   "name": "Test Record",
    //   "email1": "test@sugar.com"
    // }

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.updateString("name", "Test Record") catch {};
    json.updateString("email1", "test@sugar.com") catch {};

    http.setRequestHeader("Cache-Control", "no-cache");
    http.setRequestHeader("OAuth-Token", "<access_token>");

    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpJson("POST", "http://<site_url>/rest/v10/Accounts", json, "application/json", resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    const sb_response_body = try chilkat.StringBuilder.init();
    defer sb_response_body.deinit();
    resp.getBodySb(sb_response_body) catch {};
    const j_resp = try chilkat.JsonObject.init();
    defer j_resp.deinit();
    j_resp.loadSb(sb_response_body) catch {};
    j_resp.setEmitCompact(false);

    std.debug.print("Response Body:\n", .{});
    std.debug.print("{s}\n", .{try j_resp.emit(alloc)});

    const resp_status_code = resp.getStatusCode();
    std.debug.print("Response Status Code = {d}\n", .{resp_status_code});
    if (resp_status_code >= 400) {
        std.debug.print("Response Header:\n", .{});
        std.debug.print("{s}\n", .{try resp.getHeader(alloc)});
        std.debug.print("Failed.\n", .{});
        return;
    }

    // Sample JSON response:
    // (Sample code for parsing the JSON response is shown below)

    // {
    //   "id": "ab2222df-73da-0e92-6887-5705428f4d68",
    //   "name": "Test Record",
    //   "date_entered": "2016-04-06T13:07:41-04:00",
    //   "date_modified": "2016-04-06T13:07:41-04:00",
    //   "modified_user_id": "1",
    //   "modified_by_name": "Administrator",
    //   "modified_user_link": {
    //     "full_name": "Administrator",
    //     "id": "1",
    //     "_acl": {
    //       "fields": [
    //       ],
    //       "delete": "no",
    //       "_hash": "8e11bf9be8f04daddee9d08d44ea891e"
    //     }
    //   },
    //   "created_by": "1",
    //   "created_by_name": "Administrator",
    //   "created_by_link": {
    //     "full_name": "Administrator",
    //     "id": "1",
    //     "_acl": {
    //       "fields": [
    //       ],
    //       "delete": "no",
    //       "_hash": "8e11bf9be8f04daddee9d08d44ea891e"
    //     }
    //   },
    //   "description": "",
    //   "deleted": false,
    //   "facebook": "",
    //   "twitter": "",
    //   "googleplus": "",
    //   "account_type": "",
    //   "industry": "",
    //   "annual_revenue": "",
    //   "phone_fax": "",
    //   "billing_address_street": "",
    //   "billing_address_street_2": "",
    //   "billing_address_street_3": "",
    //   "billing_address_street_4": "",
    //   "billing_address_city": "",
    //   "billing_address_state": "",
    //   "billing_address_postalcode": "",
    //   "billing_address_country": "",
    //   "rating": "",
    //   "phone_office": "",
    //   "phone_alternate": "",
    //   "website": "",
    //   "ownership": "",
    //   "employees": "",
    //   "ticker_symbol": "",
    //   "shipping_address_street": "",
    //   "shipping_address_street_2": "",
    //   "shipping_address_street_3": "",
    //   "shipping_address_street_4": "",
    //   "shipping_address_city": "",
    //   "shipping_address_state": "",
    //   "shipping_address_postalcode": "",
    //   "shipping_address_country": "",
    //   "parent_id": "",
    //   "sic_code": "",
    //   "duns_num": "",
    //   "parent_name": "",
    //   "member_of": {
    //     "name": "",
    //     "id": "",
    //     "_acl": {
    //       "fields": [
    //       ],
    //       "_hash": "654d337e0e912edaa00dbb0fb3dc3c17"
    //     }
    //   },
    //   "campaign_id": "",
    //   "campaign_name": "",
    //   "campaign_accounts": {
    //     "name": "",
    //     "id": "",
    //     "_acl": {
    //       "fields": [
    //       ],
    //       "_hash": "654d337e0e912edaa00dbb0fb3dc3c17"
    //     }
    //   },
    //   "following": true,
    //   "my_favorite": false,
    //   "tag": [
    //   ],
    //   "assigned_user_id": "",
    //   "assigned_user_name": "",
    //   "assigned_user_link": {
    //     "full_name": "",
    //     "id": "",
    //     "_acl": {
    //       "fields": [
    //       ],
    //       "_hash": "654d337e0e912edaa00dbb0fb3dc3c17"
    //     }
    //   },
    //   "team_count": "",
    //   "team_count_link": {
    //     "team_count": "",
    //     "id": "1",
    //     "_acl": {
    //       "fields": [
    //       ],
    //       "_hash": "654d337e0e912edaa00dbb0fb3dc3c17"
    //     }
    //   },
    //   "team_name": [
    //     {
    //       "id": 1,
    //       "name": "Global",
    //       "name_2": "",
    //       "primary": true
    //     }
    //   ],
    //   "email": [
    //     {
    //       "email_address": "test@sugar.com",
    //       "invalid_email": false,
    //       "opt_out": false,
    //       "primary_address": true,
    //       "reply_to_address": false
    //     }
    //   ],
    //   "email1": "test@sugar.com",
    //   "email2": "",
    //   "invalid_email": false,
    //   "email_opt_out": false,
    //   "email_addresses_non_primary": "",
    //   "_acl": {
    //     "fields": {}
    //   },
    //   "_module": "Accounts"
    // }

    // Sample code for parsing the JSON response...
    // Use the following online tool to generate parsing code from sample JSON:
    // Generate Parsing Code from JSON

    var name: [:0]const u8 = "";
    var invalid_email: bool = false;
    var i: i32 = 0;
    var count_i: i32 = 0;
    var id_int: i32 = 0;
    var name_2: [:0]const u8 = "";
    var primary: bool = false;
    var email_address: [:0]const u8 = "";
    var opt_out: bool = false;
    var primary_address: bool = false;
    var reply_to_address: bool = false;

    _ = try j_resp.stringOf(alloc, "id");
    name = try j_resp.stringOf(alloc, "name");
    _ = try j_resp.stringOf(alloc, "date_entered");
    _ = try j_resp.stringOf(alloc, "date_modified");
    _ = try j_resp.stringOf(alloc, "modified_user_id");
    _ = try j_resp.stringOf(alloc, "modified_by_name");
    _ = try j_resp.stringOf(alloc, "modified_user_link.full_name");
    _ = try j_resp.stringOf(alloc, "modified_user_link.id");
    _ = try j_resp.stringOf(alloc, "modified_user_link._acl.delete");
    _ = try j_resp.stringOf(alloc, "modified_user_link._acl._hash");
    _ = try j_resp.stringOf(alloc, "created_by");
    _ = try j_resp.stringOf(alloc, "created_by_name");
    _ = try j_resp.stringOf(alloc, "created_by_link.full_name");
    _ = try j_resp.stringOf(alloc, "created_by_link.id");
    _ = try j_resp.stringOf(alloc, "created_by_link._acl.delete");
    _ = try j_resp.stringOf(alloc, "created_by_link._acl._hash");
    _ = try j_resp.stringOf(alloc, "description");
    _ = j_resp.boolOf("deleted");
    _ = try j_resp.stringOf(alloc, "facebook");
    _ = try j_resp.stringOf(alloc, "twitter");
    _ = try j_resp.stringOf(alloc, "googleplus");
    _ = try j_resp.stringOf(alloc, "account_type");
    _ = try j_resp.stringOf(alloc, "industry");
    _ = try j_resp.stringOf(alloc, "annual_revenue");
    _ = try j_resp.stringOf(alloc, "phone_fax");
    _ = try j_resp.stringOf(alloc, "billing_address_street");
    _ = try j_resp.stringOf(alloc, "billing_address_street_2");
    _ = try j_resp.stringOf(alloc, "billing_address_street_3");
    _ = try j_resp.stringOf(alloc, "billing_address_street_4");
    _ = try j_resp.stringOf(alloc, "billing_address_city");
    _ = try j_resp.stringOf(alloc, "billing_address_state");
    _ = try j_resp.stringOf(alloc, "billing_address_postalcode");
    _ = try j_resp.stringOf(alloc, "billing_address_country");
    _ = try j_resp.stringOf(alloc, "rating");
    _ = try j_resp.stringOf(alloc, "phone_office");
    _ = try j_resp.stringOf(alloc, "phone_alternate");
    _ = try j_resp.stringOf(alloc, "website");
    _ = try j_resp.stringOf(alloc, "ownership");
    _ = try j_resp.stringOf(alloc, "employees");
    _ = try j_resp.stringOf(alloc, "ticker_symbol");
    _ = try j_resp.stringOf(alloc, "shipping_address_street");
    _ = try j_resp.stringOf(alloc, "shipping_address_street_2");
    _ = try j_resp.stringOf(alloc, "shipping_address_street_3");
    _ = try j_resp.stringOf(alloc, "shipping_address_street_4");
    _ = try j_resp.stringOf(alloc, "shipping_address_city");
    _ = try j_resp.stringOf(alloc, "shipping_address_state");
    _ = try j_resp.stringOf(alloc, "shipping_address_postalcode");
    _ = try j_resp.stringOf(alloc, "shipping_address_country");
    _ = try j_resp.stringOf(alloc, "parent_id");
    _ = try j_resp.stringOf(alloc, "sic_code");
    _ = try j_resp.stringOf(alloc, "duns_num");
    _ = try j_resp.stringOf(alloc, "parent_name");
    _ = try j_resp.stringOf(alloc, "member_of.name");
    _ = try j_resp.stringOf(alloc, "member_of.id");
    _ = try j_resp.stringOf(alloc, "member_of._acl._hash");
    _ = try j_resp.stringOf(alloc, "campaign_id");
    _ = try j_resp.stringOf(alloc, "campaign_name");
    _ = try j_resp.stringOf(alloc, "campaign_accounts.name");
    _ = try j_resp.stringOf(alloc, "campaign_accounts.id");
    _ = try j_resp.stringOf(alloc, "campaign_accounts._acl._hash");
    _ = j_resp.boolOf("following");
    _ = j_resp.boolOf("my_favorite");
    _ = try j_resp.stringOf(alloc, "assigned_user_id");
    _ = try j_resp.stringOf(alloc, "assigned_user_name");
    _ = try j_resp.stringOf(alloc, "assigned_user_link.full_name");
    _ = try j_resp.stringOf(alloc, "assigned_user_link.id");
    _ = try j_resp.stringOf(alloc, "assigned_user_link._acl._hash");
    _ = try j_resp.stringOf(alloc, "team_count");
    _ = try j_resp.stringOf(alloc, "team_count_link.team_count");
    _ = try j_resp.stringOf(alloc, "team_count_link.id");
    _ = try j_resp.stringOf(alloc, "team_count_link._acl._hash");
    _ = try j_resp.stringOf(alloc, "email1");
    _ = try j_resp.stringOf(alloc, "email2");
    invalid_email = j_resp.boolOf("invalid_email");
    _ = j_resp.boolOf("email_opt_out");
    _ = try j_resp.stringOf(alloc, "email_addresses_non_primary");
    _ = try j_resp.stringOf(alloc, "_module");
    i = 0;
    count_i = j_resp.sizeOfArray("modified_user_link._acl.fields");
    while (i < count_i) {
        j_resp.setI(i);
        i = i + 1;
    }

    i = 0;
    count_i = j_resp.sizeOfArray("created_by_link._acl.fields");
    while (i < count_i) {
        j_resp.setI(i);
        i = i + 1;
    }

    i = 0;
    count_i = j_resp.sizeOfArray("member_of._acl.fields");
    while (i < count_i) {
        j_resp.setI(i);
        i = i + 1;
    }

    i = 0;
    count_i = j_resp.sizeOfArray("campaign_accounts._acl.fields");
    while (i < count_i) {
        j_resp.setI(i);
        i = i + 1;
    }

    i = 0;
    count_i = j_resp.sizeOfArray("tag");
    while (i < count_i) {
        j_resp.setI(i);
        i = i + 1;
    }

    i = 0;
    count_i = j_resp.sizeOfArray("assigned_user_link._acl.fields");
    while (i < count_i) {
        j_resp.setI(i);
        i = i + 1;
    }

    i = 0;
    count_i = j_resp.sizeOfArray("team_count_link._acl.fields");
    while (i < count_i) {
        j_resp.setI(i);
        i = i + 1;
    }

    i = 0;
    count_i = j_resp.sizeOfArray("team_name");
    while (i < count_i) {
        j_resp.setI(i);
        id_int = j_resp.intOf("team_name[i].id");
        name = try j_resp.stringOf(alloc, "team_name[i].name");
        name_2 = try j_resp.stringOf(alloc, "team_name[i].name_2");
        primary = j_resp.boolOf("team_name[i].primary");
        i = i + 1;
    }

    i = 0;
    count_i = j_resp.sizeOfArray("email");
    while (i < count_i) {
        j_resp.setI(i);
        email_address = try j_resp.stringOf(alloc, "email[i].email_address");
        invalid_email = j_resp.boolOf("email[i].invalid_email");
        opt_out = j_resp.boolOf("email[i].opt_out");
        primary_address = j_resp.boolOf("email[i].primary_address");
        reply_to_address = j_resp.boolOf("email[i].reply_to_address");
        i = i + 1;
    }
}