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

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonObject;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example requires the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        Http http = new Http();

        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

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

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

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

        HttpResponse resp = new HttpResponse();
        try {
            http.httpJson("POST", url, jsonRequestBody, "application/json", resp);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("Response Status Code: " + resp.getStatusCode());

        JsonObject jsonResponse = new JsonObject();
        jsonResponse.load(resp.getBodyStr());
        jsonResponse.setEmitCompact(false);
        System.out.println(jsonResponse.emit());

        if (resp.getStatusCode() >= 300) {
            System.out.println("Failed.");
            return;
        }
    }
}