Node.js SugarCRM: SugarCRM Get Accounts

Back to Index

Retrieves (exports) account records for accounts having names beginning with "C" and "D".
Note: This example uses our own demo SugarCRM domain containing demo data. You should replace "cscspanset.demo.sugarcrm.eu" with your own domain.

Documentation: http://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_7.8/Integration/Web_Services/v10/Examples/Bash/How_to_Export_a_List_of_Records/


var os = require('os');
if (os.platform() == 'win32') {  
    var chilkat = require('chilkat_node6_win32'); 
} else if (os.platform() == 'linux') {
    if (os.arch() == 'arm') {
        var chilkat = require('chilkat_node6_arm');
    } else if (os.arch() == 'x86') {
        var chilkat = require('chilkat_node6_linux32');
    } else {
        var chilkat = require('chilkat_node6_linux64');
    }
} else if (os.platform() == 'darwin') {
    var chilkat = require('chilkat_node6_macosx');
}

function chilkatExample() {

    var rest = new chilkat.Rest();
    var success;

    success = rest.Connect("cscspanset.demo.sugarcrm.eu",443,true,true);
    if (success !== true) {
        console.log(rest.LastErrorText);
        return;
    }

    rest.AddHeader("Cache-Control","no-cache");
    rest.AddHeader("OAuth-Token","OAUTH2_ACCESS_TOKEN");

    //  The following code creates the JSON request body.
    //  The JSON created by this code is shown below.
    var jsonReq = new chilkat.JsonObject();
    jsonReq.UpdateString("filter[0].$or[0].name.$starts","C");
    jsonReq.UpdateString("filter[0].$or[1].name.$starts","D");
    jsonReq.UpdateNumber("max_num","20");
    jsonReq.UpdateNumber("offset","0");
    jsonReq.UpdateString("fields","id,name,billing_address_city,billing_address_country");
    jsonReq.UpdateString("order_by","name");
    jsonReq.UpdateBool("favorites",false);
    jsonReq.UpdateBool("my_items",false);

    var sbReq = new chilkat.StringBuilder();
    jsonReq.EmitSb(sbReq);

    rest.AddHeader("Content-Type","application/json");

    var sbJson = new chilkat.StringBuilder();
    success = rest.FullRequestSb("POST","/rest/v10/Accounts/filter",sbReq,sbJson);
    if (success !== true) {
        console.log(rest.LastErrorText);
        return;
    }

    if (rest.ResponseStatusCode !== 200) {
        console.log("Received error response code: " + rest.ResponseStatusCode);
        console.log("Response body:");
        console.log(sbJson.GetAsString());
        return;
    }

    var json = new chilkat.JsonObject();
    json.LoadSb(sbJson);

    //  The following code parses the JSON response.
    //  A sample JSON response is shown below the sample code.
    var next_offset;
    var i;
    var count_i;
    var id;
    var name;
    var date_modified;
    var billing_address_city;
    var billing_address_country;
    var v_module;
    var j;
    var count_j;

    next_offset = json.IntOf("next_offset");
    i = 0;
    count_i = json.SizeOfArray("records");
    while (i < count_i) {
        json.I = i;
        id = json.StringOf("records[i].id");
        name = json.StringOf("records[i].name");
        date_modified = json.StringOf("records[i].date_modified");
        billing_address_city = json.StringOf("records[i].billing_address_city");
        billing_address_country = json.StringOf("records[i].billing_address_country");
        v_module = json.StringOf("records[i]._module");
        j = 0;
        count_j = json.SizeOfArray("records[i].locked_fields");
        while (j < count_j) {
            json.J = j;
            j = j+1;
        }

        i = i+1;
    }

    console.log("Example Completed.");

}

chilkatExample();

Sample JSON Request Body

{  
   "filter":[  
      {  
         "$or":[  
            {  
               "name":{  
                  "$starts":"C"
               }
            },
            {  
               "name":{  
                  "$starts":"D"
               }
            }
         ]
      }
   ],
   "max_num":20,
   "offset":0,
   "fields":"id,name,billing_address_city,billing_address_country",
   "order_by":"name",
   "favorites":false,
   "my_items":false
}

Sample JSON Response Body

{
  "next_offset": -1,
  "records": [
    {
      "id": "134a8efe-acd5-11e6-8c5f-0681183120c1",
      "name": "CIDE Finanzplanungen",
      "date_modified": "2016-12-14T00:13:47+00:00",
      "billing_address_city": "Frankfurt",
      "billing_address_country": "Deutschland",
      "locked_fields": [
      ],
      "_acl": {
        "fields": {}
      },
      "_module": "Accounts"
    },
    {
      "id": "1401c29a-acd5-11e6-afb2-0681183120c1",
      "name": "Commercial Autovermietung",
      "date_modified": "2016-12-14T00:13:47+00:00",
      "billing_address_city": "M\u00fcnchen",
      "billing_address_country": "Deutschland",
      "locked_fields": [
      ],
      "_acl": {
        "fields": {}
      },
      "_module": "Accounts"
    },
    {
      "id": "106b0916-acd5-11e6-b7db-0681183120c1",
      "name": "Dellinger Speditionsbetrieb",
      "date_modified": "2016-12-14T00:13:47+00:00",
      "billing_address_city": "Frankfurt",
      "billing_address_country": "Deutschland",
      "locked_fields": [
      ],
      "_acl": {
        "fields": {}
      },
      "_module": "Accounts"
    },
    {
      "id": "0fc396b8-acd5-11e6-8a91-0681183120c1",
      "name": "Deltzmeier Software Systems",
      "date_modified": "2016-12-14T00:13:47+00:00",
      "billing_address_city": "M\u00fcnchen",
      "billing_address_country": "Deutschland",
      "locked_fields": [
      ],
      "_acl": {
        "fields": {}
      },
      "_module": "Accounts"
    }
  ]
}