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
mike83mike83 

Apex Trigger Testing Help

Ok So I have this trigger on the Opportunity Object

 

trigger OppertunityUpdate on Opportunity (after insert, after update) {

public void UpdateOpportunities(ID opId) {

Opportunity o = [Select o.Id, o.Total_Freight__c, o.Total_Sales_Tax__c, o.Total_Installation__c,

o.Amount, o.Adjustment5__c, o.Adjustment4__c, o.Adjustment3__c,

o.Adjustment2__c, o.Adjustment1__c, o.CanRunTrigger__c

From Opportunity o where o.Id = :opId];

List<PricebookEntry> pbes = [Select p.Product2Id, p.Name, p.Id

From PricebookEntry p

Where p.Product2.name

in ('SalesTax','Freight','Discount1','Discount2','Discount3','Discount4','Discount5','Installation')

order by p.Product2.name ]; List<OpportunityLineItem> oli = [Select o.Id, o.OpportunityId From OpportunityLineItem o

where o.PricebookEntry.id in :pbes

and o.OpportunityId = :o.Id order by o.PricebookEntry.Product2.name];

System.Debug('OliCount: ' + oli.size());

Decimal oliCount = [Select count()

From OpportunityLineItem o

where o.PricebookEntry.id not in :pbes

and o.OpportunityId = :o.Id];

System.Debug('TotalOliSize: ' + oliCount +', o:' + o.id);

if(oliCount < 1) return; if(oli.size() <> 8)

{

System.Debug('Creating new olis');

//Delete oli if size <8 and > 0

if(oli.size() > 0)

delete oli;

oli = new List<OpportunityLineItem>();

//Adding discount1

oli.add( new OpportunityLineItem(Quantity = 1,

OpportunityId = o.Id, TotalPrice = o.Adjustment1__c == null ? 0: o.Adjustment1__c,

PriceBookEntryId = pbes[0].id, order__c = 10001));

//Adding discount2

oli.add( new OpportunityLineItem(Quantity = 1,

OpportunityId = o.Id, TotalPrice = o.Adjustment2__c == null ? 0: o.Adjustment2__c,

PriceBookEntryId = pbes[1].id, order__c = 10002));

//Adding discount3

oli.add( new OpportunityLineItem(Quantity = 1,

OpportunityId = o.Id, TotalPrice = o.Adjustment3__c == null ? 0: o.Adjustment3__c,

PriceBookEntryId = pbes[2].id, order__c = 10003));

//Adding discount4

oli.add( new OpportunityLineItem(Quantity = 1,

OpportunityId = o.Id, TotalPrice = o.Adjustment4__c == null ? 0: o.Adjustment4__c,

PriceBookEntryId = pbes[3].id, order__c = 10004));

//Adding discount5

oli.add( new OpportunityLineItem(Quantity = 1,

OpportunityId = o.Id, TotalPrice = o.Adjustment5__c == null ? 0: o.Adjustment5__c,

PriceBookEntryId = pbes[4].id, order__c = 10005));

//Adding Installation

oli.add( new OpportunityLineItem(Quantity = 1, OpportunityId = o.Id,

TotalPrice = o.Total_Installation__c == null ? 0: o.Total_Installation__c,

PriceBookEntryId = pbes[7].id, order__c = 10006));

//Adding freight

oli.add( new OpportunityLineItem(OpportunityId = o.Id,

Quantity = 1, TotalPrice = o.Total_Freight__c == null ? 0 : o.Total_Freight__c,

PriceBookEntryId = pbes[5].Id, order__c = 10007));

//adding SalesTax

oli.add( new OpportunityLineItem(Quantity = 1, OpportunityId = o.Id,

TotalPrice = o.Total_Sales_Tax__c == null ? 0: o.Total_Sales_Tax__c,

PriceBookEntryId = pbes[6].id, order__c = 10008));

upsert oli;

return;

}

System.Debug('Updating olis');

oli[0].TotalPrice = o.Adjustment1__c == null ? 0: o.Adjustment1__c;

oli[1].TotalPrice = o.Adjustment2__c == null ? 0: o.Adjustment2__c;

oli[2].TotalPrice = o.Adjustment3__c == null ? 0: o.Adjustment3__c;

oli[3].TotalPrice = o.Adjustment4__c == null ? 0: o.Adjustment4__c;

oli[4].TotalPrice = o.Adjustment5__c == null ? 0: o.Adjustment5__c;

oli[5].TotalPrice = o.Total_Installation__c == null ? 0: o.Total_Installation__c;

oli[6].TotalPrice = o.Total_Freight__c == null ? 0 : o.Total_Freight__c;

oli[7].TotalPrice = o.Total_Sales_Tax__c == null ? 0: o.Total_Sales_Tax__c;

Decimal counter = 1;

for(OpportunityLineItem ol: oli)

{

ol.order__c = 10000 + counter;

counter ++;

}

update oli;

}

}

 


 

 

 

 

