You need to sign in to do that
Don't have an account?
Ian Weller
QuoteLineItem and OpportunityLineItem Test
I am having issues testing the one trigger I have. My problem is that in my test class when I insert a QuoteLineItem or OpportunityLineItem object the Product2Id fields are NULL. Well that's a problem because the trigger I developed uses that as a key between the two fields.
Here's the code for example.
Here's the code for example.
trigger MultTransfer on QuoteLineItem (before insert) { //trigger for linking multiplier from OpportunityLineItem to QuoteLineItem //public Map<String, QuoteLineItem> quoteMap = new Map<String, QuoteLineItem>(); public Map<Id, QuoteLineItem> quoteMap = new Map<Id, QuoteLineItem>(); String quoteID,oppID; for(QuoteLineItem qli: Trigger.new){ //dumping all the QuoteLineItems into the map, also grabbing their quote ID quoteMap.put(qli.Product2Id,qli); quoteID=qli.QuoteId; } //creating a Quote object to find the OpportunityId public Quote q =[SELECT OpportunityId FROM Quote WHERE Id=:quoteID]; oppID=q.OpportunityId; public OpportunityLineItem[] oli = [SELECT Product2Id, Multiplier__c, UnitPrice FROM OpportunityLineItem WHERE OpportunityId=:oppID]; Integer i=0; for(i=0;i<oli.size();i++){ if(quoteMap.containsKey(oli[i].Product2Id)){ if(quoteMap.get(oli[i].Product2Id).UnitPrice==oli[i].UnitPrice){//Extra hoop incase multiple quotes with different multipliers quoteMap.get(oli[i].Product2Id).QuoteMultiplier__c=oli[i].Multiplier__c;//setting the new quote multiplier try{ update quoteMap.get(oli[i].Product2Id); }catch(Exception e){ //You done goofed } } } } }
@istest(SeeAllData=true) public class TestMultTransfer{ static testmethod void test(){ String opportunityName = 'My Opportunity'; String standardPriceBookId = ''; PriceBook2 pb2Standard = [select Id from Pricebook2 where isStandard=true]; standardPriceBookId = pb2Standard.Id; Opportunity o = new Opportunity(AccountId=null, Name=opportunityName, StageName='Prospecting', CloseDate=Date.today()); insert o; Opportunity opp = [SELECT Name FROM Opportunity WHERE Id = :o.Id]; opp.Name=opportunityName; update opp; System.assertEquals(opportunityName, opp.Name); Product2 p2 = new Product2(Name='Test Product',isActive=true); insert p2; Product2 p2ex = [SELECT Name FROM Product2 WHERE Id = :p2.Id]; System.assertEquals('Test Product', p2ex.Name); PricebookEntry pbe = new PricebookEntry(Pricebook2Id=standardPriceBookId, UnitPrice=322, isActive=true, Product2=p2, Product2Id=p2.Id); insert pbe; PricebookEntry pbeex = [SELECT Pricebook2Id FROM PricebookEntry WHERE Id = :pbe.Id]; System.assertEquals(standardPriceBookId, pbeex.Pricebook2Id); OpportunityLineItem oli = new OpportunityLineItem(PriceBookEntryId=pbe.Id, OpportunityId=o.Id, Quantity=1, Opportunity=o, Multiplier__c=0.50, UnitPrice=pbe.UnitPrice, PricebookEntry=pbe); insert oli; //system.assertEquals(p2.Id,oli.Product2Id); OpportunityLineItem oliex = [SELECT PriceBookEntryId FROM OpportunityLineItem WHERE Id = :oli.Id]; System.assertEquals(pbe.Id, oliex.PriceBookEntryId); Quote q = new Quote( Name='MyQuote', opportunityid=o.id, pricebook2id=pb2Standard.Id); insert q; QuoteLineItem qli = new QuoteLineItem( PricebookEntry=pbe, quoteid=q.id, PricebookEntryId=pbe.Id, Quantity=oli.Quantity, UnitPrice=oli.UnitPrice, QuoteMultiplier__c=1.00); insert qli; system.assertEquals(p2.Id,qli.Product2Id); //System.assertEquals(oli.Multiplier__c, qli.QuoteMultiplier__c); } }
Your test class could fail when you are inserting the PriceBookEntry record. Remove Product2=p2 when creating the PriceBookEntry and try again.
Thanks,
Sai