Sample code for 30+ languages & platforms
Lua 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 Lua Downloads

Lua
local chilkat = require("chilkat")

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

local http = chilkat.Http.new()

http.accept = "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

local jsonRequestBody = chilkat.JsonObject.new()
jsonRequestBody:update_string("first_name", "Rob")
jsonRequestBody:update_string("last_name", "Robertson")
jsonRequestBody:update_string("email[0].email_address", "rob.robertson@sugar.crm")
jsonRequestBody:update_string("email[0].primary_address", "1")
jsonRequestBody:update_string("email[0].invalid_email", "0")
jsonRequestBody:update_string("email[0].opt_out", "0")
jsonRequestBody:update_string("email[1].email_address", "rob@sugar.crm")
jsonRequestBody:update_string("email[1].primary_address", "0")
jsonRequestBody:update_string("email[1].invalid_email", "0")
jsonRequestBody:update_string("email[1].opt_out", "1")

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

http:set_request_header("OAuth-Token", "ACCESS_TOKEN")

local resp = chilkat.HttpResponse.new()
local success = http:http_json("POST", url, jsonRequestBody, "application/json", resp)
if not success then
    print(http.last_error_text)
    return
end

print("Response Status Code: " .. resp.status_code)

local jsonResponse = chilkat.JsonObject.new()
jsonResponse:load(resp.body_str)
jsonResponse.emit_compact = false
print(jsonResponse:emit())

if resp.status_code >= 300 then
    print("Failed.")
    return
end