You need to sign in to do that
Don't have an account?

Test Class help for an Apex Trigger
I have the following apex trigger and I need a test class. Can you guys check what I got so far?
Apex Trigger
Apex Trigger
trigger NOVEMBER2 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 == '00002a' ) { 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, Pass_Through_Percentage_Entry__c FROM PricebookEntry WHERE Name ='Car Oil Fee' 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 == '00002a' ) { oliList.add( new OpportunityLineItem( OpportunityId = oppId, PricebookEntryId = pbeProduct2.Id, Quantity = item.Quantity, UnitPrice = item.UnitPrice * pbeProduct2.Pass_Through_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 == '00002a' ) { itemProductA = item; continue; } if( item.ProductCode == '00002b' ) { oliList.add( new OpportunityLineItem( Id = item.Id, OpportunityId = oppId, PricebookEntryId = pbeProduct2.Id, Quantity = itemProductA.Quantity, UnitPrice = itemProductA.UnitPrice * pbeProduct2.Pass_Through_Percentage_Entry__c * 0.01 ) ); } } } if( oliList.size() > 0 ) update oliList; } } }Test Class:
@isTest private class OpportunityLineItemTrigger_Test{ static testMethod void test_OpportunityLineItemTrigger(){ test.startTest(); OpportunityLineItem opportunitylineitem_Obj = new OpportunityLineItem(OpportunityId = opportunity_Obj[0].id, PricebookEntryId = pricebookentry_Obj[0].id, Quantity = 9, UnitPrice = 13, Package_Delivery_Date__c = false, Batch_Readiness__c = false); Insert opportunitylineitem_Obj; test.stopTest(); } static testMethod void test_UseCase2(){ test.startTest(); OpportunityLineItem opportunitylineitem_Obj = new OpportunityLineItem(OpportunityId = opportunity_Obj[0].id, PricebookEntryId = pricebookentry_Obj[0].id, Quantity = 9, UnitPrice = 13, Package_Delivery_Date__c = false, Batch_Readiness__c = false); opportunitylineitem_Obj.ProductCode='UseCase1-0'; Insert opportunitylineitem_Obj; test.stopTest(); } }
Please try below test class: Also, I suggest not to hard code Id either in Trigger and test class, as it will change in any other Salesforce Environment.
Thanks,
Neetu
All Answers
Please try below test class: Also, I suggest not to hard code Id either in Trigger and test class, as it will change in any other Salesforce Environment.
Thanks,
Neetu
How to erite the test class in bel;ow trigger?
trigger SendAdmitCard on Admit_Card__c (after update) {
Set<Admit_Card__c> admitCardDetails = new Set<Admit_Card__c>();
if(Trigger.isInsert || Trigger.isUpdate) {
for(Admit_Card__c admitCard : trigger.new){
if(admitCard.FUA_Broadcast_Admit_Card__c){
admitCardDetails.add(admitCard);
}
}
}
if(admitCardDetails.size()>0) AdmitCardDetailsHandler.CreateAdmitCard(admitCardDetails);
}