0

I have a trigger that is effectively checking a custom list to match the Close Date with the Start/End dates for weeks so a custom field is populated with the text of the Start and End dates.

Trigger:

trigger CloseWeekUpdate on Opportunity (before insert, before update) {
Try{
    for(Opportunity opp : trigger.new){
        if(opp.CloseDate != NULL){
            system.debug('opp.CloseDate = ' + opp.CloseDate);
            for(CloseDateWeek__c rec : CloseDateWeek__c.getAll().values()){
                if (opp.CloseDate >= rec.WeekStartDate__c && opp.CloseDate <= rec.WeekEndDate__c){
                    system.debug('rec.WeekStartDate__c = ' + rec.WeekStartDate__c);
                    system.debug('rec.WeeEndDate__c = ' + rec.WeekEndDate__c);
                    opp.Close_Week__c = rec.WeekStartDate__c.format() + ' - ' + rec.WeekEndDate__c.format();
                    system.debug('rec.Close_Week__c = ' + opp.Close_Week__c);
                    break;
                }
            }
        }
    }
}
Catch(Exception Ex){
    system.debug('Exception Details = ' + Ex);
}

I have built a test class to insert a test Opportunity and I am getting to 50% but unable to get higher, how do I get the class to test line 9, 12 and 14 since these are the three it is telling me is failing.

Lines that are not testing:

  • for(CloseDateWeek__c rec : CloseDateWeek__c.getAll().values()){
  • if (opp.CloseDate >= rec.WeekStartDate__c && opp.CloseDate <= rec.WeekEndDate__c){
  • opp.Close_Week__c = rec.WeekStartDate__c.format() + ' - ' + rec.WeekEndDate__c.format();

Here is the Test class I have:

    @isTest
public class TestCloseWeekUpdate {
    static testMethod void testCloseWeek(){
        Opportunity to = new Opportunity();
        to.Name = 'Test Name';
        to.CloseDate = Date.newInstance(2020,06,06);
        to.StageName = 'Closed Won';
        to.Start_Date__c = Date.newInstance(2020,06,01);
        to.Type = 'New Business';
        to.Market__c = 'Large';
        to.Premium__c = TRUE;
        to.Startup_Needed__c = 'Yes';
        insert to;
        test.startTest();
        try{
            to.Close_Week__c = '5/31/2020 - 6/6/2020';
        }
        Catch (Exception ee){}
        test.stopTest();
    }
}

Any help with getting that last 3 lines covered is greatly appreciated!

5
  • The very first thing you should do is remove those exception-swallowing catch blocks. They are going to make your debugging process much more difficult, yield false positives, and potentially harm user data.
    – David Reed
    Commented Jun 8, 2020 at 20:53
  • It would help if you could edit to denote which lines are which. By my count line 9 is a System.debug() statement.
    – David Reed
    Commented Jun 8, 2020 at 20:54
  • Thanks for the note about the catch block, I cannot edit but the lines in question are: if (opp.CloseDate >= rec.WeekStartDate__c && opp.CloseDate <= rec.WeekEndDate__c){ and opp.Close_Week__c = rec.WeekStartDate__c.format() + ' - ' + rec.WeekEndDate__c.format(); Commented Jun 8, 2020 at 21:21
  • @DavidReed I added the three lines in the main post that are the issue if you have any ideas on what to add to the test class to get coverage. Commented Jun 8, 2020 at 21:46
  • 3
    CloseDateWeek__c is presumably a custom setting - you need to mock these (DML insert) in the test class; custom settings are not considered as metadata and hence don't exist in a test context
    – cropredy
    Commented Jun 9, 2020 at 0:49

1 Answer 1

1

Found the issue to be I needed to also declare the custom list values using the following:

    CloseDateWeek__c cdw1 = new CloseDateWeek__c();
    cdw1.WeekStartDate__c = Date.newInstance(2020, 05, 31);
    cdw1.WeekEndDate__c = Date.newInstance(2020, 06, 06);
    cdw1.Name = 'Week Test';

Once I had the list values declared as well as the Opportunity values that gave me the full coverage.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .