+ Start a Discussion
Jean Grey 10Jean Grey 10 

SObject row was retrieved via SOQL without querying the requested field: OpportunityLineItem.Opportunity

I am trying to build a simple page to display the Opportunity and associated Line Items. I am getting this error which doesn't make sense because I am including OpportunityId in the OLI query. 

My extension:
public class PrintContract {
    
    private final Opportunity opp;

    public PrintContract(ApexPages.StandardController stdController) {
            stdController.addFields(new String[]{
            'Id',
            'LinkContacttoOpportunity__c',
            'Name',
            'CloseDate',
            'Contract__c',
            'Type',
            'Age__c',
            'Amount',
            'EXW_Expiration_Date__c',
            'EXW_Effective_Date__c'
        });    
        
        //    get current opportunity record
        this.opp = (Opportunity)stdController.getRecord();
    }
        public List<OpportunityLineItem> oli {get;set;}

        public void olis(){
        oli = new List<OpportunityLineItem>([SELECT Id, TotalPrice, Class__c, ServiceDate, OpportunityId, Quantity, Contract__c, Is_Accessory__c, Serial_Number__c
                    FROM OpportunityLineItem 
                       WHERE OpportunityId = :opp.ID]);
           }
}

My Page:
<apex:page standardController="Opportunity" extensions="PrintContract" action="{!olis}">
    <apex:pageBlock rendered="{!Opportunity.HasOpportunityLineItem = TRUE}">
        <apex:pageBlockTable value="{!oli}" var="ol">
            <apex:column headerValue="Plan Number" value="{!ol.Contract__c}" />
            <apex:column headerValue="Serial Number" value="{!ol.Serial_Number__c}" />
            <apex:column headerValue="Purchase Date" value="{!ol.ServiceDate}" />
            <apex:column headerValue="Plan Type" value="{!ol.Opportunity.Type}" />
            <apex:column headerValue="Plan Effective Date" value="{!ol.Opportunity.EXW_Effective_Date__c}" />
            <apex:column headerValue="Plan Expiration Date" value="{!ol.Opportunity.EXW_Expiration_Date__c}" />
            <apex:column headerValue="Plan Cost" value="{!ol.TotalPrice}" />

        </apex:pageBlockTable>
    
    </apex:pageBlock>
</apex:page>
Best Answer chosen by Jean Grey 10
Shashikant SharmaShashikant Sharma
Hi Jean,

You have to Query Opportunity.Type, Opportunity.EXW_Effective_Date__c, Opportunity.EXW_Expiration_Date__c  fields in your query. As you are refering to them on the visualforce page.

Once you will add them in your query it should resolve the issue. Let me know if it still comes.

Thanks
Shashikant

All Answers

Shashikant SharmaShashikant Sharma
Hi Jean,

You have to Query Opportunity.Type, Opportunity.EXW_Effective_Date__c, Opportunity.EXW_Expiration_Date__c  fields in your query. As you are refering to them on the visualforce page.

Once you will add them in your query it should resolve the issue. Let me know if it still comes.

Thanks
Shashikant
This was selected as the best answer
Amit Chaudhary 8Amit Chaudhary 8
Please update your code like below

public class PrintContract {
    
    private final Opportunity opp;

    public PrintContract(ApexPages.StandardController stdController) {
            stdController.addFields(new String[]{
            'Id',
            'LinkContacttoOpportunity__c',
            'Name',
            'CloseDate',
            'Contract__c',
            'Type',
            'Age__c',
            'Amount',
            'EXW_Expiration_Date__c',
            'EXW_Effective_Date__c'
        });    
        
        //    get current opportunity record
        this.opp = (Opportunity)stdController.getRecord();
    }
        public List<OpportunityLineItem> oli {get;set;}

        public void olis(){
        oli = new List<OpportunityLineItem>([SELECT Id, TotalPrice, Class__c, ServiceDate, OpportunityId, Quantity, Contract__c, Is_Accessory__c, Serial_Number__c,Opportunity.Type,Opportunity.EXW_Effective_Date__c,Opportunity.EXW_Expiration_Date__c
                    FROM OpportunityLineItem 
                       WHERE OpportunityId = :opp.ID]);
           }
}


Let us know if this will help you