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
Sakshi PawarSakshi Pawar 

Hi I want test coverage for this class

public class ExpenditureTriggerHandler {
    
    // Trigger handler method to handle Expenditure records
    
    Public static void handler(List<Expenditure__c> newRecords, List<Expenditure__c> oldRecords, Map<Id, Expenditure__c> newMap, Map<Id, Expenditure__c> oldMap, TriggerOperation operationType)
    {
        SWITCH ON operationType
        {
            WHEN AFTER_UPDATE
            {
                // Call the handleAfterUpdate method
                
                handleAfterUpdate(newRecords, oldRecords, newMap, oldMap);
            }
        }
    }
    
    // Method to handle after update trigger events
    
    Private static void handleAfterUpdate(List<Expenditure__c> newRecords, List<Expenditure__c> oldRecords, Map<Id, Expenditure__c> newMap, Map<Id, Expenditure__c> oldMap)
    {
        updateExpenditureListItem(newRecords, oldRecords, oldMap);
        addAttachmentFile(newRecords, oldRecords, oldMap);
    }
    
    // Method to update the Expenditure Item List records
    
    Private static void updateExpenditureListItem(List<Expenditure__c> newRecords, List<Expenditure__c> oldRecords, Map<Id, Expenditure__c> oldMap)
    {
        // list to hold the Expenditure Item List records that need to be updated
        
        List<Expenditure_Item_List__c> expenditureLineItemsToUpdate  =  new List<Expenditure_Item_List__c>();
        Set<Id> expenditureIds = new Set<Id>();
        
        for(Expenditure__c expenditure: newRecords) 
        {
            if(expenditure.Status__c != oldMap.get(expenditure.Id).Status__c && expenditure.Status__c == 'In Progress')
            {
                expenditureIds.add(expenditure.Id);
            }
            
        }
        
        for(Expenditure__c expenditure: [SELECT Id, (SELECT Id, Serial_Number__c FROM Expenditure_Item_Lists__r Order By CreatedDate ASC) From Expenditure__c Where ID IN: expenditureIds])
        {
            // Check if there are any related Expenditure Item List records
            
            if(expenditure.Expenditure_Item_Lists__r != Null) 
            {
                Integer count = 1;
                for(Expenditure_Item_List__c expenditureItem: expenditure.Expenditure_Item_Lists__r) 
                {
                    // Setting the Serial Number field of the Expenditure Item List record to the counter value
                    
                    expenditureItem.Serial_Number__c = count;
                    expenditureLineItemsToUpdate.add(expenditureItem);
                    count++;
                }
            }
            
        }
        
        // Update the Expenditure Item List records 
        
        if(!expenditureLineItemsToUpdate.isEmpty()) 
        {
            //added to bypass validation for serial number
            ExpenditureItemListTriggerHandler.byPassValidation = true;
            Update expenditureLineItemsToUpdate;
            //added to remove bypass validation
            ExpenditureItemListTriggerHandler.byPassValidation = false;
        }
    } 
    
    
    // Method to add attachment to Expenditure when Payment is done
    @TestVisible
    Private static void addAttachmentFile(List<Expenditure__c> newRecords, List<Expenditure__c> oldRecords, Map<Id, Expenditure__c> oldMap)
    {
        // Set to hold the Expenditure records Id that need to be updated
        Set<Id> expenditureIds = new Set<Id>();
        
        for(Expenditure__c expenditure: newRecords) 
        {
            if(expenditure.Payment_Done__c != oldMap.get(expenditure.Id).Payment_Done__c && expenditure.Payment_Done__c 
               && expenditure.Status__c == 'Approved')
            {
                expenditureIds.add(expenditure.Id);
            }
            
        }
        
        // call future method to insert pdf file
        
        if(!expenditureIds.isEmpty()) 
        {
            attachFile(expenditureIds);
        }
        
    }
     @TestVisible
    @future(callout=true)
    private static void attachFile(Set<Id> expenditureListId)
    {
        //List to hold ContentDocumentLinks
        List<ContentDocumentLink> ContentDocumentLinkList  =  new List<ContentDocumentLink>();
        //Map to hold Expenditure Id and ContentVersion mapping
        Map<String,ContentVersion> expendContentVersionIdMap  =  new Map<String,ContentVersion>();
        
        //Query expenditure and map pdf according to recordType
        for(Expenditure__c expenditure: [Select Id, Recordtype.DeveloperName From Expenditure__c Where Id IN: expenditureListId]) 
        {
            PageReference paymentVoucherPDF =  Page.paymentVoucher;
            paymentVoucherPDF.getParameters().put('id',expenditure.Id);
            paymentVoucherPDF.setRedirect(true);
            Blob paymentVoucherBlob ;
            paymentVoucherBlob = paymentVoucherPDF.getContent();
            ContentVersion ContVerFile1 = new ContentVersion();
            ContVerFile1.VersionData = paymentVoucherBlob;
            ContVerFile1.Title = 'paymentVoucher'; 
            ContVerFile1.ContentLocation= 's';
            ContVerFile1.PathOnClient='paymentVoucher.pdf';
            expendContentVersionIdMap.put(expenditure.Id+'-'+ContVerFile1.Title, ContVerFile1);
            if(expenditure.Recordtype.DeveloperName == 'LTA_Reimbursement') {
                PageReference leaveTravelAllowancePDF =  Page.leaveTravelAllowance;
                leaveTravelAllowancePDF.getParameters().put('id',expenditure.Id);
                leaveTravelAllowancePDF.setRedirect(true);
                Blob leaveTravelAllowanceBlob ;
                leaveTravelAllowanceBlob = leaveTravelAllowancePDF.getContent();
                ContentVersion ContVerFile2 = new ContentVersion();
                ContVerFile2.VersionData = leaveTravelAllowanceBlob;
                ContVerFile2.Title = 'LeaveTravelAllowance'; 
                ContVerFile2.ContentLocation= 's';
                ContVerFile2.PathOnClient='LeaveTravelAllowance.pdf';
                expendContentVersionIdMap.put(expenditure.Id+'-'+ContVerFile2.Title, ContVerFile2);
            } else if(expenditure.Recordtype.DeveloperName == 'Travelling_Bill') {
                PageReference travelPaymentVoucherPDF =  Page.travelPaymentVoucher;
                travelPaymentVoucherPDF.getParameters().put('id',expenditure.Id);
                travelPaymentVoucherPDF.setRedirect(true);
                Blob travelPaymentVoucherBlob ;
                travelPaymentVoucherBlob = travelPaymentVoucherPDF.getContent();
                ContentVersion ContVerFile3 = new ContentVersion();
                ContVerFile3.VersionData = travelPaymentVoucherBlob;
                ContVerFile3.Title = 'TravelPaymentVoucher'; 
                ContVerFile3.ContentLocation= 's';
                ContVerFile3.PathOnClient='TravelPaymentVoucher.pdf';
                expendContentVersionIdMap.put(expenditure.Id+'-'+ContVerFile3.Title, ContVerFile3);
            } /*else if(expenditure.Recordtype.DeveloperName == 'Travelling_Bill') {
                PageReference travelPaymentVoucherPDF =  Page.travelPaymentVoucher;
                travelPaymentVoucherPDF.getParameters().put('id',expenditure.Id);
                travelPaymentVoucherPDF.setRedirect(true);
                Blob travelPaymentVoucherBlob ;
                travelPaymentVoucherBlob = travelPaymentVoucherPDF.getContent();
                ContentVersion ContVerFile3 = new ContentVersion();
                ContVerFile3.VersionData = travelPaymentVoucherBlob;
                ContVerFile3.Title = 'TravelPaymentVoucher'; 
                ContVerFile3.ContentLocation= 's';
                ContVerFile3.PathOnClient='TravelPaymentVoucher.pdf';
                expendContentVersionIdMap.put(expenditure.Id+'-'+ContVerFile3.Title, ContVerFile3);
            }    */              
        }
        
        // create contentDocumentLink 
        if(!expendContentVersionIdMap.isEmpty()) 
        {
            Insert expendContentVersionIdMap.values();
            
            List<ContentVersion> contVerList = [SELECT Id, ContentDocumentId FROM ContentVersion WHERE Id =:expendContentVersionIdMap.values()];
            
            for(String expenditureId: expendContentVersionIdMap.KeySet()) 
            {
                for(ContentVersion contVer : contVerList)
                {
                    if(contVer.Id == expendContentVersionIdMap.get(expenditureId).Id)
                    {
                        ContentDocumentLink cDe = new ContentDocumentLink();
                        cDe.ContentDocumentId = contVer.ContentDocumentId;
                        cDe.LinkedEntityId = expenditureId.split('-')[0];
                        cDe.ShareType = 'I';
                        cDe.Visibility = 'AllUsers';
                        ContentDocumentLinkList.add(cDe);
                        break;
                    }
                }
            }
            
            // Insert the ContentDocumentLink records
            
            if(!ContentDocumentLinkList.isEmpty()) 
            {
                Insert ContentDocumentLinkList;
            }
        }
    }
}
SubratSubrat (Salesforce Developers) 
Hello Sakshi ,

