• Sakshi Pawar
  • NEWBIE
  • 0 Points
  • Member since 2023

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 3
    Replies
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;
            }
        }
    }
}
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;
            }
        }
    }
}

hi ,

 

im working on to write test class, in that im facing problem to how to wrire test code for the attachment class. please provide solution for that.

 

 

thakyou.