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
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

 
Rakesh Kumar 335Rakesh Kumar 335

@Amit Chaudhary 8

Error:  SObject row was retrieved via SOQL without querying the requested field:
Can you help me with proper syntax, please? Seems like the field needs to be in the query.

Current code:
 public static void AddOrderNumber(Map<Id, Order__c> oldMap,list<Order__c> issListIn,boolean isnew,boolean isODSvccall) {
       // List<Order__c> issListIn = [select id,Order_Number__c, from Order__c where id in :Ordermap.keyset()];
*The Query is in the commented line. Trying to save it, but unable to do so