Can you try with below test class :
@isTest
private class ExpenditureTriggerHandlerTest {
    
    @isTest
    static void testHandler() {
        // Test data setup
        Expenditure_c expenditure = new Expenditure_c();
        expenditure.Status__c = 'In Progress';
        insert expenditure;
        
        Expenditure_Item_List_c expenditureItem = new Expenditure_Item_List_c();
        expenditureItem.Expenditure__c = expenditure.Id;
        insert expenditureItem;
        
        // Test the handleAfterUpdate method
        Test.startTest();
        ExpenditureTriggerHandler.handleAfterUpdate(new List<Expenditure_c>{expenditure}, new List<Expenditurec>{}, new Map<Id, Expenditurec>{expenditure.Id => expenditure}, new Map<Id, Expenditure_c>{}, TriggerOperation.AFTER_UPDATE);
        Test.stopTest();
        
        // Assert that the Expenditure_Item_List__c record has been updated
        Expenditure_Item_List_c updatedExpenditureItem = [SELECT Serial_Numberc FROM Expenditure_Item_List_c WHERE Id = :expenditureItem.Id];
        System.assertEquals(1, updatedExpenditureItem.Serial_Number__c);
        
        // Test the addAttachmentFile method
        expenditure.Payment_Done__c = true;
        expenditure.Status__c = 'Approved';
        update expenditure;
        
        // Test the attachFile future method
        Test.startTest();
        ExpenditureTriggerHandler.attachFile(new Set<Id>{expenditure.Id});
        Test.stopTest();
        
        // Assert that a ContentDocumentLink record has been created for the Expenditure record
        List<ContentDocumentLink> contentDocumentLinks = [SELECT Id FROM ContentDocumentLink WHERE LinkedEntityId = :expenditure.Id];
        System.assertNotEquals(0, contentDocumentLinks.size());
    }
}

