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

Test Coverage on Custom Object


I have a custom object called Contract_Summary__c.  I have a trigger (below) which fires when a contract summary record is created, edited or delete, and updates a number of fields on the associated Opportunity based on the values in the custom object.  The trigger works as expected, but the test class I created (see below) only covers the Insert and Update functions of the trigger and not the Delete.  Lines 49-78 are not being covered.  Does anyone know why this happens or how to fix the test class to cover those lines?


trigger ContractUpdates on Contract_Summary__c (after insert, after update, after delete)

if(checkRecursiveBI.runOnceBI() || checkRecursiveBU.runOnceBU() || checkRecursiveBD.runOnceBD())
//Update Opportunity fields from values in Contract Summary object

    Map<Id,Opportunity> oppMap = new Map<Id,Opportunity>();    
    set<Id> Ids = new Set <Id>(); 

    if(trigger.isInsert || trigger.isUpdate)
    FOR(Contract_Summary__c con {
    system.debug('@@@@@Ids: '+Ids);
    Map<id,Opportunity> oppMap2 = new Map<id,Opportunity>([Select Id,Start_Date__c, End_Date__c from Opportunity Where Id in :Ids]); 
    system.debug('@@@@@oppMap2: '+oppMap2);
    system.debug('@@@@@Trigger.newMap.keySet(): ';
    List<Contract_Summary__c> cs1 = [SELECT Id, Related_Opportunity__c,Current_Effective_Date__c, Current_Expiration_Date__c, Current_Term_Months__c, Auto_Renew_Notice_Days__c, Auto_Renew_Provision__c, Term_for_Convenience__c, Portal_SLAs__c, Coaching_Performance_Guarantees__c, Off_Shore_Resource_Restiction__c, Delegated_Entity_Agreement__c, Special_Terms__c
                                     FROM Contract_Summary__c
                                     WHERE Id IN:]; 
        for(Contract_Summary__c con :cs1) 
            Opportunity o = oppMap2.get(con.Related_Opportunity__c);

            o.Start_Date__c = con.Current_Effective_Date__c;
            o.End_Date__c = con.Current_Expiration_Date__c;
            o.Current_Term_Months__c = con.Current_Term_Months__c;
            o.Auto_Renew__c = con.Auto_Renew_Provision__c;
            o.Auto_Renew_Notice_Days__c = con.Auto_Renew_Notice_Days__c;
            o.Term_for_Convenience__c = con.Term_for_Convenience__c;
            o.Portal_SLAs__c = con.Portal_SLAs__c;
            o.Coaching_Performance_Guarantees__c = con.Coaching_Performance_Guarantees__c;
            o.Off_Shore_Resource_Restiction__c = con.Off_Shore_Resource_Restiction__c;
            o.Delegated_Entity_Agreement__c = con.Delegated_Entity_Agreement__c;
            o.Special_Terms__c = con.Special_Terms__c;

            update oppMap.values();

    FOR(Contract_Summary__c con :trigger.old) {
    Map<id,Opportunity> oppMap2 = new Map<id,Opportunity>([Select Id,Start_Date__c, End_Date__c from Opportunity Where Id in :Ids]); 

    List<Contract_Summary__c> cs2 = [SELECT Id,Related_Opportunity__c
                                     FROM Contract_Summary__c
                                     WHERE Id IN: Trigger.oldMap.keySet()]; 
        for(Contract_Summary__c con :cs2) 
            Opportunity o = oppMap2.get(con.Related_Opportunity__c);

            o.Start_Date__c = null;
            o.End_Date__c = null;
            o.Current_Term_Months__c = null;
            o.Auto_Renew__c = FALSE;
            o.Auto_Renew_Notice_Days__c = null;
            o.Term_for_Convenience__c = FALSE;
            o.Portal_SLAs__c = FALSE;
            o.Coaching_Performance_Guarantees__c = FALSE;
            o.Off_Shore_Resource_Restiction__c = FALSE;
            o.Delegated_Entity_Agreement__c = FALSE;
            o.Special_Terms__c = FALSE;

            update oppMap.values();


Test Class:

private class ContractSummary_Tests {

    private static testmethod void testSummary1() {

    Account a = [Select id, name, Type, Consultant_Partner_Primary__c FROM Account WHERE Type='Consultant/Broker' limit 1];
    Account a1 = new Account(); ='test';
    insert a1;
    Opportunity opp = new Opportunity();
            opp.Name = 'Test Opportunity';
            opp.StageName = 'Stage 1 - Learn';
            opp.CloseDate = date.newinstance(2013,1,31);
            opp.Type = 'Renewal';
            opp.Consultant_Type__c = 'Primary Consultant';
        insert opp;
 update a1;

        Contract_Summary__c testContSumm = TestContractCreate.createContSumm(;
        system.debug('@@@@testContSumm : '+testContSumm);
        insert testContSumm;

        Contract_Summary__c testContSumm2 = [SELECT id,Amendment_Number__c
                                             FROM Contract_Summary__c
                                             WHERE id =: testContSumm.Id];
        testContSumm2.Amendment_Number__c = 'Amendment #2';

        Contract_Summary__c testContSumm3 = TestContractCreate.createContSumm2(;
        system.debug('@@@@testContSumm3 : '+testContSumm3);
        insert testContSumm3;
    update opp;
    update testContSumm2;
    delete testContSumm3;


Jim JamJim Jam
In your trigger, where you populate the cs2 list, change your SOQL to include the key word ALL ROWS at the end. This is the only way you'll be able to retieve the row ids of records that that have been deleted.

for reference ..
Thanks.  I added that but it didn't increase the coverage of the trigger by the test class.It's still not covering the delete portion.  Any other ideas?
Jim JamJim Jam
Presumably youve checked testContSumm3 is created correctly? I noticed you've used a different method .. creatContSumm2 ? ... also how does checkRecursiveBD.runOnceBD() get set? .. I'm afraid i can only suggest debugging to ensure the variables have the values you expect.
Andy BoettcherAndy Boettcher
You will want to put in some System.Assert(a ==b, 'message if fail') lines in here to ensure that what you are doing in your test class is executing and returning what you expect.  Your code looks OK at first blush, but the Asserts after creating records and especially your DML within the start/stop test blocks will go a long way to helping you figure out what's happening.