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
anil87anil87 

Test class for opportunity trigger

Hi Everyone

 

i need to erite test class for the following trigger tell me how to write test class with this trigger

 

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

List<OpportunityLineItem> oli=new List<OpportunityLineItem>();
List<product2> pro=new List<product2>();
List<FullFillment_Part__c> ffp=new List<FullFillment_Part__c>();
Set<ID> prodids =new Set<ID>();

for(Opportunity o:trigger.new){
if(o.Design_Win_Approval_Process__c=='Approved')
{
oli=[select PricebookEntryId,PricebookEntry.Product2Id from OpportunityLineItem
where Opportunityid =: o.id];
}

for(OpportunityLineItem ol:oli){

prodids.add(ol.PricebookEntry.Product2Id);
}
System.Debug('Product Ids are '+prodids);

pro=[select id,name,ProductCode from Product2
where id in: prodids];

for(product2 p:pro){

FullFillment_Part__c fp=new FullFillment_Part__c();
fp.Opportunity__c=o.id;
fp.product__c=p.id;
fp.Fullfillment_Part__c=p.id;

list<OpportunityLineItem> ol=[select PricebookEntryId,PricebookEntry.Product2Id,Product_Design_Win_Value_USD_1__c,Product_Design_Win_Value_USD_2__c from OpportunityLineItem
where PricebookEntry.Product2Id =:p.id and Opportunityid =: o.id];
for(OpportunityLineItem oli1:ol){

fp.Product_Design_Win_Value_USD__c=oli1.Product_Design_Win_Value_USD_1__c;
fp.Product_Design_Win_Value_USD_Adjust__c=oli1.Product_Design_Win_Value_USD_2__c;

}
ffp.add(fp);
}
insert ffp;
}
}

Jeff MayJeff May

You can create an Apex Class called, "TestOpportunityTriggers", then add a testmethod that creates and updates an Opportunity.  The trigger will fire when the Opportunity is created/changed.