Node.js SugarCRM: SugarCRM Submit a File Attachment

Back to Index

Uploads (submits) a file attachment to a Note record.
Attention: 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_Manipulate_File_Attachments/#Manipulating_File_Attachments


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");

    var fileStream = new chilkat.Stream();
    fileStream.SourceFile = "starfish.jpg";

    rest.AddHeader("Content-Type","multipart/form-data");

    var sbJson = new chilkat.StringBuilder();
    rest.PartSelector = "1";
    rest.AddHeader("Content-Type","image/jpg");
    rest.AddHeader("Content-Disposition","form-data; name=\"filename\"; filename=\"starfish.jpg\"");
    rest.SetMultipartBodyStream(fileStream);
    rest.PartSelector = "0";
    var responseBody = rest.FullRequestMultipart("POST","/rest/v10/Notes/07c96c68-d87f-11e7-b2fa-02e359029409/file/filename");
    if (rest.LastMethodSuccess !== true) {
        console.log(rest.LastErrorText);
        return;
    }

    sbJson.Append(responseBody);

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

    console.log("Example Completed.");

}

chilkatExample();

Sample JSON Response Body

{
  "filename": {
    "content-type": "image\/jpeg",
    "content-length": 2935,
    "name": "starfish.jpg",
    "uri": "rest\/v10\/Notes\/07c96c68-d87f-11e7-b2fa-02e359029409\/file\/filename",
    "doc_type": "Sugar"
  },
  "record": {
    "id": "07c96c68-d87f-11e7-b2fa-02e359029409",
    "name": "Test Note",
    "date_modified": "2017-12-04T23:28:44+00:00",
    "modified_user_id": "19b22ff0-cad8-11e7-9849-0a53b542acfd",
    "modified_by_name": "SpanSetUSTest",
    "modified_user_link": {
      "full_name": "SpanSetUSTest",
      "id": "19b22ff0-cad8-11e7-9849-0a53b542acfd",
      "_acl": {
        "fields": {
          "user_name": {
            "write": "no",
            "create": "no"
          },
          "user_hash": {
            "read": "no",
            "write": "no",
            "create": "no"
          },
          "system_generated_password": {
            "read": "no",
            "write": "no",
            "create": "no"
          },
          "pwd_last_changed": {
            "write": "no",
            "create": "no"
          },
          "authenticate_id": {
            "read": "no",
            "write": "no",
            "create": "no"
          },
          "sugar_login": {
            "read": "no",
            "write": "no",
            "create": "no"
          },
          "is_admin": {
            "write": "no",
            "create": "no"
          },
          "external_auth_only": {
            "read": "no",
            "write": "no",
            "create": "no"
          },
          "last_login": {
            "write": "no",
            "create": "no"
          },
          "title": {
            "write": "no",
            "create": "no"
          },
          "department": {
            "write": "no",
            "create": "no"
          },
          "status": {
            "write": "no",
            "create": "no"
          },
          "portal_only": {
            "read": "no",
            "write": "no",
            "create": "no"
          },
          "show_on_employees": {
            "read": "no",
            "write": "no",
            "create": "no"
          },
          "employee_status": {
            "write": "no",
            "create": "no"
          },
          "reports_to_id": {
            "write": "no",
            "create": "no"
          },
          "reports_to_name": {
            "write": "no",
            "create": "no"
          },
          "reports_to_link": {
            "write": "no",
            "create": "no"
          },
          "is_group": {
            "read": "no",
            "write": "no",
            "create": "no"
          }
        },
        "admin": "no",
        "developer": "no",
        "delete": "no",
        "import": "no",
        "export": "no",
        "massupdate": "no",
        "_hash": "257b4383573ff966869d52fecb12ff12"
      }
    },
    "created_by": "19b22ff0-cad8-11e7-9849-0a53b542acfd",
    "created_by_name": "Test Account SpanSet US",
    "created_by_link": {
      "full_name": "Test Account SpanSet US",
      "id": "19b22ff0-cad8-11e7-9849-0a53b542acfd",
      "_acl": {
        "fields": {
          "user_name": {
            "write": "no",
            "create": "no"
          },
          "user_hash": {
            "read": "no",
            "write": "no",
            "create": "no"
          },
          "system_generated_password": {
            "read": "no",
            "write": "no",
            "create": "no"
          },
          "pwd_last_changed": {
            "write": "no",
            "create": "no"
          },
          "authenticate_id": {
            "read": "no",
            "write": "no",
            "create": "no"
          },
          "sugar_login": {
            "read": "no",
            "write": "no",
            "create": "no"
          },
          "is_admin": {
            "write": "no",
            "create": "no"
          },
          "external_auth_only": {
            "read": "no",
            "write": "no",
            "create": "no"
          },
          "last_login": {
            "write": "no",
            "create": "no"
          },
          "title": {
            "write": "no",
            "create": "no"
          },
          "department": {
            "write": "no",
            "create": "no"
          },
          "status": {
            "write": "no",
            "create": "no"
          },
          "portal_only": {
            "read": "no",
            "write": "no",
            "create": "no"
          },
          "show_on_employees": {
            "read": "no",
            "write": "no",
            "create": "no"
          },
          "employee_status": {
            "write": "no",
            "create": "no"
          },
          "reports_to_id": {
            "write": "no",
            "create": "no"
          },
          "reports_to_name": {
            "write": "no",
            "create": "no"
          },
          "reports_to_link": {
            "write": "no",
            "create": "no"
          },
          "is_group": {
            "read": "no",
            "write": "no",
            "create": "no"
          }
        },
        "admin": "no",
        "developer": "no",
        "delete": "no",
        "import": "no",
        "export": "no",
        "massupdate": "no",
        "_hash": "257b4383573ff966869d52fecb12ff12"
      }
    },
    "description": "",
    "deleted": false,
    "file_mime_type": "image\/jpeg",
    "file_url": "",
    "filename": "starfish.jpg",
    "upload_id": "",
    "parent_type": "",
    "parent_id": "",
    "contact_id": "",
    "portal_flag": false,
    "embed_flag": false,
    "parent_name": "",
    "parent": [
    ],
    "contact_name": "",
    "contact": {
      "name": "",
      "id": "",
      "_acl": {
        "fields": [
        ],
        "admin": "no",
        "developer": "no",
        "_hash": "20f20362961956e200539170d3fdabb9"
      }
    },
    "contact_phone": "",
    "contact_email": "",
    "account_id": "",
    "opportunity_id": "",
    "acase_id": "",
    "lead_id": "",
    "product_id": "",
    "quote_id": "",
    "following": true,
    "my_favorite": false,
    "tag": [
    ],
    "locked_fields": [
    ],
    "assigned_user_id": "",
    "assigned_user_name": "",
    "assigned_user_link": {
      "full_name": "",
      "id": "",
      "_acl": {
        "fields": {
          "user_name": {
            "write": "no",
            "create": "no"
          },
          "user_hash": {
            "read": "no",
            "write": "no",
            "create": "no"
          },
          "system_generated_password": {
            "read": "no",
            "write": "no",
            "create": "no"
          },
          "pwd_last_changed": {
            "read": "no",
            "write": "no",
            "create": "no"
          },
          "authenticate_id": {
            "read": "no",
            "write": "no",
            "create": "no"
          },
          "sugar_login": {
            "read": "no",
            "write": "no",
            "create": "no"
          },
          "is_admin": {
            "write": "no",
            "create": "no"
          },
          "external_auth_only": {
            "read": "no",
            "write": "no",
            "create": "no"
          },
          "last_login": {
            "read": "no",
            "write": "no",
            "create": "no"
          },
          "title": {
            "write": "no",
            "create": "no"
          },
          "department": {
            "write": "no",
            "create": "no"
          },
          "status": {
            "write": "no",
            "create": "no"
          },
          "portal_only": {
            "read": "no",
            "write": "no",
            "create": "no"
          },
          "show_on_employees": {
            "read": "no",
            "write": "no",
            "create": "no"
          },
          "employee_status": {
            "write": "no",
            "create": "no"
          },
          "reports_to_id": {
            "write": "no",
            "create": "no"
          },
          "reports_to_name": {
            "write": "no",
            "create": "no"
          },
          "reports_to_link": {
            "write": "no",
            "create": "no"
          },
          "is_group": {
            "read": "no",
            "write": "no",
            "create": "no"
          }
        },
        "admin": "no",
        "developer": "no",
        "edit": "no",
        "delete": "no",
        "import": "no",
        "export": "no",
        "massupdate": "no",
        "create": "no",
        "_hash": "6609e3b6881aab5349af8271ff796164"
      }
    },
    "team_count": "",
    "team_count_link": {
      "team_count": "",
      "id": "1",
      "_acl": {
        "fields": {
          "id": {
            "write": "no",
            "create": "no"
          },
          "name": {
            "write": "no",
            "create": "no"
          },
          "date_entered": {
            "write": "no",
            "create": "no"
          },
          "date_modified": {
            "write": "no",
            "create": "no"
          },
          "modified_user_id": {
            "write": "no",
            "create": "no"
          },
          "modified_by_name": {
            "write": "no",
            "create": "no"
          },
          "created_by": {
            "write": "no",
            "create": "no"
          },
          "created_by_name": {
            "write": "no",
            "create": "no"
          },
          "description": {
            "write": "no",
            "create": "no"
          },
          "deleted": {
            "write": "no",
            "create": "no"
          },
          "created_by_link": {
            "write": "no",
            "create": "no"
          },
          "modified_user_link": {
            "write": "no",
            "create": "no"
          },
          "activities": {
            "write": "no",
            "create": "no"
          },
          "name_2": {
            "write": "no",
            "create": "no"
          },
          "associated_user_id": {
            "write": "no",
            "create": "no"
          },
          "private": {
            "write": "no",
            "create": "no"
          },
          "users": {
            "write": "no",
            "create": "no"
          },
          "teams_sets": {
            "write": "no",
            "create": "no"
          },
          "activities_teams": {
            "write": "no",
            "create": "no"
          },
          "following": {
            "write": "no",
            "create": "no"
          },
          "following_link": {
            "write": "no",
            "create": "no"
          },
          "my_favorite": {
            "write": "no",
            "create": "no"
          },
          "favorite_link": {
            "write": "no",
            "create": "no"
          },
          "locked_fields": {
            "write": "no",
            "create": "no"
          },
          "locked_fields_link": {
            "write": "no",
            "create": "no"
          }
        },
        "admin": "no",
        "developer": "no",
        "edit": "no",
        "delete": "no",
        "import": "no",
        "massupdate": "no",
        "create": "no",
        "_hash": "8b45033179dfb79bf13ef2e0f5948464"
      }
    },
    "team_name": [
      {
        "id": "1",
        "name": "Global",
        "name_2": "",
        "primary": true,
        "selected": false
      }
    ],
    "_acl": {
      "fields": {}
    },
    "_module": "Notes"
  }
}