You need to sign in to do that
Don't have an account?

Help with Controller Extension test method
Hi All,
I have been given this great code; however I am not a developer and having difficulties on writing the test code.
I have googled and search many test codes, but am lost and stuck. Any help would be much appreciated.
Thank you in advance!
Apex Class
VF Page
I have been given this great code; however I am not a developer and having difficulties on writing the test code.
I have googled and search many test codes, but am lost and stuck. Any help would be much appreciated.
Thank you in advance!
Apex Class
01 <apex:page standardController="Case" extensions="OppProductsOnCaseController" title="Related Opportunity Products"> 02 <apex:form id="oppProdForm"> 03 <apex:pageBlock id="prodPB" title="Opportunity Product Details"> 04 <apex:pageBlockTable id="pbTable" value="{!oppLineItem}" var="oli"> 05 <apex:column headerValue="Product Name" value="{!oli.PriceBookEntry.Product2.Name}" /> 06 <apex:column headerValue="Quantity" value="{!oli.Quantity}"/> 07 </apex:pageBlockTable> 08 </apex:pageBlock> 09 </apex:form> 10 </apex:page>
VF Page
01 public class OppProductsOnCaseController{ 02 03 // declatre variables to hold the current case record 04 public Case inContextCase {get; set;} 05 // decalre variable to hold the list of oli fields for the related opportunity 06 public List<OpportunityLineItem> oppLineItem {get; set;} 07 08 // constructor for the class 09 public OppProductsOnCaseController(ApexPages.StandardController stdCont){ 10 // instantiate the variables decalred 11 inContextCase = new Case(); 12 oppLineItem = new List<OpportunityLineItem>(); 13 14 // get the in context record from the passed in argument in the constructor 15 inContextCase = (Case) stdCont.getRecord(); 16 // query the necessary fields from the case object like the related opportunity field value 17 inContextCase = [SELECT Id, Related_Opportunity__c FROM Case WHERE Id = :inContextCase.Id]; 18 19 // if the case is related to an opportunity then using the opportunity id query and fetch the opportunity line items 20 if(inContextCase.Related_Opportunity__c != null){ 21 // add or delete fields from the query as required 22 oppLineItem = [SELECT Id, ListPrice, OpportunityId, PriceBookEntryId, PriceBookEntry.Product2.Name, Quantity, UnitPrice, TotalPrice FROM OpportunityLineItem 23 WHERE OpportunityId = :inContextCase.Related_Opportunity__c]; 24 if(oppLineItem != null && oppLineItem.size() > 0){ 25 // do further operations with opp line items if required. 26 // if not then delete the if condition 27 } 28 } 29 30 } 31 }
All Answers
This is Great, Thank you!
I'm getting errors on the pricebook ID, System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: PricebookEntryId, unknown (versions 3.0 and higher must specify pricebook entry id, others must specify product id): [PricebookEntryId, unknown]
I tried adding PricebookEntryID = pb.ID after the unit price line item but still getting errors. Variable does not exist: pb.ID
Any advice on the price book ID?
Thank you again for your help.
I got it working, Thank you!