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

SugarCRM: Importing Email Addresses (New Records)

See more SugarCRM Examples

Demonstrates how to import a new contact with email addresses.

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 requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

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

    http.setAccept("application/json");

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

    // {
    //   "first_name": "Rob",
    //   "last_name": "Robertson",
    //   "email": [
    //     {
    //       "email_address": "rob.robertson@sugar.crm",
    //       "primary_address": "1",
    //       "invalid_email": "0",
    //       "opt_out": "0"
    //     },
    //     {
    //       "email_address": "rob@sugar.crm",
    //       "primary_address": "0",
    //       "invalid_email": "0",
    //       "opt_out": "1"
    //     }
    //   ]
    // }

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

    const json_request_body = try chilkat.JsonObject.init();
    defer json_request_body.deinit();
    json_request_body.updateString("first_name", "Rob") catch {};
    json_request_body.updateString("last_name", "Robertson") catch {};
    json_request_body.updateString("email[0].email_address", "rob.robertson@sugar.crm") catch {};
    json_request_body.updateString("email[0].primary_address", "1") catch {};
    json_request_body.updateString("email[0].invalid_email", "0") catch {};
    json_request_body.updateString("email[0].opt_out", "0") catch {};
    json_request_body.updateString("email[1].email_address", "rob@sugar.crm") catch {};
    json_request_body.updateString("email[1].primary_address", "0") catch {};
    json_request_body.updateString("email[1].invalid_email", "0") catch {};
    json_request_body.updateString("email[1].opt_out", "1") catch {};

    const url = "http://<site url>/rest/v10/Contacts";

    http.setRequestHeader("OAuth-Token", "ACCESS_TOKEN");

    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpJson("POST", url, json_request_body, "application/json", resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("Response Status Code: {d}\n", .{resp.getStatusCode()});

    const json_response = try chilkat.JsonObject.init();
    defer json_response.deinit();
    json_response.load(try resp.getBodyStr(alloc)) catch {};
    json_response.setEmitCompact(false);
    std.debug.print("{s}\n", .{try json_response.emit(alloc)});

    if (resp.getStatusCode() >= 300) {
        std.debug.print("Failed.\n", .{});
        return;
    }
}