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
yarramyarram 

Urgent-"For Loop" code coverage on Apex Test class

HI all,

how to do for loop code coverage? for bleow code............

public Incoice__c invoice;
public List<QuoteLineItem> QLItemsList;
public List<QuoteLineItem> getQLItems()
    {
        QLItemsList=[Select id,TotalPrice,Qty_Sent__c,Comments__c,Subtotal,Product2.Name,Product2.ProductCode,Product2.Id,Quote.Name,
                        Quote.Id,UnitPrice,Quantity,ListPrice,LineNumber,Description,Discount from QuoteLineItem where QuoteId=:currentQuoteID ];
        return QLItemsList;
    }
public PageReference onSave()
    {
                invoice =new Invoice__c();
               
                invoice.Boxes_Shipped__c=boxesShipped;
                invoice.CRF=custRefNo;               
                        try{
                            insert invoice;
                        }
                        catch(exception e){
                            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Please Check' + e.getMessage()));
                            return null;
                        }
               
                for(QuoteLineItem qlItem:QLItemsList) {  //from here not coverd                  
                    InvoiceLine_Items__c invLine=new InvoiceLine_Items__c();
                        invLine.Incoice__c=invoice.Id;
                        invLine.Description__c=qlItem.Product2.Name;
                        invLine.Product__c=qlItem.Product2.Id;
                        invLine.Product_Code__c=qlItem.Product2.ProductCode;
                        invLine.Qty_Ordered__c=qlItem.Quantity;
                        invLine.Qty_Sent__c=qlItem.Qty_Sent__c;
                        invLine.Unit_Price__c=qlItem.UnitPrice;
                        invLine.Comments__c=qlItem.Comments__c;
                        try{
                            Insert invLine;
                        }
                        catch(exception e){
                            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Please Check' + e.getMessage()));
                            return null;
                        }
                }
//upto here not coverd
               PageReference detailsPage=new PageReference('/apex/invoiceDetails?id='+newDelDktID);
                        detailsPage.setRedirect(true);
                        return detailsPage;
    }



please help me on this .......this is Urgent
Vi$hVi$h
That part is not covered because there is nothing in QLItemsList.
The query for QLItemsList is returning nothing.
Also in the for loop there is an insert statement, it is not a good practice.
You might face governor limit issues.
thsrthsr
you can modify code as this way and try again.good luck!
for(QuoteLineItem qlItem:getQLItems()) 
Vi$hVi$h
Can you provide the test class, so that I can see what is missing ...
Vi$hVi$h
I dont know much about opportunity and products but what I can understand from your test class is that QuoteLineItem is not inserted from here.
In your class QLItemsList is fetched whose quote Id is currentQuoteID.
currentQuoteID is hardcoded (Ideally not to be done)
QuoteLineItem needs to be inserted with quoteID= currentQuoteID.