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
HoysalaHoysala 

Test class for this class please, anyone help

public class DS_ExpenseNoteTrigger_Handler {
    public static void checkExpenseNoteOverlapNew(list<DS_Expense_Note__c> listNewExpenseNotes){
        Set<String> setExistingExpensePeriodId = new set<String>();
        for(DS_Expense_Note__c thisExpense : [select Id,DS_Period_Information__c,CreatedbyId from DS_Expense_Note__c where CreatedbyId =:UserInfo.getUserId()]){
            setExistingExpensePeriodId.add(thisExpense.DS_Period_Information__c);
        }
        
        for(DS_Expense_Note__c thisExpenseNote : listNewExpenseNotes){
            if(setExistingExpensePeriodId.contains(thisExpenseNote.DS_Period_Information__c)){
                thisExpenseNote.addError(Label.DS_ExpenseNote_Validation);
            }
        }
    }
    
}
Dushyant SonwarDushyant Sonwar
If you asking for covering the addError method in your test class, then you can use Database.update or Database.insert method depending on your case when the trigger handler calls the method.

It will not give test failures in your test class and you can check for assertion using the SaveResult getErrors() methods

You can read more about here
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_database_saveresult.htm#apex_methods_system_database_saveresult


Hope this helps.
HoysalaHoysala
Hi @  Dushyant sonwar, i am not able to understand as i am new to this