Heres my test case in a seperate class

@isTest

private class TestOpportunityTriggers {

public static testMethod void testOppTrigger(){

Opportunity z = new Opportunity( name='Test',StageName='Closed Won',

CloseDate=Date.valueof('2008-05-01'), Amount=1.25, Adjustment5__c=1.25,

Adjustment4__c=1.25, Adjustment3__c=7.25, Adjustment2__c=0, Adjustment1__c=3,

CanRunTrigger__c=true);

insert z;

update z;

}

}

 


 

 

 

But my Force.com IDE keeps saying 0% of my trigger is covered and I need atleast 1% but when I run in the the browser it says it has 100% coverage.

 

What am i missing? Please any help would be great

Message Edited by mike83 on 09-18-2009 09:26 AM
Message Edited by mike83 on 09-18-2009 09:32 AM
JimRaeJimRae

My guess would be that your insert is failing for some reason, and the trigger is not firing.

 

I would enclose the insert in a try-catch loop, and report on errors:

 

 

@isTest private class TestOpportunityTriggers { public static testMethod void testOppTrigger(){ Opportunity z = new Opportunity( name='Test',StageName='Closed Won', CloseDate=Date.valueof('2008-05-01'), Amount=1.25, Adjustment5__c=1.25,Adjustment4__c=1.25, Adjustment3__c=7.25, Adjustment2__c=0, Adjustment1__c=3, CanRunTrigger__c=true); try{ insert z; }catch(DMLException d){ system.assert(false,'\n\nERROR INSERTING OPP: '+d.getDMLMessage(0)); } try{ update z; }catch(DMLException d1){ system.assert(false,'\n\nERROR UPDATING OPP: '+d1.getDMLMessage(0)); } } }

 

 

 

mike83mike83

Thanks I tried that  still nothing and I had added

System.Assert(z.name=='Test');

to validate but It still shows 100% code coverage in the browser based tests but says: "Test coverage of selected Apex Trigger is 0%, atleast 1% test coverage is required" 

JimRaeJimRae

Are you looking at the results in the the UI, or in Eclipse?  In Eclipse, I have seen results like this in the "General Warnings" section, but if I go to the bottom of the tree view, and open the "Code Coverage Results", find my trigger, I will often see the coverage is good, even 100%, depending on the complexity of the trigger.

If this is the case, when I deploy, the code is successfully moved to production.

 

mike83mike83
I get the error in eclipse but not on the browser one. And When I go to deploy it fails which is where I am stuck... it looks good but the deploy is failing. Eclipse just keeps saying that its not hitting the trigger.
JimRaeJimRae
Could you post the debug log from your deployment window in eclipse?
mike1983mike1983

Thank you so much for all your help if we can get this working I am sending you a gift card or something

 

 

 

