function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
kris chitkris chit 

Email notification on ContentVersion object


I am sending an email notification if a field is changed on Content Version object through Apex as we cannot create Workflows/processbuilder on Contentversion object. In my Apex code, I am referring to a email template which I created for this purpose.  In the body of email I am trying to include few ContentVersion fields, but there is no option to select merge fields for Content Version object. 

Is there a way I can use ContentVersion fields in my email template ?
Deepali KulshresthaDeepali Kulshrestha
Hi Kris,

You can't just use the fields of content Version ,But you can query the contentversion and include into email as attachment,see the code below:

1.If you have the ContentDocument Id you can fetch the ContentVersion file as this:

// If you only want the ContentVersion ID's
Map<Id, ContentVersion> contentVersions = new Map<Id, ContentVersion> {};
Set<Id> versionsToAttach = new Set<Id> {};
    SELECT Id, ContentDocumentId 
    FROM ContentVersion 
    WHERE ContentDocument IN :contentDocumentIds 

return contentVersions.keySet();

or you can mix both code, I just picked part of mine as example but do everything in one SOQL query
public static List<Messaging.EmailFileAttachment> ContentDocumentAsAttachement(Id[] contentDocumentIds) {
    List<Messaging.EmailFileAttachment> attachments = new List<Messaging.EmailFileAttachment>{};
    List<ContentVersion> documents                  = new List<ContentVersion>{};

      SELECT Id, Title, FileType, VersionData, isLatest, ContentDocumentId
      FROM ContentVersion
      WHERE isLatest = true AND ContentDocumentId IN :contentDocumentIds

    for (ContentVersion document: documents) {
      Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment();



    return attachments;


You can attach those Messaging.EmailFileAttachment with public Void setFileAttachments(EmailFileAttachment[] fileNames) Or only the Ids with public void setEntityAttachments(List<String> ids).

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
Hi Deepali

How to write test class you have provided over here to cover the for loop in the code?
mukesh guptamukesh gupta
Hi Sree,

Please follow below sample code:-

public static List<Messaging.EmailFileAttachment> ContentDocumentAsAttachement(Id[] contentDocumentIds) { 
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); 

// Get ContentVersion
ContentVersion[] cv_list = [select VersionData, PathOnClient from ContentVersion where ContentDocumentId = :contentDocumentIds];
ContentVersion cv = new ContentVersion();

if(cv_list.size() != 0) {
	cv = cv_list[0];

	// Create the email attachment
	Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
	email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});

email.setSubject('Sending ContentVersion');
String[] toAddresses = new String[]{ '<your_email_address>' };
email.setToAddresses( toAddresses );
email.setPlainTextBody( 'Body of email');

// Sends the email  
Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});


private class ContentDocumentTest {
private static testMethod void testCreate() {
ContentVersion contentVersion_1 = new ContentVersion(
Title = 'Penguins',
PathOnClient = 'Penguins.jpg',
VersionData = Blob.valueOf('Test Content')
IsMajorVersion = true
insert contentVersion_1;
ContentVersion contentVersion_2 = [SELECT Id, Title, ContentDocumentId FROM ContentVersion WHERE Id = :contentVersion_1.Id LIMIT 1];
List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument];


System.assertEquals(documents.size(), 1);
System.assertEquals(documents[0].Id, contentVersion_2.ContentDocumentId);
System.assertEquals(documents[0].LatestPublishedVersionId, contentVersion_2.Id);
System.assertEquals(documents[0].Title, contentVersion_2.Title);

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.