0

I have written a LWC component for file upload using ContentDocumentLink and it is completed. Right now I am writing a test class for the Apex class.

Apex class:

public with sharing class Sitevisit{
    @AuraEnabled(cacheable=true)
    public static Site_Visit__c getSingleSitevisit(string recordId) {
        System.debug('SitevisitrecordID>>'+recordId);
        return [
            SELECT Id,Date_of_Visit__c,Flags__c,Details__c,Grant__c
            FROM Site_Visit__c
            WHERE Grant__c=:recordId
            ORDER BY LastModifiedDate DESC
            LIMIT 1
        ];
    }
    @AuraEnabled(cacheable=true)
    public static List<ContentDocumentLink> fetchFiles(String recordId,String file){
        return [SELECT LinkedEntityId, ContentDocument.CreatedDate, ContentDocument.Title, ContentDocument.ContentSize, ContentDocument.FileType 
                FROM ContentDocumentLink  
                WHERE LinkedEntityId  =:recordId];
    }
    
    @AuraEnabled
    public static void linkFileToRecord(String recordId,String contentVersionId){
        try {
            String contentDocId=[Select ContentDocumentId FROM ContentVersion WHERE Id=:contentVersionId].ContentDocumentId;
            insert new ContentDocumentLink(
                ContentDocumentId=contentDocId,
                LinkedEntityId=recordId
            );
        } catch (Exception e) {
            throw new AuraHandledException(e.getMessage());
        }
    }
}

Test class:

I have covered the first method for the data entry in sitevisit. Regarding the file upload I am bit confused:

@isTest
public class SiteVisitTest {
    static testMethod void testSite() {
        Site_Visit__c sv=new Site_Visit__c();
        sv.Name='a270R000007F1Ep';
        sv.Date_of_Visit__c=Date.Parse('03/03/2022');
        sv.Flags__c='Red';
        sv.Details__c='Testing';        
        Insert sv;
        ContentDocumentLink cdl=new ContentDocumentLink();
        cdl.LinkedEntityId='0060R00000IOEmKQAX';
        cdl.ContentDocument.CreatedDate='2022-03-23T13:06;20.000+0000';
        cdl.ContentDocument.Title='salesforce01';
        cdl.ContentDocument.ContentSize='12137';
        cdl.ContentDocument.FileType='jpg';
        Insert cdl;

I have written it this way. But there are some issues:

Field is not writeable: ContentDocument.FileType

Field is not writeable: ContentDocument.FileType

I understand that I cannot directly assign these values. How do I complete this?

0

1 Answer 1

0

Before you can create a ContentDocumentLink, you must first create a ContentVersion.

ContentVersion version = new ContentVersion(
  Title='File Name',
  PathOnClient='sample.jpg',
  VersionData=EncodingUtil.base64Decode('AA=='),
  Origin='H'
);
insert version;
version = [SELECT ContentDocumentId FROM ContentVersion WHERE Id = :version.Id];

Note that FileType is derived from the PathOnClient. You don't need to set this manually.

Also note that you can't just insert/update a parent object in this manner. You have to create each record in order. So, first create a ContentVersion, then create a ContentDocumentLink.

Not the answer you're looking for? Browse other questions tagged .