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
Jeff BomarJeff Bomar 

Code coverage on a Apex trigger

Any Help would be much apricated I created an Apex Trigger and am having trouble writing the test class for code coverage really green when it comes to this 

Triger 
trigger updateCustomDev on Custom_Programming__c (before Insert) {
  for (Custom_Programming__c Custom : trigger.new) {

    //SQL statement to lookup price 
    List<PricebookEntry> sqlResult = [SELECT UnitPrice 
        FROM PricebookEntry 
        WHERE Product2.Name = 'Custom Development'
                               AND PriceBook2.Name='DAKCS'];

    //save the returned SQL result inside the field 
    Custom.Hourly_Cost__c = sqlResult[0].UnitPrice;
  }    
}

Test Class
@IsTest
public class TestupdateCustomDev {
    @IsTest static void testHourlyCost(){
        //Create Custom
        Custom_Programming__c c = new Custom_Programming__c(Name='Test',Custom_State__c='Active',Custom_Type__c='Custom',Status__c='New');
        
        Test.startTest();
        Database.SaveResult result = Database.insert (c, true);
         System.assert(result.isSuccess());
        Test.stopTest();
    }
}

I was hoping this would be really simple all I want the trigger to do is update a field with an entry from pice book entry.

Thanks again
Best Answer chosen by Jeff Bomar
Daniel AhlDaniel Ahl
Hello Jeff,
It's not very beautiful but it should cover 100%:
 
@isTest(SeeAllData=True)
public class TestupdateCustomDev {
	@isTest
    private static void testHourlyCost(){
        Pricebook2 SPB = [SELECT Id FROM Pricebook2 WHERE IsStandard = true];
        Pricebook2 PB = new Pricebook2(Name='DAKCS');
        insert PB;
        Product2 prod = new Product2(Name = 'Custom Development');
        insert prod;
        PricebookEntry PBE = new PricebookEntry(UnitPrice = 22.00, Product2Id = prod.Id, PriceBook2Id = SPB.Id);
        insert PBE;
        PBE = new PricebookEntry(UnitPrice = 22.00, Product2Id = prod.Id, PriceBook2Id = PB.Id);
        insert PBE;
        
        Custom_Programming__c CP = new Custom_Programming__c(Name='Test',Custom_State__c='Active',Custom_Type__c='Custom',Status__c='New');
        insert CP;
    }
}

Let me know if you get any errors and I'll try to help you sort it out :)

All Answers

Daniel AhlDaniel Ahl
Hello Jeff,
It's not very beautiful but it should cover 100%:
 
@isTest(SeeAllData=True)
public class TestupdateCustomDev {
	@isTest
    private static void testHourlyCost(){
        Pricebook2 SPB = [SELECT Id FROM Pricebook2 WHERE IsStandard = true];
        Pricebook2 PB = new Pricebook2(Name='DAKCS');
        insert PB;
        Product2 prod = new Product2(Name = 'Custom Development');
        insert prod;
        PricebookEntry PBE = new PricebookEntry(UnitPrice = 22.00, Product2Id = prod.Id, PriceBook2Id = SPB.Id);
        insert PBE;
        PBE = new PricebookEntry(UnitPrice = 22.00, Product2Id = prod.Id, PriceBook2Id = PB.Id);
        insert PBE;
        
        Custom_Programming__c CP = new Custom_Programming__c(Name='Test',Custom_State__c='Active',Custom_Type__c='Custom',Status__c='New');
        insert CP;
    }
}

Let me know if you get any errors and I'll try to help you sort it out :)

This was selected as the best answer
Jeff BomarJeff Bomar
Worked perfectly. Thanks again.