addOpportunityProductsController1.myProductType from method public LIST:addOpportunityProductsController1.myProductType getPMProductTypes() in 479 ms
20090918185929.963:Class.addOpportunityProductsController1.saveChanges: line 589, column 9:     returning from end of method private void UpdateOliOrder() in 1680 ms
20090918185929.963:Class.addOpportunityProductsController1.saveChanges: line 592, column 9: Update: SOBJECT:Opportunity
*** Beginning OppertunityUpdate on Opportunity trigger event BeforeUpdate for 0067000000G4I0f


Cumulative resource usage:

Resource usage for namespace: (default)
Number of SOQL queries: 9 out of 100
Number of query rows: 355 out of 500 ******* CLOSE TO LIMIT
Number of SOSL queries: 0 out of 20
Number of DML statements: 1 out of 100
Number of DML rows: 1 out of 500
Number of script statements: 31447 out of 200000
Maximum heap size: 144969 out of 1000000
Number of callouts: 0 out of 10
Number of Email Invocations: 0 out of 10
Number of fields describes: 2 out of 10
Number of record type describes: 0 out of 10
Number of child relationships describes: 0 out of 10
Number of picklist describes: 2 out of 10
Number of future calls: 0 out of 10
Number of find similar calls: 0 out of 10
Number of System.runAs() invocations: 0 out of 20

Total email recipients queued to be sent : 0

*** Ending OppertunityUpdate on Opportunity trigger event BeforeUpdate for 0067000000G4I0f


*** Beginning Workflow Evaluation
User: Mike Rametta
Start Time: 20090918185936.706
Starting All Rules Evaluation
Starting evaluation of rule type Assignment
Starting evaluation of rule type Response
Starting evaluation of rule type Workflow
[Opportunity: KRE001 (2) 0067000000G4I0f]
Rule Name: Alert Closed Won
Trigger type: ON_CREATE_OR_TRIGGERING_UPDATE
Evaluating Workflow Entry Criteria:
[Opportunity : Won equals true]
Value found: 0
Criteria evaluates to false
Spooling All Immediate Actions
Ending All Rules Evaluation
Bulk Execute all Immediate Actions
Starting evaluation of rule type Escalation
End Time: 20090918185936.724
*** Ending Workflow Evaluation20090918185929.963:Class.addOpportunityProductsController1.saveChanges: line 592, column 9:     DML Operation executed in 827 ms
20090918185929.963:Class.addOpportunityProductsController1.saveChanges: line 595, column 9: ===============saveChanges()===
20090918185929.963:Class.addOpportunityProductsController1.saveChanges: line 596, column 9: SelectLoop:LIST:addOpportunityProductsController1.myFamily
20090918185929.963:Class.addOpportunityProductsController1.saveChanges: line 597, column 13: ===============BEGIN FAMILY: Software
20090918185929.963:Class.addOpportunityProductsController1.saveChanges: line 598, column 13: SelectLoop:LIST:addOpportunityProductsController1.myProductType
20090918185929.963:Class.addOpportunityProductsController1.saveChanges: line 599, column 17: ===============BEGIN PRODUCTTYPE: meridianEMR Software
20090918185929.963:Class.addOpportunityProductsController1.saveChanges: line 600, column 17: SelectLoop:LIST:SOBJECT:OpportunityLineItem
20090918185929.963:Class.addOpportunityProductsController1.saveChanges: line 602, column 21: ===============BEGIN OLI EMRURPH: 1.00
20090918185929.963:Class.addOpportunityProductsController1.saveChanges: line 604, column 21: SelectLoop:LIST:SOBJECT:OpportunityLineItem
20090918185929.963:Class.addOpportunityProductsController1.saveChanges: line 606, column 29: ===============MATCH eOLI:1.00
20090918185929.963:Class.addOpportunityProductsController1.saveChanges: line 611, column 30: OliUpdated: 00k70000009VpD4AAK; Order0.0
20090918185929.963:Class.addOpportunityProductsController1.saveChanges: line 612, column 33: Update: SOBJECT:OpportunityLineItem

