[SalesForce] 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.

Regards,

Sangram


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

    client.setSessionToken('{!$Api.Session_ID}');

    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){
            console.log(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

    }
  

Best Answer

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
    client.setSessionToken('{!$Api.Session_ID}');
    var file =files[0];
     $('#loadingmessage').show();
     client.createBlob('Attachment', {
        'ParentId': parentId,
        'Name': file.name,
        'ContentType': file.type
        }, file.name, 'Body', file, function(response){
        console.log(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');
Related Topic