Lua Requires Chilkat v11.0.0+
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'.
Lua
SugarCRM Create a Record
See more SugarCRM Examples
Submit a record to the Sugar instance using the /Chilkat Lua Downloads
local chilkat = require("chilkat")
-- This example assumes the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
local http = chilkat.Http.new()
-- 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"
-- }
local json = chilkat.JsonObject.new()
json:update_string("name", "Test Record")
json:update_string("email1", "test@sugar.com")
http:set_request_header("Cache-Control", "no-cache")
http:set_request_header("OAuth-Token", "<access_token>")
local resp = chilkat.HttpResponse.new()
local success = http:http_json("POST", "http://<site_url>/rest/v10/Accounts", json, "application/json", resp)
if not success then
print(http.last_error_text)
return
end
local sbResponseBody = chilkat.StringBuilder.new()
resp:get_body_sb(sbResponseBody)
local jResp = chilkat.JsonObject.new()
jResp:load_sb(sbResponseBody)
jResp.emit_compact = false
print("Response Body:")
print(jResp:emit())
local respStatusCode = resp.status_code
print("Response Status Code = " .. respStatusCode)
if respStatusCode >= 400 then
print("Response Header:")
print(resp.header)
print("Failed.")
return
end
-- 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
local id_int = 0
local name_2 = ""
local primary = false
local email_address = ""
local opt_out = false
local primary_address = false
local reply_to_address = false
local id = jResp:string_of("id")
local name = jResp:string_of("name")
local date_entered = jResp:string_of("date_entered")
local date_modified = jResp:string_of("date_modified")
local modified_user_id = jResp:string_of("modified_user_id")
local modified_by_name = jResp:string_of("modified_by_name")
local modified_user_linkFull_name = jResp:string_of("modified_user_link.full_name")
local modified_user_linkId = jResp:string_of("modified_user_link.id")
local modified_user_link_aclDelete = jResp:string_of("modified_user_link._acl.delete")
local modified_user_link_acl_hash = jResp:string_of("modified_user_link._acl._hash")
local created_by = jResp:string_of("created_by")
local created_by_name = jResp:string_of("created_by_name")
local created_by_linkFull_name = jResp:string_of("created_by_link.full_name")
local created_by_linkId = jResp:string_of("created_by_link.id")
local created_by_link_aclDelete = jResp:string_of("created_by_link._acl.delete")
local created_by_link_acl_hash = jResp:string_of("created_by_link._acl._hash")
local description = jResp:string_of("description")
local deleted = jResp:bool_of("deleted")
local facebook = jResp:string_of("facebook")
local twitter = jResp:string_of("twitter")
local googleplus = jResp:string_of("googleplus")
local account_type = jResp:string_of("account_type")
local industry = jResp:string_of("industry")
local annual_revenue = jResp:string_of("annual_revenue")
local phone_fax = jResp:string_of("phone_fax")
local billing_address_street = jResp:string_of("billing_address_street")
local billing_address_street_2 = jResp:string_of("billing_address_street_2")
local billing_address_street_3 = jResp:string_of("billing_address_street_3")
local billing_address_street_4 = jResp:string_of("billing_address_street_4")
local billing_address_city = jResp:string_of("billing_address_city")
local billing_address_state = jResp:string_of("billing_address_state")
local billing_address_postalcode = jResp:string_of("billing_address_postalcode")
local billing_address_country = jResp:string_of("billing_address_country")
local rating = jResp:string_of("rating")
local phone_office = jResp:string_of("phone_office")
local phone_alternate = jResp:string_of("phone_alternate")
local website = jResp:string_of("website")
local ownership = jResp:string_of("ownership")
local employees = jResp:string_of("employees")
local ticker_symbol = jResp:string_of("ticker_symbol")
local shipping_address_street = jResp:string_of("shipping_address_street")
local shipping_address_street_2 = jResp:string_of("shipping_address_street_2")
local shipping_address_street_3 = jResp:string_of("shipping_address_street_3")
local shipping_address_street_4 = jResp:string_of("shipping_address_street_4")
local shipping_address_city = jResp:string_of("shipping_address_city")
local shipping_address_state = jResp:string_of("shipping_address_state")
local shipping_address_postalcode = jResp:string_of("shipping_address_postalcode")
local shipping_address_country = jResp:string_of("shipping_address_country")
local parent_id = jResp:string_of("parent_id")
local sic_code = jResp:string_of("sic_code")
local duns_num = jResp:string_of("duns_num")
local parent_name = jResp:string_of("parent_name")
local member_ofName = jResp:string_of("member_of.name")
local member_ofId = jResp:string_of("member_of.id")
local member_of_acl_hash = jResp:string_of("member_of._acl._hash")
local campaign_id = jResp:string_of("campaign_id")
local campaign_name = jResp:string_of("campaign_name")
local campaign_accountsName = jResp:string_of("campaign_accounts.name")
local campaign_accountsId = jResp:string_of("campaign_accounts.id")
local campaign_accounts_acl_hash = jResp:string_of("campaign_accounts._acl._hash")
local following = jResp:bool_of("following")
local my_favorite = jResp:bool_of("my_favorite")
local assigned_user_id = jResp:string_of("assigned_user_id")
local assigned_user_name = jResp:string_of("assigned_user_name")
local assigned_user_linkFull_name = jResp:string_of("assigned_user_link.full_name")
local assigned_user_linkId = jResp:string_of("assigned_user_link.id")
local assigned_user_link_acl_hash = jResp:string_of("assigned_user_link._acl._hash")
local team_count = jResp:string_of("team_count")
local team_count_linkTeam_count = jResp:string_of("team_count_link.team_count")
local team_count_linkId = jResp:string_of("team_count_link.id")
local team_count_link_acl_hash = jResp:string_of("team_count_link._acl._hash")
local email1 = jResp:string_of("email1")
local email2 = jResp:string_of("email2")
local invalid_email = jResp:bool_of("invalid_email")
local email_opt_out = jResp:bool_of("email_opt_out")
local email_addresses_non_primary = jResp:string_of("email_addresses_non_primary")
local v_module = jResp:string_of("_module")
local i = 0
local count_i = jResp:size_of_array("modified_user_link._acl.fields")
while i < count_i do
jResp.i = i
i = i + 1
end
i = 0
count_i = jResp:size_of_array("created_by_link._acl.fields")
while i < count_i do
jResp.i = i
i = i + 1
end
i = 0
count_i = jResp:size_of_array("member_of._acl.fields")
while i < count_i do
jResp.i = i
i = i + 1
end
i = 0
count_i = jResp:size_of_array("campaign_accounts._acl.fields")
while i < count_i do
jResp.i = i
i = i + 1
end
i = 0
count_i = jResp:size_of_array("tag")
while i < count_i do
jResp.i = i
i = i + 1
end
i = 0
count_i = jResp:size_of_array("assigned_user_link._acl.fields")
while i < count_i do
jResp.i = i
i = i + 1
end
i = 0
count_i = jResp:size_of_array("team_count_link._acl.fields")
while i < count_i do
jResp.i = i
i = i + 1
end
i = 0
count_i = jResp:size_of_array("team_name")
while i < count_i do
jResp.i = i
id_int = jResp:int_of("team_name[i].id")
name = jResp:string_of("team_name[i].name")
name_2 = jResp:string_of("team_name[i].name_2")
primary = jResp:bool_of("team_name[i].primary")
i = i + 1
end
i = 0
count_i = jResp:size_of_array("email")
while i < count_i do
jResp.i = i
email_address = jResp:string_of("email[i].email_address")
invalid_email = jResp:bool_of("email[i].invalid_email")
opt_out = jResp:bool_of("email[i].opt_out")
primary_address = jResp:bool_of("email[i].primary_address")
reply_to_address = jResp:bool_of("email[i].reply_to_address")
i = i + 1
end