You need to sign in to do that
Don't have an account?

how do i write test class for this method...i tried creating test data setup but i m not getting how to create the recordid...can someone help ?
public static Map<ID, ContentVersion> getRelatedFilesByRecordId(String recordId) {
system.debug('recordId-->'+recordId);
// Get record file IDs
List<ContentDocumentLink> files = [SELECT ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId = :recordId];
List<ID> fileIDs = new List<ID>();
for (ContentDocumentLink docLink : files) {
fileIDs.add(docLink.ContentDocumentId);
}
system.debug('fileIDs-->'+fileIDs);
List<ContentVersion> docs = [SELECT ContentDocumentId, FileExtension, Title, CreatedDate,Document_Name__c,FileType,Comments__c
FROM ContentVersion WHERE ContentDocumentId IN : fileIDs Order by CreatedDate DESC ];
Map<ID, ContentVersion> mapIdTitle = new Map<ID, ContentVersion>();
for (ContentVersion docLink : docs) {
mapIdTitle.put(docLink.ContentDocumentId, docLink);
}
return mapIdTitle;
}
system.debug('recordId-->'+recordId);
// Get record file IDs
List<ContentDocumentLink> files = [SELECT ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId = :recordId];
List<ID> fileIDs = new List<ID>();
for (ContentDocumentLink docLink : files) {
fileIDs.add(docLink.ContentDocumentId);
}
system.debug('fileIDs-->'+fileIDs);
List<ContentVersion> docs = [SELECT ContentDocumentId, FileExtension, Title, CreatedDate,Document_Name__c,FileType,Comments__c
FROM ContentVersion WHERE ContentDocumentId IN : fileIDs Order by CreatedDate DESC ];
Map<ID, ContentVersion> mapIdTitle = new Map<ID, ContentVersion>();
for (ContentVersion docLink : docs) {
mapIdTitle.put(docLink.ContentDocumentId, docLink);
}
return mapIdTitle;
}
Hi Hitesh,
In this case the recordId will be the id of the object which you have created in setup method.
For example, if you have created an account record named accObj in setup then you can do the following in your test class to cover the code coverage of this class :
test.starttest();
YourClassName.getRelatedFilesByRecordId(accObj.id);
test.stop();
Please mark it as best answer if it resloves your issue.Thanks.
I wwrote this...but i am getting "cr variable doesnt exist"
It is the record of object where you want to insert the file. You can create a contact and assign its id in place of lettingobj.
Thanks.
Please try this code. Hope this will help you.
It is covering here.
Please give it a like if it helps you.
also this