*** Beginning Workflow Evaluation
User: Mike Rametta
Start Time: 20090918185936.820
Starting All Rules Evaluation
Starting evaluation of rule type Assignment
Starting evaluation of rule type Response
Starting evaluation of rule type Workflow
[Opportunity Product:  00k70000009VpD4]
Rule Name: UpdateDiscountDescription
Trigger type: ON_ALL_CHANGES
Evaluating Workflow Entry Criteria:
[Product : Product Name contains Discount]
AND [Opportunity Product : Total Price not equal to 0]
Value found: EMRURPH
Criteria evaluates to false
[Opportunity Product:  00k70000009VpD4]
Rule Name: OpptyLineFieldUpdates
Trigger type: ON_ALL_CHANGES
Evaluating Workflow Entry Criteria:
[Opportunity Product : Created Date not equal to null]
Value found: 2009-05-28 15:36:45
Criteria evaluates to true
Spooling All Immediate Actions
 [Opportunity Product:  00k70000009VpD4]
  Field Update
  Field: Opportunity Product: Product EMR_PM Flag
  Value: EMR
Skipping Field Update since the old value and new value for the field are the same: EMR
 [Opportunity Product:  00k70000009VpD4]
  Field Update
  Field: Opportunity Product: Product Family
  Value: Software
Skipping Field Update since the old value and new value for the field are the same: Software
 [Opportunity Product:  00k70000009VpD4]
  Field Update
  Field: Opportunity Product: Installation Total
  Value:
Skipping Field Update since the old value and new value for the field are the same: null
 [Opportunity Product:  00k70000009VpD4]
  Field Update
  Field: Opportunity Product: Product Category
  Value: meridianEMR Software
Skipping Field Update since the old value and new value for the field are the same: meridianEMR Software
Ending All Rules Evaluation
Bulk Execute all Immediate Actions
Starting evaluation of rule type Escalation
End Time: 20090918185936.849
*** Ending Workflow Evaluation
*** Beginning Workflow Evaluation
User: Mike Rametta
Start Time: 20090918185936.850
Starting All Rules Evaluation
Starting evaluation of rule type Assignment
Starting evaluation of rule type Response
Starting evaluation of rule type Workflow
[Opportunity: KRE001 (2) 0067000000G4I0f]
Rule Name: Alert Closed Won
Trigger type: ON_CREATE_OR_TRIGGERING_UPDATE
Evaluating Workflow Entry Criteria:
[Opportunity : Won equals true]
Value found: 0
Criteria evaluates to false
Spooling All Immediate Actions
Ending All Rules Evaluation
Bulk Execute all Immediate Actions
Starting evaluation of rule type Escalation
End Time: 20090918185936.851
*** Ending Workflow Evaluation20090918185929.963:Class.addOpportunityProductsController1.saveChanges: line 612, column 33:     DML Operation executed in 171 ms
20090918185929.963:Class.addOpportunityProductsController1.saveChanges: line 602, column 21: ===============BEGIN OLI EMRURNP/PA: 2.00
20090918185929.963:Class.addOpportunityProductsController1.saveChanges: line 604, column 21: SelectLoop:LIST:SOBJECT:OpportunityLineItem
20090918185929.963:Class.addOpportunityProductsController1.saveChanges: line 606, column 29: ===============MATCH eOLI:2.00
20090918185929.963:Class.addOpportunityProductsController1.saveChanges: line 611, column 30: OliUpdated: 00k70000009VpIpAAK; Order1.0
20090918185929.963:Class.addOpportunityProductsController1.saveChanges: line 612, column 33: Update: SOBJECT:OpportunityLineItem

*** Beginning Workflow Evaluation
User: Mike Rametta
Start Time: 20090918185936.970
Starting All Rules Evaluation
Starting evaluation of rule type Assignment
Starting evaluation of rule type Response
Starting evaluation of rule type Workflow
[Opportunity Product:  00k70000009VpIp]
Rule Name: UpdateDiscountDescription
Trigger type: ON_ALL_CHANGES
Evaluating Workflow Entry Criteria:
[Product : Product Name contains Discount]
AND [Opportunity Product : Total Price not equal to 0]
Value found: EMRURNP/PA
Criteria evaluates to false
[Opportunity Product:  00k70000009VpIp]
Rule Name: OpptyLineFieldUpdates
Trigger type: ON_ALL_CHANGES
Evaluating Workflow Entry Criteria:
[Opportunity Product : Created Date not equal to null]
Value found: 2009-05-28 15:36:46
Criteria evaluates to true
Spooling All Immediate Actions
 [Opportunity Product:  00k70000009VpIp]
  Field Update
  Field: Opportunity Product: Product EMR_PM Flag
  Value: EMR
Skipping Field Update since the old value and new value for the field are the same: EMR
 [Opportunity Product:  00k70000009VpIp]
  Field Update
  Field: Opportunity Product: Product Family
  Value: Software
Skipping Field Update since the old value and new value for the field are the same: Software
 [Opportunity Product:  00k70000009VpIp]
  Field Update
  Field: Opportunity Product: Installation Total
  Value:
Skipping Field Update since the old value and new value for the field are the same: null
 [Opportunity Product:  00k70000009VpIp]
  Field Update
  Field: Opportunity Product: Product Category
  Value: meridianEMR Software
Skipping Field Update since the old value and new value for the field are the same: meridianEMR Software
Ending All Rules Evaluation
Bulk Execute all Immediate Actions
Starting evaluation of rule type Escalation
End Time: 20090918185936.972
*** Ending Workflow Evaluation
*** Beginning Workflow Evaluation
User: Mike Rametta
Start Time: 20090918185936.974
Starting All Rules Evaluation
Starting evaluation of rule type Assignment
Starting evaluation of rule type Response
Starting evaluation of rule type Workflow
[Opportunity: KRE001 (2) 0067000000G4I0f]
Rule Name: Alert Closed Won
Trigger type: ON_CREATE_OR_TRIGGERING_UPDATE
Evaluating Workflow Entry Criteria:
[Opportunity : Won equals true]
Value found: 0
Criteria evaluates to false
Spooling All Immediate Actions
Ending All Rules Evaluation
Bulk Execute all Immediate Actions
Starting evaluation of rule type Escalation
End Time: 20090918185936.974
*** Ending Workflow Evaluation20090918185929.963:Class.addOpportunityProductsController1.saveChanges: line 612, column 33:     DML Operation executed in 122 ms
20090918185929.963:Class.addOpportunityProductsController1.saveChanges: line 602, column 21: ===============BEGIN OLI Blended Model: 3.00
20090918185929.963:Class.addOpportunityProductsController1.saveChanges: line 604, column 21: SelectLoop:LIST:SOBJECT:OpportunityLineItem
20090918185929.963:Class.addOpportunityProductsController1.saveChanges: line 606, column 29: ===============MATCH eOLI:3.00
20090918185929.963:Class.addOpportunityProductsController1.saveChanges: line 611, column 30: OliUpdated: 00k70000009VpIqAAK; Order2.0
20090918185929.963:Class.addOpportunityProductsController1.saveChanges: line 612, column 33: Update: SOBJECT:OpportunityLineItem

*** Beginning Workflow Evaluation
User: Mike Rametta
Start Time: 20090918185937.098
Starting All Rules Evaluation
Starting evaluation of rule type Assignment
Starting evaluation of rule type Response
Starting evaluation of rule type Workflow
[Opportunity Product:  00k70000009VpIq]
Rule Name: UpdateDiscountDescription
Trigger type: ON_ALL_CHANGES
Evaluating Workflow Entry Criteria:
[Product : Product Name contains Discount]
AND [Opportunity Product : Total Price not equal to 0]
Value found: Blended Model
Criteria evaluates to false
[Opportunity Product:  00k70000009VpIq]
Rule Name: OpptyLineFieldUpdates
Trigger type: ON_ALL_CHANGES
Evaluating Workflow Entry Criteria:
[Opportunity Product : Created Date not equal to null]
Value found: 2009-05-28 15:36:47
Criteria evaluates to true
Spooling All Immediate Actions
 [Opportunity Product:  00k70000009VpIq]
  Field Update
  Field: Opportunity Product: Product EMR_PM Flag
  Value: EMR
Skipping Field Update since the old value and new value for the field are the same: EMR
 [Opportunity Product:  00k70000009VpIq]
  Field Update
  Field: Opportunity Product: Product Family
  Value: Software
Skipping Field Update since the old value and new value for the field are the same: Software
 [Opportunity Product:  00k70000009VpIq]
  Field Update
  Field: Opportunity Product: Installation Total
  Value:
Skipping Field Update since the old value and new value for the field are the same: null
 [Opportunity Product:  00k70000009VpIq]
  Field Update
  Field: Opportunity Product: Product Category
  Value: meridianEMR Software
Skipping Field Update since the old value and new value for the field are the same: meridianEMR Software
Ending All Rules Evaluation
Bulk Execute all Immediate Actions
Starting evaluation of rule type Escalation
End Time: 20090918185937.100
*** Ending Workflow Evaluation
*** Beginning Workflow Evaluation
User: Mike Rametta
Start Time: 20090918185937.101
Starting All Rules Evaluation
Starting evaluation of rule type Assignment
Starting evaluation of rule type Response
Starting evaluation of rule type Workflow
[Opportunity: KRE001 (2) 0067000000G4I0f]
Rule Name: Alert Closed Won
Trigger type: ON_CREATE_OR_TRIGGERING_UPDATE
Evaluating Workflow Entry Criteria:
[Opportunity : Won equals true]
Value found: 0
Criteria evaluates to false
Spooling All Immediate Actions
Ending All Rules Evaluation
Bulk Execute all Immediate Actions
Starting evaluation of rule type Escalation
 

JimRaeJimRae

From your log, it appears that your insert is not working, odd that it doesn't show the insert occuring at all, not even failing.   I can see in about the 6th line down, the BeforeUpdatepart of your trigger is firing.  Odd that the insert isn't showing at all.

I would recommend you add more debug statements to your testmethod, so you can better see where things are going wrong. Here is a version you could use:

 

 

@isTest private class TestOpportunityTriggers { public static testMethod void testOppTrigger(){ system.debug('\n\nSTARTING TESTMethod'); Opportunity z = new Opportunity( name='Test',StageName='Closed Won', CloseDate=Date.valueof('2008-05-01'), Amount=1.25, Adjustment5__c=1.25,Adjustment4__c=1.25, Adjustment3__c=7.25, Adjustment2__c=0, Adjustment1__c=3, CanRunTrigger__c=true); system.debug('\n\nTHIS IS THE Opportunity, pre insert:'); system.debug('\n\n'+z); system.debug('\n\n'); try{ insert z; system.debug('\n\nINSERTED THE Opportunity Successfully'); }catch(DMLException d){ system.assert(false,'\n\nERROR INSERTING OPP: '+d.getDMLMessage(0)); system.debug('\n\nERROR INSERTING THE Opportunity Successfully'); } system.debug('\n\nTHIS IS THE INSERTED Opportunity:'); system.debug('\n\n'+z); system.debug('\n\n'); try{ update z; system.debug('\n\nUPDATED THE Opportunity Successfully'); }catch(DMLException d1){ system.assert(false,'\n\nERROR UPDATING OPP: '+d1.getDMLMessage(0)); system.debug('\n\nERROR UPDATING THE Opportunity'); } } }