If it helps , please mark this as Best Answer.
Thank you.
Sakshi PawarSakshi Pawar
Thank you for the solution but not able to cover attachFile method

     @TestVisible
    @future(callout=true)
    private static void attachFile(Set<Id> expenditureListId)
    {
        //List to hold ContentDocumentLinks
        List<ContentDocumentLink> ContentDocumentLinkList  =  new List<ContentDocumentLink>();
        //Map to hold Expenditure Id and ContentVersion mapping
        Map<String,ContentVersion> expendContentVersionIdMap  =  new Map<String,ContentVersion>();
        
        //Query expenditure and map pdf according to recordType
        for(Expenditure__c expenditure: [Select Id, Recordtype.DeveloperName From Expenditure__c Where Id IN: expenditureListId]) 
        {
            PageReference paymentVoucherPDF =  Page.paymentVoucher;
            paymentVoucherPDF.getParameters().put('id',expenditure.Id);
            paymentVoucherPDF.setRedirect(true);
            Blob paymentVoucherBlob ;
            paymentVoucherBlob = paymentVoucherPDF.getContent();
            ContentVersion ContVerFile1 = new ContentVersion();
            ContVerFile1.VersionData = paymentVoucherBlob;
            ContVerFile1.Title = 'paymentVoucher'; 
            ContVerFile1.ContentLocation= 's';
            ContVerFile1.PathOnClient='paymentVoucher.pdf';
            expendContentVersionIdMap.put(expenditure.Id+'-'+ContVerFile1.Title, ContVerFile1);
            if(expenditure.Recordtype.DeveloperName == 'LTA_Reimbursement') {
                PageReference leaveTravelAllowancePDF =  Page.leaveTravelAllowance;
                leaveTravelAllowancePDF.getParameters().put('id',expenditure.Id);
                leaveTravelAllowancePDF.setRedirect(true);
                Blob leaveTravelAllowanceBlob ;
                leaveTravelAllowanceBlob = leaveTravelAllowancePDF.getContent();
                ContentVersion ContVerFile2 = new ContentVersion();
                ContVerFile2.VersionData = leaveTravelAllowanceBlob;
                ContVerFile2.Title = 'LeaveTravelAllowance'; 
                ContVerFile2.ContentLocation= 's';
                ContVerFile2.PathOnClient='LeaveTravelAllowance.pdf';
                expendContentVersionIdMap.put(expenditure.Id+'-'+ContVerFile2.Title, ContVerFile2);
            } else if(expenditure.Recordtype.DeveloperName == 'Travelling_Bill') {
                PageReference travelPaymentVoucherPDF =  Page.travelPaymentVoucher;
                travelPaymentVoucherPDF.getParameters().put('id',expenditure.Id);
                travelPaymentVoucherPDF.setRedirect(true);
                Blob travelPaymentVoucherBlob ;
                travelPaymentVoucherBlob = travelPaymentVoucherPDF.getContent();
                ContentVersion ContVerFile3 = new ContentVersion();
                ContVerFile3.VersionData = travelPaymentVoucherBlob;
                ContVerFile3.Title = 'TravelPaymentVoucher'; 
                ContVerFile3.ContentLocation= 's';
                ContVerFile3.PathOnClient='TravelPaymentVoucher.pdf';
                expendContentVersionIdMap.put(expenditure.Id+'-'+ContVerFile3.Title, ContVerFile3);
            } /*else if(expenditure.Recordtype.DeveloperName == 'Travelling_Bill') {
                PageReference travelPaymentVoucherPDF =  Page.travelPaymentVoucher;
                travelPaymentVoucherPDF.getParameters().put('id',expenditure.Id);
                travelPaymentVoucherPDF.setRedirect(true);
                Blob travelPaymentVoucherBlob ;
                travelPaymentVoucherBlob = travelPaymentVoucherPDF.getContent();
                ContentVersion ContVerFile3 = new ContentVersion();
                ContVerFile3.VersionData = travelPaymentVoucherBlob;
                ContVerFile3.Title = 'TravelPaymentVoucher'; 
                ContVerFile3.ContentLocation= 's';
                ContVerFile3.PathOnClient='TravelPaymentVoucher.pdf';
                expendContentVersionIdMap.put(expenditure.Id+'-'+ContVerFile3.Title, ContVerFile3);
            }    */              
        }
        
        // create contentDocumentLink 
        if(!expendContentVersionIdMap.isEmpty()) 
        {
            Insert expendContentVersionIdMap.values();
            
            List<ContentVersion> contVerList = [SELECT Id, ContentDocumentId FROM ContentVersion WHERE Id =:expendContentVersionIdMap.values()];
            
            for(String expenditureId: expendContentVersionIdMap.KeySet()) 
            {
                for(ContentVersion contVer : contVerList)
                {
                    if(contVer.Id == expendContentVersionIdMap.get(expenditureId).Id)
                    {
                        ContentDocumentLink cDe = new ContentDocumentLink();
                        cDe.ContentDocumentId = contVer.ContentDocumentId;
                        cDe.LinkedEntityId = expenditureId.split('-')[0];
                        cDe.ShareType = 'I';
                        cDe.Visibility = 'AllUsers';
                        ContentDocumentLinkList.add(cDe);
                        break;
                    }
                }
            }
            
            // Insert the ContentDocumentLink records
            
            if(!ContentDocumentLinkList.isEmpty()) 
            {
                Insert ContentDocumentLinkList;
            }
        }
    }
