2

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

    }
  

1 Answer 1

3

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');
3
  • Mohit Shrivastava would this work for a community user with a customer community license? Jul 6, 2017 at 16:20
  • @ShaikhMohdNadeem Yes that should work .Instead of Attachment object , I would recommend to use new ContentVersion Object for all files storage . Jul 6, 2017 at 18:07
  • Thanks for replying @Mohith Shrivastava! I am using content version but I receive a 403 Forbidden Error every time I try to create a content version. I know this one deserves to be an own question in it self. Jul 6, 2017 at 18:23

You must log in to answer this question.