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
MayankDkPantMayankDkPant 

SObject row was retrieved via SOQL without querying the requested field

Hi,

 

I have writtern an Extension class for opportunity visual force page.

I have used 4 custom picklist  fields of opportunity on vf page, fields names are Product_Group__c,Product_Area__c,Product_Family__c,Product_Family_Child__c.

 

My problem is when I am running the test class is throwing an exception

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

 

Class.opportunityProductEntryExtension.updateAvailableList: line 180, column 1 Class.opportunityProductEntryExtension.<init>: line 69, column 1 Class.productentry_test.mytest: line 73, column 1 [productentry_test. is my test class].

 

 

public opportunityProductEntryExtension(ApexPages.StandardController controller) 
    {
        objOpp= (Opportunity)controller.getRecord();
        
        // Need to know if org has multiple currencies enabled
        multipleCurrencies = UserInfo.isMultiCurrencyOrganization();

        // Get information about the Opportunity being worked on
        if(multipleCurrencies)
            theOpp = database.query('select Id, Product_Group__c, Product_Area__c, Product_Family__c, Product_Family_Child__c, Pricebook2Id, Pricebook2.Name, CurrencyIsoCode from Opportunity where Id = \'' + controller.getRecord().Id + '\' limit 1');
        else
            theOpp = [select Id,Product_Group__c, Product_Area__c, Product_Family__c, Product_Family_Child__c, Pricebook2Id, PriceBook2.Name from Opportunity where Id = :controller.getRecord().Id limit 1];
        
        // If products were previously selected need to put them in the "selected products" section to start with
        shoppingCart = [select Id,Product_Group__c,Product_Area__c,Product_families__c,Product_Child__c, Quantity, TotalPrice, UnitPrice, Description, PriceBookEntryId, PriceBookEntry.Name, PriceBookEntry.IsActive, PriceBookEntry.Product2Id, PriceBookEntry.Product2.Name, PriceBookEntry.PriceBook2Id from opportunityLineItem where OpportunityId=:theOpp.Id];

        // Check if Product hierarchy picklist is changed
        
        
        // Check if Opp has a pricebook associated yet
        if(theOpp.Pricebook2Id == null){
            Pricebook2[] activepbs = [select Id, Name from Pricebook2 where isActive = true limit 2];
            if(activepbs.size() == 2){
                forcePricebookSelection = true;
                theBook = new Pricebook2();
            }
            else{
                theBook = activepbs[0];
            }
        }
        else{
            theBook = theOpp.Pricebook2;
        }
        
        if(!forcePricebookSelection)
            updateAvailableList();
    }
 