SubratSubrat (Salesforce Developers) 
Hello Sakshi ,

Created a test class for attachfile method :
 
@IsTest
private class TestAttachFile {
    
    @IsTest
    static void testAttachFile() {
        
        // Create a test Expenditure record
        Expenditure_c expenditure = new Expenditure_c();
        expenditure.Name = 'Test Expenditure';
        expenditure.RecordtypeId = Schema.SObjectType.Expenditure__c.getRecordTypeInfosByDeveloperName().get('LTA_Reimbursement').getRecordTypeId();
        insert expenditure;
        
        // Create a set of Expenditure record Ids to be passed as input
        Set<Id> expenditureIds = new Set<Id>();
        expenditureIds.add(expenditure.Id);
        
        // Invoke the attachFile method
        Test.startTest();
        MyClassName.attachFile(expenditureIds);
        Test.stopTest();
        
        // Assert that the ContentDocumentLink record has been created
        List<ContentDocumentLink> contentDocumentLinks = [            SELECT Id, ContentDocumentId, LinkedEntityId            FROM ContentDocumentLink            WHERE LinkedEntityId = :expenditure.Id        ];
        System.assertEquals(1, contentDocumentLinks.size());
    }
}

Hope this helps !
Thank you.
Sakshi PawarSakshi Pawar
Got thi error  
do not support getContent call