0
votes

I have this very simple before insert / update trigger on Opportunity that auto-selects the Price Book based on a dropdown value containing Sales Office (State) location info.

Here's my Trigger:

trigger SelectPriceBook on Opportunity ( before insert, before update ) {

    for( Opportunity opp : Trigger.new ) {
        // Change Price Book
        // New York
        if( opp.Campus__c == 'NYC' )
          opp.Pricebook2Id = PB_NYC; // contains a Pricebook's ID

        // Atlanta
        if( opp.Campus__c == 'ATL' )
          opp.Pricebook2Id = PB_ATL; // contains another Pricebook's ID
    }
}

Here's my Test Class:

@isTest (SeeAllData = true)
public class SelectPriceBookTestClass {

    static testMethod void validateSelectPriceBook() {

        // Pricebook IDs
        ID PB_NYC =  'xxxx';
        ID PB_ATL =  'xxxx';

        // New Opp
        Opportunity opp = new Opportunity();
        opp.Name = 'Test Opp';
        opp.Office__c = 'NYC';
        opp.StageName = 'Quote';       

        // Insert
        insert opp;

        // Retrive inserted opportunity
        opp = [SELECT Pricebook2id FROM Opportunity WHERE Id =:opp.Id];
        System.debug( 'Retrieved Pricebook Id: ' + opp.Pricebook2Id );

        // Change Campus
        opp.Office__c = 'ATL';
        // Update Opportunity
        update opp;

        // Retrive updated opportunity
        opp = [SELECT Pricebook2id FROM Opportunity WHERE Id =:opp.Id];
        System.debug( 'Retrieved Updated Pricebook Id: ' + opp.Pricebook2Id );        

        // Test
        System.assertEquals( PB_ATL, opp.Pricebook2Id );

    }
}

The test runs report 0% test coverage.

Also, on similar lines I have another before insert trigger that sets the Owner of an Event same as the Owner of the parent Lead. Here's the code:

trigger AutoCampusTourOwner on Event( before insert ) {

    for( Event evt : Trigger.new ) {
        // Abort if other kind of Event
        if( evt.Subject != 'Visit' )
            return;        

        // Set Owner Id
        Lead parentLead = [SELECT OwnerId FROM Lead WHERE Id = :evt.WhoId];
        evt.OwnerId = parentLead.OwnerId;

    }
}

This, too, is causing 0% coverage - my guess is that it's got something to do with the for loops in both. I know I'm seriously flouting DML rules by invoking SOQL query inside a for loop, but for my purposes it should be fine as these Events are created manually and only one at a time - so there are no scopes of governor limits kicking in due to bulk inserts.

The code in both cases work 100%. Please suggest a fix for the test cases.

3
In the first case, your trigger is looking at campus__c but your test is setting office__c - superfell
@superfell Sorry - that's a typo here. But the code still fails. Any suggestions? - miCRoSCoPiC_eaRthLinG

3 Answers

0
votes

Have you tried trigger.old ?? My thinking is, when you update the office in your test class from NYC to ATL, the value 'NYC' will be in trigger.old, and that's what you want to check in your trigger. I could be wrong since i'm new to apex too, but try it and let me know what happens.

0
votes

For the first trigger don't do anything rather just create opportunity and execute like this.

Test class for SelectPriceBook

@isTest
    private class TriggerTestClass {

        static testmethod void selectPriceTest(){
            Opportunity opps = new Opportunity(
                Name= 'Test Opps',
                CloseDate = System.today().addDays(30),
                StageName = 'Prospecting', 
                ForecastCategoryName = 'Pipeline',
                Office__c = 'NYC');
                insert opps;

                Opportunity opps2 = new Opportunity(
                Name= 'Test Opps 2',
                CloseDate = System.today().addDays(28),
                StageName = 'Prospecting', 
                ForecastCategoryName = 'Pipeline',
                Office__c = 'ATL');
                insert opps2;
        }

    }

It will give you good test coverage and I don't know what are you trying to do in AutoCampusTourOwner!

0
votes

i have test class for these

trigger ClientEmailTrigger on inflooens__Client_Email__c (after insert, after update, before insert, before update) {

ApexTriggerSettings__c setting = ApexTriggerSettings__c.getValues('Inflooens Trigger Settings');

if(setting != NULL && setting.ClientEmailTrigger__c == TRUE){

    AuditTrailController objAdt = new AuditTrailController();
    
    if(Trigger.isAfter){
        if(Trigger.isUpdate){
            System.debug('In Update Client Email Record');
            objAdt.insertAuditRecord(Trigger.newMap, 'inflooens__Client_Email__c', Trigger.new.get(0).Id, Trigger.oldMap);
        }
    
        if(Trigger.isInsert){
            objAdt.insertAuditRecord(Trigger.newMap, 'inflooens__Client_Email__c', null , null);
        }
    }
}

}