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
pmozz01pmozz01 

Could Use Some Help With Trigger Test Code

My trigger is working as desired however, I have issues with not enough test coverage.  I would appreciate some suggestions on how to test some of this code.  The lines NOT COVERED are those in Red and large font below.  I am really unsure how to procedd with testing these lines of code???

 

 

 

 

trigger triggerWorkorderOppUpdate on Work_Order__c (after insert, after update, after delete) {

//************************************************
// Build a LIST of Opportunity ID's that will
// need recalculating
//************************************************
set <id> oppIDs = new set <id>();
list <Opportunity> opportunity = [SELECT ID FROM Opportunity WHERE ID in:oppIDs];

if(Trigger.isInsert || Trigger.isUpdate){
for(Work_Order__c w : trigger.new){
if(w.Opportunity__c != null){
if(!oppIDs.contains(w.Opportunity__c))
oppIDs.add(w.Opportunity__c);

}

}
}
// INSERT/UPDATE Trigger

if(Trigger.isDelete || Trigger.isUpdate){
for(Work_Order__c w : trigger.old){
if(w.Opportunity__c != null){
if(!oppIDs.contains(w.Opportunity__c))
oppIDs.add(w.Opportunity__c);

}
// w = [select ID, Opportunity__c from Work_Order__c where Opportunity__c in :oppIds];
}
} // DELETE/UPDATE Trigger

if(oppIDs .size() > 0) {

Map<ID, Opportunity> opps = new Map<ID, Opportunity>([Select id, Estimated_Cost__c from Opportunity Where Id in :oppIds]);

Opportunity d = null;

for (AggregateResult dr : [SELECT Opportunity__c, SUM(Cost_WO__c) SubPO FROM Work_Order__c WHERE Opportunity__c in :oppIds GROUP BY Opportunity__c]) {


String dID = (String)dr.get('Opportunity__c');
//get record or create one

if(opps.get(dID) == null)
d = new Opportunity (ID=dID,
Estimated_Cost__c = 0);

else
if(opps.containskey(dID))
d = opps.get(dID);


// update the summary total fields on the opportunity
Decimal po = (Decimal)dr.Get('SubPO');

d.Estimated_Cost__c = po;

opps.put(dID, d);
}
//commit the changes to Salesforce

update opps.values();

}

}

 

 

Test Code

 

 

public with sharing class testTriggerWorkorderOppUpdate {



    static testMethod void myTest() {

       //Insert a test opportunity
		Account a = [select Id, account.OwnerId, account.Name from Account limit 1];
		system.debug('Account' + Account.ID);
		
       Opportunity o = new Opportunity();
       o.OwnerID = a.OwnerId;
       o.AccountId = a.id;
       o.StageName='Target';
       o.Name='TestOp';
       o.closeDate=Date.today();

       insert o;
       system.debug('opportunity id' + Opportunity.ID);
       
         //Insert a Work_Order

       Work_Order__c wo = new Work_Order__c();

       wo.Opportunity__c = o.id;
       
       wo.Close_Date_WO__c = Date.today();

       wo.Cost_WO__c = 55.00;

 
       insert wo;
       
       wo.Cost_WO__c = 75.00;
       update wo;
       
		system.debug('workorder' + wo.ID);
		
       Opportunity updated_Opportunity = [SELECT ID, Estimated_Cost__c FROM Opportunity WHERE Id = :o.Id];

       //Verify that the values of the opp were changed by the trigger
       
  System.assertEquals(wo.Cost_WO__c,[Select Estimated_Cost__c From Opportunity Where Id = :o.Id].Estimated_Cost__c);
       
 
     }
static testMethod void testBulkInsert() {

  List<Opportunity> opps = new List<Opportunity>();
  
 }
}

 

 

Best Answer chosen by Admin (Salesforce Developers) 
pmozz01pmozz01

Thanks! As soon as I removed that code, the test ran with 95% coverage.

All Answers

BritishBoyinDCBritishBoyinDC

One thought to begin with - unless I'm missing something, you don't need to check the OppId Set to see if an Id already exists - Sets enforce uniqueness automatically (i.e. you can't add the same value twice to a Set)  - so just add the Id to the set. 

 

 

Instead of

if(!oppIDs.contains(w.Opportunity__c)) 
                oppIDs.add(w.Opportunity__c);
            }

just use

oppIDs.add(w.Opportunity__c);

 

 

That might fix the other issue in the code coverage  I think....

Jeremy.NottinghJeremy.Nottingh

All of the places that the test skipped are because there's no ID in the Work_Order__c.Opportunity__c field. I can see that your testmethod inserts the work order with that field populated, but is it possible that another trigger or validation rule is stopping it?

 

I would put a

 

system.debug('\n\n' + w + '\n\n');

 

in the trigger right before the first if statement, just to see what you're working with. For some reason the Opportunity ID didn't make it to the trigger.

 

Oh, and the other poster is right about sets. No need to check if the ID is already in it; just add the ID and it will automatically dedupe.

 

Jeremy

pmozz01pmozz01

Thanks! As soon as I removed that code, the test ran with 95% coverage.

This was selected as the best answer