Find your content:

Search form

You are here

Can you change which records chatter files are shared to through apex?

 
Share

I want to take a file that a user uploaded in an accounts chatter feed (meaning it shows up as a feed attachment underneath that account) and move it over to another account through apex. Here is my code and the error I'm getting:

ContentDocumentLink oldLink = [select id, linkedentityid, ContentDocumentID, ShareType from ContentDocumentLink where ContentDocumentId = '069E0000000Mp1n'];
ContentDocumentLink link = oldlink.clone(false,true);
newlink.linkedentityid = '001E000000L2DUB';
insert newlink;
delete oldlink;

Error on insert:

FIELD_INTEGRITY_EXCEPTION, You cannot create a link for this type of entity through the api: [LinkedEntityId]

So it appears that you can't create ContentDocumentLinks for records but is there any other way of doing this?


Attribution to: Greg Grinberg

Possible Suggestion/Solution #1

Its not really ContentDocumentLink you need to pull, its the FeedItem Object. Here are the steps

  1. Get the FeedItem, that has Type = 'ContentPost' and ParentId = AccountId.
  2. Create a new FeedItem object and copy the Body, RelatedRecordId, Title & Type fields from FeedItem record from Step 1.
  3. Set the new FeedItem from Step 2 ParentId as the AccountID you want to move attachment to.
  4. Insert FeedItem, (and delete prev. feed Item if you want to move)

Eg :

 Account a = [Select Id from Account where Name like 'Olive%' LIMIT 1];
FeedItem fi = new FeedItem();
fi = [Select Body,CommentCount,ContentData,ContentDescription,ContentFileName,ContentSize,ContentType,CreatedById,CreatedDate,Id,InsertedById,IsDeleted,LastModifiedDate,LikeCount,LinkUrl,ParentId,RelatedRecordId,SystemModstamp,Title,Type FROM FeedItem where ParentId = '001E000000IOGIl' and CreatedDate = TODAY];
FeedItem fi2 = new FeedItem();
fi2.body = fi.body;
fi2.RelatedRecordId = fi.RelatedRecordId;
fi2.Title = fi.Title;
fi2.Type = fi.Type;
fi2.ParentId = a.Id;
 insert fi2;
delete fi;

Attribution to: logontokartik
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/941

My Block Status

My Block Content