public void updateAvailableList() 
    {
        //objOpp= (Opportunity)controller.getRecord();
        String qString;
        // We dynamically build a query string and exclude items already in the shopping cart
        
        String idOpp=System.currentPageReference().getParameters().get('id'); 
        Opportunity oppList=[SELECT Product_Group__c, Product_Area__c, Product_Family__c, 
                 Product_Family_Child__c FROM opportunity 
                 where id=:idOpp limit 1];
                 
       String sProductGroup =oppList.Product_Group__c;
       String sProductArea = oppList.Product_Area__c;
       String sProductFamily = oppList.Product_Family__c;
       String sProductChild = oppList.Product_Family_Child__c;
            
       If(oppList.Product_Group__c !=null && oppList.Product_Area__c!='Multiple' && oppList.Product_Family__c!='Multiple' && oppList.Product_Family_Child__c!='Multiple')
        { 
            
            qString = 'select Id, Pricebook2Id, IsActive, Product2.Name, Product2.Family, Product2.IsActive, Product2.Description, UnitPrice from PricebookEntry where IsActive=true and Product2.Product_Group__c= \'' + sProductGroup + '\' and Product2.Product_Area__c= \'' + sProductArea  + '\' and Product2.Product_Family__c= \'' + sProductFamily  + '\' and Product2.Product_Family_Child__c  =\''+ sProductChild+ '\' and Pricebook2Id = \'' + theBook.Id + '\'';
            if(multipleCurrencies)
                qstring += ' and CurrencyIsoCode = \'' + theOpp.get('currencyIsoCode') + '\'';
            
            // note that we are looking for the search string entered by the user in the name OR description
            // modify this to search other fields if desired
            if(searchString!=null)
            {
                qString+= ' and (Product2.Name like \'%' + searchString + '%\' or Product2.productcode like \'%' + searchString + '%\')';
            }
            
            
        }
        
       If(oppList.Product_Area__c!='Multiple' && oppList.Product_Family__c!='Multiple' &&  oppList.Product_Family_Child__c=='Multiple')
       {
           qString = 'select Id, Pricebook2Id, IsActive, Product2.Name, Product2.Family, Product2.IsActive, Product2.Description, UnitPrice from PricebookEntry where IsActive=true and Product2.Product_Group__c= \'' + sProductGroup + '\' and Product2.Product_Area__c= \'' + sProductArea  + '\' and Product2.Product_Family__c= \'' + sProductFamily  + '\' and Pricebook2Id = \'' + theBook.Id + '\'';
            if(multipleCurrencies)
                qstring += ' and CurrencyIsoCode = \'' + theOpp.get('currencyIsoCode') + '\'';
            
            // note that we are looking for the search string entered by the user in the name OR description
            // modify this to search other fields if desired
            if(searchString!=null)
            {
                qString+= ' and (Product2.Name like \'%' + searchString + '%\' or Product2.productcode like \'%' + searchString + '%\')';
            }
       }
       
       if(oppList.Product_Area__c!='Multiple' && oppList.Product_Family__c=='Multiple')
       {
       
           qString = 'select Id, Pricebook2Id, IsActive, Product2.Name, Product2.Family, Product2.IsActive, Product2.Description, UnitPrice from PricebookEntry where IsActive=true and Product2.Product_Group__c= \'' + sProductGroup + '\' and Product2.Product_Area__c= \'' + sProductArea  + '\' and Pricebook2Id = \'' + theBook.Id + '\'';
           if(multipleCurrencies)
                qstring += ' and CurrencyIsoCode = \'' + theOpp.get('currencyIsoCode') + '\'';
            
            // note that we are looking for the search string entered by the user in the name OR description
            // modify this to search other fields if desired
           if(searchString!=null)
           {
               qString+= ' and (Product2.Name like \'%' + searchString + '%\' or Product2.productcode like \'%' + searchString + '%\')';
           }
       }
      if(oppList.Product_Area__c=='Multiple')
      {
          qString = 'select Id, Pricebook2Id, IsActive, Product2.Name, Product2.Family, Product2.IsActive, Product2.Description, UnitPrice from PricebookEntry where IsActive=true and Product2.Product_Group__c= \'' + sProductGroup + '\'and Pricebook2Id = \'' + theBook.Id + '\'';
          if(multipleCurrencies)
                qstring += ' and CurrencyIsoCode = \'' + theOpp.get('currencyIsoCode') + '\'';
            
            // note that we are looking for the search string entered by the user in the name OR description
            // modify this to search other fields if desired
          
         if(searchString!=null)
          {
              qString+= ' and (Product2.Name like \'%' + searchString + '%\' or Product2.productcode  like \'%' + searchString + '%\')';
          }
      }
      
        if (objOpp.Product_Group__c!=null)
         {   
             System.Debug('Product Group:'+ objOpp.Product_Group__c);
             qString+= ' and (Product2.Product_Group__c like \'%' + objOpp.Product_Group__c + '%\')';    
         }
         
         if (objOpp.Product_Area__c!=null)
         {   
             System.Debug('Product Area :'+ objOpp.Product_Area__c);
             qString+= ' and (Product2.Product_Area__c like \'%' + objOpp.Product_Area__c+ '%\')';    
         }
         if (objOpp.Product_Family__c!=null)
         {
             System.Debug('Product Family:'+ objOpp.Product_Family__c);
             qString+= ' and (Product2.Product_Family__c like \'%' + objOpp.Product_Family__c + '%\')';    
         }
         if (objOpp.Product_Family_Child__c!=null) 
         {
             System.Debug('Product Family Child:'+ objOpp.Product_Family_Child__c);
             qString+= ' and (Product2.Product_Family_Child__c  like \'%' + objOpp.Product_Family_Child__c + '%\')';    
         }
               
        Set<Id> selectedEntries = new Set<Id>();
        for(opportunityLineItem d:shoppingCart)
        {
            selectedEntries.add(d.PricebookEntryId);
        }
        
        if(selectedEntries.size()>0){
            String tempFilter = ' and Id not in (';
            for(Id i : selectedEntries){
                tempFilter+= '\'' + (String)i + '\',';
            }
            String extraFilter = tempFilter.substring(0,tempFilter.length()-1);
            extraFilter+= ')';
            
            qString+= extraFilter;
        }
        
        qString+= ' order by Product2.Name';
        qString+= ' limit 101';
        
        system.debug('qString:' +qString);        
        AvailableProducts = database.query(qString);
        
        // We only display up to 100 results... if there are more than we let the user know (see vf page)
        if(AvailableProducts.size()==101){
            AvailableProducts.remove(100);
            overLimit = true;
        }
        else{
            overLimit=false;
        }
    }

