As a system administrator, I want to fetch all the files from my organization uploaded by different users.
To achieve my goal I use the request to GET /services/data/v48.0/query/?q=SELECT+Id+FROM+ContentDocument+USING+SCOPE+Everything
.
But here is the following problem: when the user deletes his file, I want to upload that file back using a system admin token, but with the possibility to change its owner (back to that user who deletes that file).
Can anyone help me to solve such a problem?
It's bit counter-intuitive but to upload a File you need to upload ContentVersion
record. If the parent lookup (ContentDocumentId
) field will be blank - it'll silently create the parent ContentDocument
for you.
Examples of upload can be found here: https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/dome_sobject_insert_update_blob.htm
(I've already answered that for you, didn't I? :))
So the only difference would be that you want to set OwnerId
, possibly also CreatedById
as that user. You can set Owner always as long as the owner is an active user. "Created By", "Last Modified By" are audit fields, normally not editable. If the owner is inactive or you really want to set "Created By" - you can. But you have only 1 shot at this, audit fields can be set only on insert. After that every update is managed by SF. Check Help if you need more info on this.
Now... this isn't always needed. OK, you will recreate the file but all links to it (from other records, from chatter posts, libraries) will be lost. It's in ContentDOcumentLink
and if you delete the document - they cascade delete. You might not have info or permissions to recreate all the links.
Your first line of defense could be checking whether file can be restored from Recycle Bin. It can't be done in pure REST API but you could use SF SOAP API or write an Apex service that would try to do that for you. See https://salesforce.stackexchange.com/questions/117370/how-can-a-record-be-undeleted-using-the-rest-api for tips.