Uploading attachments using ForceTk

Please suggest me how to put attachments for a record under notes and attachments using forceTk, currently i’m using the following code and it creates attachments, but i’m not sure how to link these attachments to one of my existing records in salesforce. Do i have to update the parent.id or something of each attachment. I am using the standard code from forceTk github repo. and it’s given below.



    var client = new forcetk.Client();
    var attachmentId;


    function upload() {
        var file = $("#file")[0].files[0];
        client.createBlob('ContentVersion', {
            Origin: 'C', // 'H' for Chatter File, 'C' for Content Document
            PathOnClient: file.name
        }, file.name, 'VersionData', file, function(response){
            attachmentId = response.id;
            $("#message").html("Chatter File created: Take a look!");
        }, function(request, status, response){
            $("#message").html("Error: " + status);

        //link the attachment to a specific record



Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You will need ParentId to associate to ,

Here is sample code for same

function uploadattachment(files,parentId){
    var client = new forcetk.Client();
    // Get the token from Visualforce
    var file =files[0];
     client.createBlob('Attachment', {
        'ParentId': parentId,
        'Name': file.name,
        'ContentType': file.type
        }, file.name, 'Body', file, function(response){
      }, function(request, status, response){
          $("#message").html("Error: " + status);

You will call this function above with parent Id and file object

var file = $("#file")[0].files[0];
uploadattachment(file,'pass your parent Id here');

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x