I am new to saleforce and I used the following code to save files to the Attachment object in salesfore:
String base64Data = EncodingUtil.urlDecode(base64Data, 'UTF-8');
Attachment attach = new Attachment();
attach.ParentId = parentId;
attach.Body = EncodingUtil.base64Decode(base64Data);
attach.Name = fileName;
attach.ContentType = contentType;
attach.IsPrivate = false;
insert attach;
Now, I need to generate an url by which the files can be shared outside of the salesforce. After some googling, I found out that it can be done using ContentDistribution
. And I want to generate the url in the apex class itself. I have refereed to many tutorials and no one them worked for me. So, please anyone can suggest a way to do it or point to a proper tutorial link which explains the process properly that how the shareable url is generated? I used this answer and I couldn't figure out what I should add to the ContentVersionId
field. When I added attachment Id to that field, I got this error:
System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, ContentVersion ID: id value of incorrect type
So, How can I share attachments using an url?