Sample code for 30+ languages & platforms
DataFlex

SugarCRM: Importing Email Addresses (New Records)

See more SugarCRM Examples

Demonstrates how to import a new contact with email addresses.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoHttp
    Variant vJsonRequestBody
    Handle hoJsonRequestBody
    String sUrl
    Variant vResp
    Handle hoResp
    Handle hoJsonResponse
    String sTemp1
    Integer iTemp1

    Move False To iSuccess

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

    Get Create (RefClass(cComChilkatHttp)) To hoHttp
    If (Not(IsComObjectCreated(hoHttp))) Begin
        Send CreateComObject of hoHttp
    End

    Set ComAccept Of hoHttp To "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

    Get Create (RefClass(cComChilkatJsonObject)) To hoJsonRequestBody
    If (Not(IsComObjectCreated(hoJsonRequestBody))) Begin
        Send CreateComObject of hoJsonRequestBody
    End
    Get ComUpdateString Of hoJsonRequestBody "first_name" "Rob" To iSuccess
    Get ComUpdateString Of hoJsonRequestBody "last_name" "Robertson" To iSuccess
    Get ComUpdateString Of hoJsonRequestBody "email[0].email_address" "rob.robertson@sugar.crm" To iSuccess
    Get ComUpdateString Of hoJsonRequestBody "email[0].primary_address" "1" To iSuccess
    Get ComUpdateString Of hoJsonRequestBody "email[0].invalid_email" "0" To iSuccess
    Get ComUpdateString Of hoJsonRequestBody "email[0].opt_out" "0" To iSuccess
    Get ComUpdateString Of hoJsonRequestBody "email[1].email_address" "rob@sugar.crm" To iSuccess
    Get ComUpdateString Of hoJsonRequestBody "email[1].primary_address" "0" To iSuccess
    Get ComUpdateString Of hoJsonRequestBody "email[1].invalid_email" "0" To iSuccess
    Get ComUpdateString Of hoJsonRequestBody "email[1].opt_out" "1" To iSuccess

    Move "http://<site url>/rest/v10/Contacts" To sUrl

    Send ComSetRequestHeader To hoHttp "OAuth-Token" "ACCESS_TOKEN"

    Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
    If (Not(IsComObjectCreated(hoResp))) Begin
        Send CreateComObject of hoResp
    End
    Get pvComObject of hoJsonRequestBody to vJsonRequestBody
    Get pvComObject of hoResp to vResp
    Get ComHttpJson Of hoHttp "POST" sUrl vJsonRequestBody "application/json" vResp To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoHttp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComStatusCode Of hoResp To iTemp1
    Showln "Response Status Code: " iTemp1

    Get Create (RefClass(cComChilkatJsonObject)) To hoJsonResponse
    If (Not(IsComObjectCreated(hoJsonResponse))) Begin
        Send CreateComObject of hoJsonResponse
    End
    Get ComBodyStr Of hoResp To sTemp1
    Get ComLoad Of hoJsonResponse sTemp1 To iSuccess
    Set ComEmitCompact Of hoJsonResponse To False
    Get ComEmit Of hoJsonResponse To sTemp1
    Showln sTemp1

    Get ComStatusCode Of hoResp To iTemp1
    If (iTemp1 >= 300) Begin
        Showln "Failed."
        Procedure_Return
    End



End_Procedure