Best Answer chosen by Admin (Salesforce Developers) 
MayankDkPantMayankDkPant

I have solved the issue. Instead of declaring object objOpp at beginning of the opportunityProductEntryExtension class controller, I have declared it after :

 

if(multipleCurrencies)
            theOpp = database.query('select Id, Product_Group__c, Product_Area__c, Product_Family__c, Product_Family_Child__c, Pricebook2Id, Pricebook2.Name, CurrencyIsoCode from Opportunity where Id = \'' + controller.getRecord().Id + '\' limit 1');
        else
            theOpp = [select Id,Product_Group__c, Product_Area__c, Product_Family__c, Product_Family_Child__c, Pricebook2Id, PriceBook2.Name from Opportunity where Id = :controller.getRecord().Id limit 1];

 

 objOpp= (Opportunity)theOpp;// this part is solved my problem.

 

Thanks for your response.

 

With Regards,

Mayank Pant

 

 

All Answers

kiranmutturukiranmutturu
if you are using a standard controller in vf page , Note that only the fields that are referenced in the associated Visualforce markup are available for querying on this SObject. All other fields, including fields from any related objects, must be queried using a SOQL expression in your code objOpp.Product_Group__c is coming from standard controller .... r u can do one thing add this to vf page this may resolve your issue
kiranmutturukiranmutturu
apex:outputText value="{!opportunity.product_group__c}" rendered="false"
MayankDkPantMayankDkPant

I have solved the issue. Instead of declaring object objOpp at beginning of the opportunityProductEntryExtension class controller, I have declared it after :

 

if(multipleCurrencies)
            theOpp = database.query('select Id, Product_Group__c, Product_Area__c, Product_Family__c, Product_Family_Child__c, Pricebook2Id, Pricebook2.Name, CurrencyIsoCode from Opportunity where Id = \'' + controller.getRecord().Id + '\' limit 1');
        else
            theOpp = [select Id,Product_Group__c, Product_Area__c, Product_Family__c, Product_Family_Child__c, Pricebook2Id, PriceBook2.Name from Opportunity where Id = :controller.getRecord().Id limit 1];

 

 objOpp= (Opportunity)theOpp;// this part is solved my problem.

 

Thanks for your response.

 

With Regards,

Mayank Pant

 

 

This was selected as the best answer