You need to sign in to do that
Don't have an account?
Bhatia
Trigger.new not reading the list price field in QuoteLineItem
In the Test class , I need to complete the Test coverage for a Trigger on QuoteLineItem. When I am inserting a QuoteLineItem like
insert Qli;
then trigger is firing on the before insert event.
when i am debugging the System.debug(Trigger.new), it is not reading the List Price (Read only field) so that I am unable to cover the Test coverage for a Trigger. As this field is automatically getting the value from unit price field of PriceBookEntry.
Please it is urgent for me......
Any comment will be appreciated.
Thanks
Please share your trigger and test class both.
Apex Triggerr
=======================
trigger CalcDiscQuoteLI on QuoteLineItem (before insert,before update) {
//PURPOSE: When the discount field is left blank, automatically insert
// the discount using the Sales Price (unitprice) entered by the user
// and the List Price.
for (QuoteLineItem li: Trigger.new){
System.debug('Trigger.new #######################' + li);
if (li.listprice!= null){
if (li.unitprice != null){
if (li.discount == null){
li.discount = ((li.listprice - li.unitprice)/li.listprice) * 100;
system.debug('++++' + li.discount);
li.unitprice = li.listprice;
}
}
}
}
}
Test Class
==================
@isTest
private class TestUpdateQuoteLIDiscount
{
static testMethod void setupTestOppQuote(){
Double insertedOLIDisc;
Double insertedOLIListPrice;
Double insertedQLIDISC;
Double insertedQLIUnitPrice;
Double insertedQLIListPrice;
// Create an account
Account a=new Account();
a.name='TestAcct';
insert a;
// Get the standard pricebook
Pricebook2 pb =[select name,isactive from Pricebook2 where isStandard=true];
if(!pb.isactive){
pb.isactive=true;
Database.update(pb);
}
// get valid stage name
OpportunityStage stage=[select Masterlabel from OpportunityStage limit 1];
// setup basic opportunity
Opportunity o = new Opportunity();
o.Name='TestOpp';
o.AccountID=a.id;
o.CloseDate=date.today();
o.StageName=stage.masterlabel;
o.Pricebook2Id=pb.id;
insert o;
// Create a product2
Product2 p = new Product2();
p.Name='TestProd';
insert p;
//Create pricebook entry
PricebookEntry pbe=new PricebookEntry();
pbe.Pricebook2Id=pb.id;
pbe.Product2Id=p.id;
pbe.IsActive=true;
pbe.UnitPrice=10;
insert pbe;
system.debug('pricebookentryid:::'+pbe);
system.debug(pbe.unitprice);
// create opportunity line item
OpportunityLineItem oli = new OpportunityLineItem();
oli.opportunityId=o.id;
oli.pricebookentryid=pbe.id;
oli.quantity=1;
oli.discount=0.01;
oli.totalprice = 45.46;
insert oli;
//Check that OLI has been inserted
for(OpportunityLineItem x :[select discount,listprice from OpportunityLineItem where createdDate=Today]){
insertedOLIDisc=x.discount;
insertedOLIListPrice=x.listprice;
system.debug(x.discount);
system.debug('jhjhjjkhjhhkhkhkj'+x.listprice);
}
Quote q = new Quote();
q.opportunityId=o.id;
q.Pricebook2Id=pb.id;
q.name='TestQuote';
insert q;
QuoteLineItem qli = new QuoteLineItem();
qli.quoteId=q.id;
qli.pricebookentryid=pbe.id;
qli.quantity=1;
qli.unitprice=5;
qli.discount=0.02;
system.debug(qli.pricebookentryid);
system.debug(qli.listprice);
QuoteLineItem o1=qli;
insert o1;
System.debug('qllllllllllllllllllllqq'+o1);
QuoteLineItem y1;
y1 =[select listprice,quantity,unitprice,discount from quotelineItem where id=:o1.id];
system.debug('New List price'+y1);
y1.discount=null;
Test.startTest();
update y1;
Test.stopTest();
/*system.debug(qli.discount);
system.debug(qli.unitprice);
system.debug(qli.listprice);
*/
/*QuoteLineItem y=new QuoteLineItem();
y =[select discount,unitprice,listprice from quotelineItem where createdDate=Today and unitprice=5];
System.debug('hjkhkshdksdhsd'+y);*/
//update y1;
//System.debug('hjkhkshdksdhsdfdfffddf'+y1);
/*
for(QuoteLineItem y :[select discount,unitprice,listprice from quotelineItem where createdDate=Today and unitprice=5])
{
System.debug('hjkhkshdksdhsd'+y.listprice);
update y;
}*/
}
}
Do these changes to your test class discount should be null so that this could be covered
if (li.discount == null){
li.discount = ((li.listprice - li.unitprice)/li.listprice) * 100;
system.debug('++++' + li.discount);
li.unitprice = li.listprice;
}
Please read my comments
It is not entering into the If(li.listprice!=null) condition, If it enters then test coverage can be 100%. It is now 28% only. I am giving the code again after modifying into it. Please test in your dev org.
Apex Trigger :
trigger CalcDiscQuoteLI on QuoteLineItem (before insert,before update) {
//PURPOSE: When the discount field is left blank, automatically insert
// the discount using the Sales Price (unitprice) entered by the user
// and the List Price.
for (QuoteLineItem li: Trigger.new){
System.debug('Trigger.new #######################' + li);
if (li.listprice!= null){
if (li.unitprice != null){
if (li.discount == null){
li.discount = ((li.listprice - li.unitprice)/li.listprice) * 100;
system.debug('++++' + li.discount);
li.unitprice = li.listprice;
}
}
}
}
}
Test Class :
@isTest
private class TestUpdateQuoteLIDiscount
{
static testMethod void setupTestOppQuote(){
Double insertedOLIDisc;
Double insertedOLIListPrice;
Double insertedQLIDISC;
Double insertedQLIUnitPrice;
Double insertedQLIListPrice;
// Create an account
Account a=new Account();
a.name='TestAcct';
insert a;
// Get the standard pricebook
Pricebook2 pb =[select name,isactive from Pricebook2 where isStandard=true];
if(!pb.isactive){
pb.isactive=true;
Database.update(pb);
}
// get valid stage name
OpportunityStage stage=[select Masterlabel from OpportunityStage limit 1];
// setup basic opportunity
Opportunity o = new Opportunity();
o.Name='TestOpp';
o.AccountID=a.id;
o.CloseDate=date.today();
o.StageName=stage.masterlabel;
o.Pricebook2Id=pb.id;
insert o;
// Create a product2
Product2 p = new Product2();
p.Name='TestProd';
insert p;
//Create pricebook entry
PricebookEntry pbe=new PricebookEntry();
pbe.Pricebook2Id=pb.id;
pbe.Product2Id=p.id;
pbe.IsActive=true;
pbe.UnitPrice=10;
insert pbe;
system.debug('pricebookentryid:::'+pbe);
system.debug(pbe.unitprice);
// create opportunity line item
OpportunityLineItem oli = new OpportunityLineItem();
oli.opportunityId=o.id;
oli.pricebookentryid=pbe.id;
oli.quantity=1;
oli.discount=0.01;
oli.totalprice = 45.46;
insert oli;
//Check that OLI has been inserted
for(OpportunityLineItem x :[select discount,listprice from OpportunityLineItem where id =: oli.id]){
insertedOLIDisc=x.discount;
insertedOLIListPrice=x.listprice;
system.debug(x.discount);
system.debug('jhjhjjkhjhhkhkhkj'+x.listprice);
}
Quote q = new Quote();
q.opportunityId=o.id;
q.Pricebook2Id=pb.id;
q.name='TestQuote';
insert q;
QuoteLineItem qli = new QuoteLineItem();
qli.quoteId=q.id;
qli.pricebookentryid=pbe.id;
qli.quantity=1;
qli.unitprice=5;
qli.discount=null;
system.debug(qli.pricebookentryid);
system.debug(qli.listprice);
insert qli;
System.debug('qllllllllllllllllllllqq'+qli);
QuoteLineItem y1;
y1 =[select listprice,quantity,unitprice,discount from quotelineItem where id=:qli.id];
system.debug('New List price'+y1);
system.assertequals(pbe.unitprice,y1.listprice);
y1.discount=null;
Test.startTest();
update y1;
Test.stopTest();
/*system.debug(qli.discount);
system.debug(qli.unitprice);
system.debug(qli.listprice);
*/
/*QuoteLineItem y=new QuoteLineItem();
y =[select discount,unitprice,listprice from quotelineItem where createdDate=Today and unitprice=5];
System.debug('hjkhkshdksdhsd'+y);*/
//update y1;
//System.debug('hjkhkshdksdhsdfdfffddf'+y1);
/*
for(QuoteLineItem y :[select discount,unitprice,listprice from quotelineItem where createdDate=Today and unitprice=5])
{
System.debug('hjkhkshdksdhsd'+y.listprice);
update y;
}*/
}
}
Please do as mentioned in your test class, hope will help you
It is not writable, we cannot give any value in it. Please do test in your dev org, so that you will get the issue in System Logs or Debug Logs. Where the issue is residing.
In the Debug Log :
QuoteLineItem:{QuoteId=0Q090000000CoLaCAK, Quantity=1.00, PricebookEntryId=01u90000000c80nAAA, LastModifiedById=00590000000dwDDAAY, Subtotal=5.00, TotalPrice=5.00, LastModifiedDate=2011-06-16 16:48:43, UnitPrice=5.00, LineNumber=00000062, SystemModstamp=2011-06-16 16:48:43, CreatedById=00590000000dwDDAAY, CreatedDate=2011-06-16 16:48:43, IsDeleted=false, Id=0QL90000000CqTIGA0}
I am not able to find the ListPrice field so that the test coverage of the trigger is not going 100%.
However, Trigger is working fine , If we check it by the UI interface.
Hi,
I think you cant create Price book entry,
Instead try querying PBE with standard price.
My issue is with QuoteLineItem not with the PriceBookEntry. But yes, QuotelIneItem is associated with the PriceBookEntry. If you can some changes in the code. I have provided all the code on which I am working on it. Please give some workaround it.
Thanks