C
/:record endpoint, and the PUT Http method. In this example we update the Account record and change it's name to "Updated Test Record".
C
SugarCRM Updating a Record
See more SugarCRM Examples
Demonstrates how to update a record in the Sugar instance using the /Chilkat C Downloads
#include <C_CkHttp.h>
#include <C_CkJsonObject.h>
#include <C_CkStringBuilder.h>
#include <C_CkHttpResponse.h>
void ChilkatSample(void)
{
BOOL success;
HCkHttp http;
HCkJsonObject json;
HCkStringBuilder sbRequestBody;
HCkHttpResponse resp;
HCkStringBuilder sbResponseBody;
HCkJsonObject jResp;
int respStatusCode;
const char *id;
const char *name;
const char *date_entered;
const char *date_modified;
const char *modified_user_id;
const char *modified_by_name;
const char *modified_user_linkFull_name;
const char *modified_user_linkId;
const char *modified_user_link_aclDelete;
const char *modified_user_link_acl_hash;
const char *created_by;
const char *created_by_name;
const char *created_by_linkFull_name;
const char *created_by_linkId;
const char *created_by_link_aclDelete;
const char *created_by_link_acl_hash;
const char *description;
BOOL deleted;
const char *facebook;
const char *twitter;
const char *googleplus;
const char *account_type;
const char *industry;
const char *annual_revenue;
const char *phone_fax;
const char *billing_address_street;
const char *billing_address_street_2;
const char *billing_address_street_3;
const char *billing_address_street_4;
const char *billing_address_city;
const char *billing_address_state;
const char *billing_address_postalcode;
const char *billing_address_country;
const char *rating;
const char *phone_office;
const char *phone_alternate;
const char *website;
const char *ownership;
const char *employees;
const char *ticker_symbol;
const char *shipping_address_street;
const char *shipping_address_street_2;
const char *shipping_address_street_3;
const char *shipping_address_street_4;
const char *shipping_address_city;
const char *shipping_address_state;
const char *shipping_address_postalcode;
const char *shipping_address_country;
const char *parent_id;
const char *sic_code;
const char *duns_num;
const char *parent_name;
const char *member_ofName;
const char *member_ofId;
const char *member_of_acl_hash;
const char *campaign_id;
const char *campaign_name;
const char *campaign_accountsName;
const char *campaign_accountsId;
const char *campaign_accounts_acl_hash;
BOOL following;
BOOL my_favorite;
const char *assigned_user_id;
const char *assigned_user_name;
const char *assigned_user_linkFull_name;
const char *assigned_user_linkId;
const char *assigned_user_link_acl_hash;
const char *team_count;
const char *team_count_linkTeam_count;
const char *team_count_linkId;
const char *team_count_link_acl_hash;
const char *email1;
const char *email2;
BOOL invalid_email;
BOOL email_opt_out;
const char *email_addresses_non_primary;
const char *v_module;
int i;
int count_i;
int id_int;
const char *name_2;
BOOL primary;
const char *email_address;
BOOL opt_out;
BOOL primary_address;
BOOL reply_to_address;
success = FALSE;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
http = CkHttp_Create();
// Implements the following CURL command:
// curl -X PUT -H OAuth-Token:<access_token> -H Cache-Control:no-cache -d '{
// "name":"Updated Record"
// }' http://<site_url>/rest/v10/Accounts/<record_id>
// 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": "Updated Record"
// }
json = CkJsonObject_Create();
CkJsonObject_UpdateString(json,"name","Updated Record");
CkHttp_SetRequestHeader(http,"Cache-Control","no-cache");
CkHttp_SetRequestHeader(http,"OAuth-Token","<access_token>");
sbRequestBody = CkStringBuilder_Create();
CkJsonObject_EmitSb(json,sbRequestBody);
resp = CkHttpResponse_Create();
success = CkHttp_HttpSb(http,"PUT","http://<site_url>/rest/v10/Accounts/<record_id>",sbRequestBody,"utf-8","application/json",resp);
if (success == FALSE) {
printf("%s\n",CkHttp_lastErrorText(http));
CkHttp_Dispose(http);
CkJsonObject_Dispose(json);
CkStringBuilder_Dispose(sbRequestBody);
CkHttpResponse_Dispose(resp);
return;
}
sbResponseBody = CkStringBuilder_Create();
CkHttpResponse_GetBodySb(resp,sbResponseBody);
jResp = CkJsonObject_Create();
CkJsonObject_LoadSb(jResp,sbResponseBody);
CkJsonObject_putEmitCompact(jResp,FALSE);
printf("Response Body:\n");
printf("%s\n",CkJsonObject_emit(jResp));
respStatusCode = CkHttpResponse_getStatusCode(resp);
printf("Response Status Code = %d\n",respStatusCode);
if (respStatusCode >= 400) {
printf("Response Header:\n");
printf("%s\n",CkHttpResponse_header(resp));
printf("Failed.\n");
CkHttp_Dispose(http);
CkJsonObject_Dispose(json);
CkStringBuilder_Dispose(sbRequestBody);
CkHttpResponse_Dispose(resp);
CkStringBuilder_Dispose(sbResponseBody);
CkJsonObject_Dispose(jResp);
return;
}
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// {
// "id": "ab2222df-73da-0e92-6887-5705428f4d68",
// "name": "Updated Test Record",
// "date_entered": "2016-04-06T15:03:21-04:00",
// "date_modified": "2016-04-06T15:03:22-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
id = CkJsonObject_stringOf(jResp,"id");
name = CkJsonObject_stringOf(jResp,"name");
date_entered = CkJsonObject_stringOf(jResp,"date_entered");
date_modified = CkJsonObject_stringOf(jResp,"date_modified");
modified_user_id = CkJsonObject_stringOf(jResp,"modified_user_id");
modified_by_name = CkJsonObject_stringOf(jResp,"modified_by_name");
modified_user_linkFull_name = CkJsonObject_stringOf(jResp,"modified_user_link.full_name");
modified_user_linkId = CkJsonObject_stringOf(jResp,"modified_user_link.id");
modified_user_link_aclDelete = CkJsonObject_stringOf(jResp,"modified_user_link._acl.delete");
modified_user_link_acl_hash = CkJsonObject_stringOf(jResp,"modified_user_link._acl._hash");
created_by = CkJsonObject_stringOf(jResp,"created_by");
created_by_name = CkJsonObject_stringOf(jResp,"created_by_name");
created_by_linkFull_name = CkJsonObject_stringOf(jResp,"created_by_link.full_name");
created_by_linkId = CkJsonObject_stringOf(jResp,"created_by_link.id");
created_by_link_aclDelete = CkJsonObject_stringOf(jResp,"created_by_link._acl.delete");
created_by_link_acl_hash = CkJsonObject_stringOf(jResp,"created_by_link._acl._hash");
description = CkJsonObject_stringOf(jResp,"description");
deleted = CkJsonObject_BoolOf(jResp,"deleted");
facebook = CkJsonObject_stringOf(jResp,"facebook");
twitter = CkJsonObject_stringOf(jResp,"twitter");
googleplus = CkJsonObject_stringOf(jResp,"googleplus");
account_type = CkJsonObject_stringOf(jResp,"account_type");
industry = CkJsonObject_stringOf(jResp,"industry");
annual_revenue = CkJsonObject_stringOf(jResp,"annual_revenue");
phone_fax = CkJsonObject_stringOf(jResp,"phone_fax");
billing_address_street = CkJsonObject_stringOf(jResp,"billing_address_street");
billing_address_street_2 = CkJsonObject_stringOf(jResp,"billing_address_street_2");
billing_address_street_3 = CkJsonObject_stringOf(jResp,"billing_address_street_3");
billing_address_street_4 = CkJsonObject_stringOf(jResp,"billing_address_street_4");
billing_address_city = CkJsonObject_stringOf(jResp,"billing_address_city");
billing_address_state = CkJsonObject_stringOf(jResp,"billing_address_state");
billing_address_postalcode = CkJsonObject_stringOf(jResp,"billing_address_postalcode");
billing_address_country = CkJsonObject_stringOf(jResp,"billing_address_country");
rating = CkJsonObject_stringOf(jResp,"rating");
phone_office = CkJsonObject_stringOf(jResp,"phone_office");
phone_alternate = CkJsonObject_stringOf(jResp,"phone_alternate");
website = CkJsonObject_stringOf(jResp,"website");
ownership = CkJsonObject_stringOf(jResp,"ownership");
employees = CkJsonObject_stringOf(jResp,"employees");
ticker_symbol = CkJsonObject_stringOf(jResp,"ticker_symbol");
shipping_address_street = CkJsonObject_stringOf(jResp,"shipping_address_street");
shipping_address_street_2 = CkJsonObject_stringOf(jResp,"shipping_address_street_2");
shipping_address_street_3 = CkJsonObject_stringOf(jResp,"shipping_address_street_3");
shipping_address_street_4 = CkJsonObject_stringOf(jResp,"shipping_address_street_4");
shipping_address_city = CkJsonObject_stringOf(jResp,"shipping_address_city");
shipping_address_state = CkJsonObject_stringOf(jResp,"shipping_address_state");
shipping_address_postalcode = CkJsonObject_stringOf(jResp,"shipping_address_postalcode");
shipping_address_country = CkJsonObject_stringOf(jResp,"shipping_address_country");
parent_id = CkJsonObject_stringOf(jResp,"parent_id");
sic_code = CkJsonObject_stringOf(jResp,"sic_code");
duns_num = CkJsonObject_stringOf(jResp,"duns_num");
parent_name = CkJsonObject_stringOf(jResp,"parent_name");
member_ofName = CkJsonObject_stringOf(jResp,"member_of.name");
member_ofId = CkJsonObject_stringOf(jResp,"member_of.id");
member_of_acl_hash = CkJsonObject_stringOf(jResp,"member_of._acl._hash");
campaign_id = CkJsonObject_stringOf(jResp,"campaign_id");
campaign_name = CkJsonObject_stringOf(jResp,"campaign_name");
campaign_accountsName = CkJsonObject_stringOf(jResp,"campaign_accounts.name");
campaign_accountsId = CkJsonObject_stringOf(jResp,"campaign_accounts.id");
campaign_accounts_acl_hash = CkJsonObject_stringOf(jResp,"campaign_accounts._acl._hash");
following = CkJsonObject_BoolOf(jResp,"following");
my_favorite = CkJsonObject_BoolOf(jResp,"my_favorite");
assigned_user_id = CkJsonObject_stringOf(jResp,"assigned_user_id");
assigned_user_name = CkJsonObject_stringOf(jResp,"assigned_user_name");
assigned_user_linkFull_name = CkJsonObject_stringOf(jResp,"assigned_user_link.full_name");
assigned_user_linkId = CkJsonObject_stringOf(jResp,"assigned_user_link.id");
assigned_user_link_acl_hash = CkJsonObject_stringOf(jResp,"assigned_user_link._acl._hash");
team_count = CkJsonObject_stringOf(jResp,"team_count");
team_count_linkTeam_count = CkJsonObject_stringOf(jResp,"team_count_link.team_count");
team_count_linkId = CkJsonObject_stringOf(jResp,"team_count_link.id");
team_count_link_acl_hash = CkJsonObject_stringOf(jResp,"team_count_link._acl._hash");
email1 = CkJsonObject_stringOf(jResp,"email1");
email2 = CkJsonObject_stringOf(jResp,"email2");
invalid_email = CkJsonObject_BoolOf(jResp,"invalid_email");
email_opt_out = CkJsonObject_BoolOf(jResp,"email_opt_out");
email_addresses_non_primary = CkJsonObject_stringOf(jResp,"email_addresses_non_primary");
v_module = CkJsonObject_stringOf(jResp,"_module");
i = 0;
count_i = CkJsonObject_SizeOfArray(jResp,"modified_user_link._acl.fields");
while (i < count_i) {
CkJsonObject_putI(jResp,i);
i = i + 1;
}
i = 0;
count_i = CkJsonObject_SizeOfArray(jResp,"created_by_link._acl.fields");
while (i < count_i) {
CkJsonObject_putI(jResp,i);
i = i + 1;
}
i = 0;
count_i = CkJsonObject_SizeOfArray(jResp,"member_of._acl.fields");
while (i < count_i) {
CkJsonObject_putI(jResp,i);
i = i + 1;
}
i = 0;
count_i = CkJsonObject_SizeOfArray(jResp,"campaign_accounts._acl.fields");
while (i < count_i) {
CkJsonObject_putI(jResp,i);
i = i + 1;
}
i = 0;
count_i = CkJsonObject_SizeOfArray(jResp,"tag");
while (i < count_i) {
CkJsonObject_putI(jResp,i);
i = i + 1;
}
i = 0;
count_i = CkJsonObject_SizeOfArray(jResp,"assigned_user_link._acl.fields");
while (i < count_i) {
CkJsonObject_putI(jResp,i);
i = i + 1;
}
i = 0;
count_i = CkJsonObject_SizeOfArray(jResp,"team_count_link._acl.fields");
while (i < count_i) {
CkJsonObject_putI(jResp,i);
i = i + 1;
}
i = 0;
count_i = CkJsonObject_SizeOfArray(jResp,"team_name");
while (i < count_i) {
CkJsonObject_putI(jResp,i);
id_int = CkJsonObject_IntOf(jResp,"team_name[i].id");
name = CkJsonObject_stringOf(jResp,"team_name[i].name");
name_2 = CkJsonObject_stringOf(jResp,"team_name[i].name_2");
primary = CkJsonObject_BoolOf(jResp,"team_name[i].primary");
i = i + 1;
}
i = 0;
count_i = CkJsonObject_SizeOfArray(jResp,"email");
while (i < count_i) {
CkJsonObject_putI(jResp,i);
email_address = CkJsonObject_stringOf(jResp,"email[i].email_address");
invalid_email = CkJsonObject_BoolOf(jResp,"email[i].invalid_email");
opt_out = CkJsonObject_BoolOf(jResp,"email[i].opt_out");
primary_address = CkJsonObject_BoolOf(jResp,"email[i].primary_address");
reply_to_address = CkJsonObject_BoolOf(jResp,"email[i].reply_to_address");
i = i + 1;
}
CkHttp_Dispose(http);
CkJsonObject_Dispose(json);
CkStringBuilder_Dispose(sbRequestBody);
CkHttpResponse_Dispose(resp);
CkStringBuilder_Dispose(sbResponseBody);
CkJsonObject_Dispose(jResp);
}