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
Mahesh Tandan 6Mahesh Tandan 6 

test a method

hi i'm here

please can somebody help me in test class

this is my extension
 
public class addContactExtension {

 public List<Id> getFileId() {
        List<Id> ListOfImagesIds= new List<Id>();//store files/images Ids
        for(ContentDocumentLink CDL : [Select ContentDocument.LatestPublishedVersionId from ContentDocumentLink where LinkedEntityId IN(select Id from Campaign where Id=:camId.id) order by ContentDocument.CreatedDate ASC]){
            ListOfImagesIds.add(CDL.ContentDocument.LatestPublishedVersionId);
            //System.debug('Total Id'+ ListOfImagesIds);
        }
        return ListOfImagesIds;
    }//dynamically Images Fetching from Files Ends here.

}



How can I test this method
Raghu NaniRaghu Nani
Hi Mahesh,

As per your, my under standing was Content document is shared with the Campaign) so please records in order in test class.

First - Create Campaign record
2nd  - Create ContentDocument
3rd - Create ContentDocumentLink 

Please create ablve records in stepwise and call this main class for test coverage. Foe more details refer below link (ContentDocument & ContentDocumentLink)
https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_contentdocumentlink.htm

Regards,
Raghu
skype :raghup101
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hi mahesh,
Try this code.Kindly modify according to requirements.
@istest
public class testadd {
    @istest
    public static void testmethod123(){
        campaign camp = new campaign(Name='unit test');
        insert camp;
        
        ContentVersion contentVersion = new ContentVersion(
            Title = 'unittest',
            PathOnClient = 'test.jpg',
            VersionData = Blob.valueOf('Test file'),
            IsMajorVersion = true
        );
        insert contentVersion;    
        List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument];
        
        //create ContentDocumentLink  record 
        ContentDocumentLink cdl = New ContentDocumentLink();
        cdl.LinkedEntityId = camp.id;
        cdl.ContentDocumentId = documents[0].Id;
        cdl.shareType = 'V';
        insert cdl;
        addContactExtension a = new addContactExtension();
        a.getFileId();
        
    }
    
}

Hope this helps you
Let me know if this helps you. Kindly mark it as solved so that it may help others in future.

Thanks and Regards