Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@arun12209
Created March 17, 2019 07:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arun12209/4abdf6ec421c97bb69af955370df396f to your computer and use it in GitHub Desktop.
Save arun12209/4abdf6ec421c97bb69af955370df396f to your computer and use it in GitHub Desktop.
uploadImageCntrl
/* Name: uploadImageCntrl
Description: Upload files and show image in richtext field on object record page.
Created Date: 17/03/2019
LastModified Date: 17/03/2019
Created By: Arun Kumar
*/
public with sharing class uploadImageCntrl {
public transient Blob imageFile{get;set;}
public string parentRecId ='';
public void uploadImageCntrl(){
}
public PageReference uploadImg() {
parentRecId = ApexPages.currentPage().getParameters().get('id');
system.debug('record id: ' +parentRecId );
//ContentVersion
ContentVersion conVer = new ContentVersion();
conVer.ContentLocation = 'S'; // S specify this document is in SF, use E for external files
conVer.Title = 'Test File';
conVer.PathOnClient = 'Test File';
conVer.VersionData = imageFile; //
insert conVer;
ContentVersion cv = [select id,ContentDocumentId from ContentVersion where id =: conVer.id];
//Create ContentDocumentLink
ContentDocumentLink cDe = new ContentDocumentLink();
cDe.ContentDocumentId = cv.contentDocumentId;
cDe.LinkedEntityId = parentRecId; // you can use objectId,GroupId etc
cDe.ShareType = 'V'; // Inferred permission, checkout description of ContentDocumentLink object for more details
//Insert content document link
insert cDe ;
//ContentDistribution: Represents information about sharing a document externally
ContentDistribution cd = new ContentDistribution();
cd.Name = 'Test File';
cd.ContentVersionId = conVer.Id;
//insert
insert cd;
ContentDistribution distribution = [select Name,ContentDownloadUrl from ContentDistribution where id=: cd.id];
Account acc = [select id,ARI__Images__c from Account where id=: parentRecId]; //ARI__Images__c (ARI__ is Namspace. it will be different in your case)
if(acc.ARI__Images__c == null){
acc.ARI__Images__c ='';
}
acc.ARI__Images__c += '<img src="'+distribution.ContentDownloadUrl+'" width="478" height="247">'+'</img><br/>';
update acc;
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment