You need to sign in to do that
Don't have an account?
Lights
Apex Trigger: Increasing coverage. Help?
Hello Friends,
I'm getting the following error because the trigger doesn't have enough coverage. How do I accomplish that? Thank you!
The following triggers have 0% code coverage. Each trigger must have at least 1% code coverage.
I'm getting the following error because the trigger doesn't have enough coverage. How do I accomplish that? Thank you!
The following triggers have 0% code coverage. Each trigger must have at least 1% code coverage.
trigger Product00001 on OpportunityLineItem( after insert, after update ) { List<OpportunityLineItem> oliList = new List<OpportunityLineItem>(); Set<Id> opptyIds = new Set<Id>(); for( OpportunityLineItem optLineItem: trigger.new ) { if( optLineItem.ProductCode == '00001a' ) { opptyIds.add( optLineItem.OpportunityId ); } } if( opptyIds.size() > 0 ) { //retrieve the values based on Product list List<OpportunityLineItem> lstOpptyLineItems = [ SELECT Opportunity.Pricebook2Id, OpportunityId, Name, ProductCode, PricebookEntryId, Quantity, UnitPrice FROM OpportunityLineItem WHERE OpportunityId IN: opptyIds order by ProductCode ]; Map<Id, List<OpportunityLineItem>> mapOpptyLineItem = new Map<Id, List<OpportunityLineItem>>(); for( OpportunityLineItem item : lstOpptyLineItems ) { List<OpportunityLineItem> oliList1 = new List<OpportunityLineItem>(); if( mapOpptyLineItem.containsKey( item.OpportunityId )) { oliList1 = mapOpptyLineItem.get( item.OpportunityId ); } oliList1.add( item ); mapOpptyLineItem.put( item.OpportunityId, oliList1 ); } //retrieve PriceBookEntry of the Product B, this is most important PricebookEntry pbeProduct2 = [ SELECT Id, Pricebook2Id, UnitPrice, Name, Percentage_Entry__c FROM PricebookEntry WHERE Name ='Product 00001' AND Pricebook2Id IN (SELECT Id FROM PriceBook2 WHERE Id ='01sA00000004lbRIAQ') LIMIT 1 ]; if( trigger.isInsert ) { for( Id oppId : mapOpptyLineItem.keySet() ) { for( OpportunityLineItem item : mapOpptyLineItem.get( oppId )) { if( item.ProductCode == '00001a' ) { oliList.add( new OpportunityLineItem( OpportunityId = oppId, PricebookEntryId = pbeProduct2.Id, Quantity = item.Quantity, UnitPrice = item.UnitPrice * pbeProduct2.Percentage_Entry__c * 0.01 ) ); } } } if( oliList.size() > 0 ) insert oliList; } else if( trigger.isUpdate ) { for( Id oppId : mapOpptyLineItem.keySet() ) { OpportunityLineItem itemProductA = new OpportunityLineItem(); for( OpportunityLineItem item : mapOpptyLineItem.get( oppId )) { if( item.ProductCode == '00001a' ) { itemProductA = item; break; } } for( OpportunityLineItem item : mapOpptyLineItem.get( oppId )) { if( item.ProductCode == '00001b' ) { oliList.add( new OpportunityLineItem( Id = item.Id, OpportunityId = oppId, PricebookEntryId = pbeProduct2.Id, Quantity = itemProductA.Quantity, UnitPrice = itemProductA.UnitPrice * pbeProduct2.Percentage_Entry__c * 0.01 ) ); } } } if( oliList.size() > 0 ) update oliList; } } }
You will have to write a test class to provide coverage for your trigger, this is standard with Salesforce APEX development.
Test class in Salesforce effectively allow us to simulate scenarios that the tested logic and functions would normally do, without actually persisting the action into the system (so it's safe as far as the database is concerned) and whatever lines of your code were executed in the process by the test class when it ran will be considered as covered.
In this case i can see you have a trigger on OpportunityLineItem with after insert and update, so your test class will have to simulate this process by generating and/or updating a test record of OpportunityLineItem.
For more information on test classes please see https://trailhead.salesforce.com/en/modules/apex_testing/units/apex_testing_intro
I actually have a test class. Sorry. I didn't post it.