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
mauricio.ramos@corpitalmauricio.ramos@corpital 

Dynamic VF Table: Invalid field for SObject Product2

Hello all,

 

I am trying to create a VF page that will list products (product2) in a table BUT I want to be able to select cetain rows. For this I am using a wrapper class to create the Product with a selected boolean. The problem is that I need to create a table where the fields shown are defined in a Product field set. I have been able to get the controller to load all the products I need and also to create the wrapper products (wProds) BUT I am not sure how to build the table, can someone assist??? Below is my best shot at it.....

 

Upon saving the page I get this error:

 
Error: Invalid field for SObject Product2

 

 

I know the error is in this line: 

 

<apex:outputText value="{!w.Product.[f.fieldPath]}"/>

 

But not sure what im doing wrong OR how to fix it.

 

VF PAGE:

<apex:page controller="opportunityList2Con" tabStyle="Opportunity" >
    <apex:form >
        <apex:pageBlock tabStyle="Opportunity"  >
        <apex:sectionHeader subtitle="{!Opp.Name}" title="Add products to "  />
        <apex:pageblocksection title="Opportunity Line Items" >
        </apex:pageblocksection>
        
        <apex:pageblocksection title="Select Products" columns="1" >
          
          <apex:pageBlockTable value="{!wProds}" var="w">
              <apex:column headerValue="Selected" width="30px;">
                  <apex:inputCheckbox value="{!w.selected}"/>
              </apex:column>
              <apex:repeat value="{!fields}" var="f">
                  <apex:column >
                     <!-- <apex:outputText value="{!w.Product.[f.fieldPath]}"/> COMMENTED OUT SINCE IT WILL NOT SAVE AS IS-->
                  </apex:column>
              </apex:repeat> 
               
          </apex:pageBlockTable>
        </apex:pageblocksection>
        
        </apex:pageBlock>
    </apex:form>
</apex:page>

 

 

 

 CONTROLLER: 

 

public class opportunityList2Con {

//Product and Oli Field Names Map
//Map<String, Schema.SObjectField> prodFields = Schema.SObjectType.Product2.fields.getMap();
Map<String, Schema.SObjectField> OliFields = Schema.SObjectType.OpportunityLineItem.fields.getMap();
Map<String, Schema.SObjectField> PbEntryFields = Schema.SObjectType.PriceBookEntry.fields.getMap();

public Opportunity Opp {get;set;}
private List<PricebookEntry> lstPBEs {get;set;}
private set<id> setProdIds {get;set;}
public List<Product2> lstProducts {get;set;}
private String oId;
public List<wProd> wProds {get;set;}

public opportunityList2Con () {
    
    oId = ApexPages.currentPage().getParameters().get('oId');
    //get Opp Id and Pricebook2Id
    Opp = [Select id, Name, Pricebook2Id, CloseDate From Opportunity where id = :oId LIMIT 1];
    
    //Get all Pricebookentries available for Opportuniy Pricebook Id:
    lstPBEs = [Select IsActive, Product2Id, Pricebook2Id, Id From PricebookEntry where isActive = true and PriceBook2Id = :Opp.Pricebook2Id];
    
    // Get all products using dynamic query:
    setProdIds = new set<id>();
    for(PricebookEntry pb:lstPBEs) { this.setProdIds.add(pb.Product2Id); }
    lstProducts = new List<Product2>();
    lstProducts = this.loadProducts();
    buildWProds();
    system.debug('###wProds: ' + wProds);
     
}

    public List<Schema.FieldSetMember> getFields() { return SObjectType.Product2.FieldSets.ALL_FIELDS.getFields(); }

    private List<Product2> loadProducts() {
        String query;
        String fieldQuery = '';
        String fieldsStr = '';
        for(Schema.FieldSetMember f :SObjectType.Product2.FieldSets.ALL_FIELDS.getFields()) {
            fieldsStr += f.getFieldPath() + ',';
        }
        fieldsStr.trim();
        Integer len = fieldsStr.length();
        fieldQuery= fieldsStr.left(len-1);
        query = 'SELECT ' + fieldQuery + ' FROM Product2 WHERE IsActive = true AND id in:setProdIds';
        lstProducts = Database.query(query);
        return lstProducts;
    }
    
    private void buildWProds() {
        wProds = new List<wProd>();
        for(Product2 p:lstProducts) {
            wProd wp = new wProd(p);
            wProds.add(wp);
        }
    }
    
    //Product wrapper class
    public class wProd {
        public Boolean selected {get;set;}
        public Product2 Product {get;set;}
        
        public wProd (Product2 pProd) {
            selected = false;
            Product = pProd;
        }
    }
        
 
    
}

 

 

 

 

Kind regards,

Mauricio