You need to sign in to do that
Don't have an account?
SFDC12
req
Hi everyone tried below trigger which is working fine but testclass failed,can someone help me out.
Trigger:
//when opportunity is inserted opportunityLineItem with one product associated should insert
trigger Insertingoppandoppline on Opportunity (after insert) {
Pricebook2 standardPb = [select id, name, isActive from Pricebook2 where IsStandard = true limit 1];
Product2 prd1 = new Product2 (); // ----> Create product
prd1.Name='myproduct';
insert prd1;
PricebookEntry pbe1 = new PricebookEntry (); //------->Create PriceBookEntry
pbe1.Product2ID=prd1.id;
pbe1.Pricebook2ID=standardPb.id;
pbe1.UnitPrice=50;
pbe1.isActive=true;
insert pbe1;
List<OpportunityLineItem> oplist=new List<OpportunityLineItem>(); //-->Create List to store OpportunityLineItem
for(Opportunity opp: Trigger.New)
{
OpportunityLineItem oppli = new OpportunityLineItem(); //---->Create OpportunityLineItem.
oppli.PricebookEntryId=pbe1.Id;
oppli.OpportunityId = opp.Id;
oppli.Quantity = 5;
oppli.TotalPrice = 10.0;
oplist.add(oppli);
}
insert oplist; //----->insert OpportunityLineItem
}
testclass:
@isTest
public class Testinsertoppandoppline {
@isTest
static void invoke(){
PricebookEntry PBE=[select id from PricebookEntry limit 1 ];
Opportunity opp= new Opportunity();
opp.name='testOpp';
opp.closeDate=system.today()+20;
opp.stageName='Qualification';
try{
insert opp;
}
catch(Exception e){
System.debug(e);
}
List<opportunitylineitem> opppro = new opportunitylineitem[]{new opportunitylineitem(UnitPrice=10000.00, Quantity=10, opportunityid=opp.id, pricebookEntryId=PBE.id),new opportunitylineitem(UnitPrice=10000.00, Quantity=10, opportunityid=opp.id, pricebookEntryId=PBE.id),new opportunitylineitem(UnitPrice=10000.00, Quantity=10, opportunityid=opp.id, pricebookEntryId=PBE.id),new opportunitylineitem(UnitPrice=10000.00, Quantity=10, pricebookEntryId=PBE.id, opportunityid=opp.id)};
insert opppro;
}
}
Trigger:
//when opportunity is inserted opportunityLineItem with one product associated should insert
trigger Insertingoppandoppline on Opportunity (after insert) {
Pricebook2 standardPb = [select id, name, isActive from Pricebook2 where IsStandard = true limit 1];
Product2 prd1 = new Product2 (); // ----> Create product
prd1.Name='myproduct';
insert prd1;
PricebookEntry pbe1 = new PricebookEntry (); //------->Create PriceBookEntry
pbe1.Product2ID=prd1.id;
pbe1.Pricebook2ID=standardPb.id;
pbe1.UnitPrice=50;
pbe1.isActive=true;
insert pbe1;
List<OpportunityLineItem> oplist=new List<OpportunityLineItem>(); //-->Create List to store OpportunityLineItem
for(Opportunity opp: Trigger.New)
{
OpportunityLineItem oppli = new OpportunityLineItem(); //---->Create OpportunityLineItem.
oppli.PricebookEntryId=pbe1.Id;
oppli.OpportunityId = opp.Id;
oppli.Quantity = 5;
oppli.TotalPrice = 10.0;
oplist.add(oppli);
}
insert oplist; //----->insert OpportunityLineItem
}
testclass:
@isTest
public class Testinsertoppandoppline {
@isTest
static void invoke(){
PricebookEntry PBE=[select id from PricebookEntry limit 1 ];
Opportunity opp= new Opportunity();
opp.name='testOpp';
opp.closeDate=system.today()+20;
opp.stageName='Qualification';
try{
insert opp;
}
catch(Exception e){
System.debug(e);
}
List<opportunitylineitem> opppro = new opportunitylineitem[]{new opportunitylineitem(UnitPrice=10000.00, Quantity=10, opportunityid=opp.id, pricebookEntryId=PBE.id),new opportunitylineitem(UnitPrice=10000.00, Quantity=10, opportunityid=opp.id, pricebookEntryId=PBE.id),new opportunitylineitem(UnitPrice=10000.00, Quantity=10, opportunityid=opp.id, pricebookEntryId=PBE.id),new opportunitylineitem(UnitPrice=10000.00, Quantity=10, pricebookEntryId=PBE.id, opportunityid=opp.id)};
insert opppro;
}
}
Please apply the test class as below. It will cover 100% with out errors.
If this solution helps, Please mark it as best answer.
Thanks,