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
Alex PAlex P 

Test class for Quote Line Item VF component

Trying to test my first component for pulling quote line items into a VF email template. Any help greatly appreciated! 
VF Component:
<apex:component access="global" controller="SortedLineItemsController">
<apex:attribute name="value" type="Quote" assignTo="{!quote}" description="TODO: Describe me"/>

<apex:repeat var="li" value="{!sorted}">
<tr>
<td>{!li.Part_Name__c}</td>
<td>{!li.Technology_Pick__c}</td>
<td>{!li.Rev__c}</td>
<td>{!li.Material__c}</td>
<td>{!li.Description_Summary__c}</td>
<td>${!li.UnitPrice}</td>
<td>{!li.Quantity}</td>
<td>${!li.TotalPrice}</td>
</tr>
</apex:repeat>

<!-- Needed to work around limitation that the standard controller cannot "see"
the linkage between the value attribute and the opportunity property -->
<apex:variable var="oli" value="{!value.QuoteLineItems}" rendered="false">
{!oli.Part_Name__c}
{!oli.Technology_Pick__c}
{!oli.Rev__c}
{!oli.Material__c}
{!oli.Description_Summary__c}
{!oli.UnitPrice}
{!oli.Quantity}
{!oli.TotalPrice}
</apex:variable>
</apex:component
Apex Controller:
public class SortedLineItemsController {
    public Quote quote { get; set; }
    
    public QuoteLineItem[] getSorted() {
        if (quote == null || quote.quoteLineItems== null) {
            return null;
        }
        
        
        QuoteLineItem[] result = new QuoteLineItem[1];
        for (QuoteLineItem item : Quote.quoteLineItems) {
            result.add(0, item);
        }
        
        return result;
   }
}

​​​​​​​
Nalini ch 6Nalini ch 6
Hello 

It looks like our team of experts can help you resolve this ticket. We have Salesforce global help-desk support and you can log a case and our Customer Success Agents will help you solve this issue. You can also speak to them on live chat. Click on the below link to contact our help-desk. Trust me it is a support service that we are offering for free!

https://jbshelpdesk.secure.force.com

Thanks,
Jarvis SFDC team
AbhishekAbhishek (Salesforce Developers) 
Hi Alex,

Follow the suggestions as mentioned in the below blogs,

https://developer.salesforce.com/forums/?id=906F000000092J5IAI

https://developer.salesforce.com/forums/?id=906F000000093D3IAI

https://developer.salesforce.com/forums/?id=9060G000000XZuUQAW

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks.

 
Alex PAlex P
I keep running into this error on my test class- System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_VALUE, This price definition already exists in this price book: []
 
@isTest(SeeAllData=true)
private class TestSortedLineItemsController {
    @isTest static void insertOpp() {
            Test.startTest();            
        Account acc = new Account (name='Acme');
        insert acc;
        
        Contact con = new Contact(FirstName = 'First', LastName = 'Last', AccountId = acc.Id, Email = 'test@mail.com');
        insert con;
       
        Opportunity opp= new Opportunity ();
        opp.name= 'Testopp';
        opp.Accountid= acc.id;
        opp.CloseDate= date.today();
        opp.StageName= 'CAD Received';
        opp.Pricebook2id=Test.getStandardPricebookId();
        insert opp;

        Quote q= new Quote ();
        q.Name= 'Testq';
        q.OpportunityId= Opp.id;       
        q.Pricebook2Id=Test.getStandardPricebookId();      
        insert q;
                
        Pricebook2 pb = new Pricebook2(Name = 'Services', Description = 'Price Book for all services', IsActive = true );
        insert pb;
        Product2 prod = new Product2(Name = 'CNC Machining', IsActive = true);
        nsert prod;
       PricebookEntry pbe=new PricebookEntry(unitprice=0.01,Product2Id=prod.Id, Pricebook2Id=Test.getStandardPricebookId(), IsActive= true); 
    insert pbe;    
         
        QuoteLineItem qli= new QuoteLineItem(Product2Id=prod.id,QuoteId=q.id,PriceBookEntryID=pbe.id,UnitPrice=pbe.UnitPrice,Quantity=4);
        insert qli;
        
        opp.StageName= 'Closed Won';

        SortedLineItemsController slic = new SortedLineItemsController();
        List<QuoteLineItem> slic_q = slic.getSorted();
        QuoteLineItem[] slic_qli = slic.getSorted();
        Test.stopTest();