[SalesForce] Passing inputField value from apex:pageBlockTable

EDIT:
Create products:

List<wrapperClass> WrapperClassList = new List<wrapperClass>();
public List<wrapperClass> getProducts() 
{
    List<Product2> products = new List<Product2>();

    for(product2 p : [select id,name,... from product2]) {
       Product2 pro = (Product2)p;
       wrapperClass wc = new wrapperClass(pro);            
       WrapperClassList.add(wc);
    }
    return WrapperClassList;
}

Save button:

public PageReference save() 
{   
       try {
           system.debug(WrapperClassList);     
       } 
        catch(Exception e){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, e.getMessage()));
            return null;
           }
   return null;
}

END:

I've PageBlockTable that allows users to enter multiple values at one time. I'm not sure how to pass the entered value when the user click on save button. In other words how can I pass the Input field value from Visualforce page to apex controller class.

Wrapper class:

public wrapperClass 
{    
  public Product2 product {get;set;}
  public Asset_Line_Item__c ali {get;set;}

  public wrapperClass(Product2 p){
      product = p;
      ali = New Asset_Line_Item__c();
  }
}

Visualforce page:

<apex:pageBlock>
  <apex:pageBlockTable value="{!products}" var="a">                     
          <apex:column headerValue="quantity" >                            
                <apex:inputField value="{!a.ali.qty}" required="true" />                    
          </apex:column>
  </apex:pageBlockTable>
</apex:pageBlock> 

Apex Code:

this is how i'm updating the wrapperClass

for(product2 p : [select id,name,... from product2]) {
   Product2 pro = (Product2)p;
   wrapperClass wc = new wrapperClass(pro);            
   WrapperClassList.add(wc);
}

Best Answer

I would start by modifying your code as follows:

Minimal Reproducible Example

Class

    public class myExample {

public List<wrapperClass> products{
    get {
        if(products == null){
            products = new List< wrapperClass >();

            for(product2 p : [select id,name from product2 limit 4]) {
                products.add(New wrapperClass(p));
            }
        }
        return products;
    }
    set;
}


    public PageReference save() {
        try {
            for (wrapperClass w : products) {
                system.debug('The Quantity is: ' + w.ali.Quantity);

            }
        } catch (Exception e) {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, e.getMessage()));
            return null;
        }
        return null;
    }

    public class wrapperClass {
        public Product2 product { get; set; }
        public opportunitylineitem ali { get; set; }

        public wrapperClass(Product2 p) {
            product = p;
            ali = New opportunitylineitem();
        }
    }



    }

VFP

<apex:form id="editForm">

    <apex:pageBlock>
        <apex:pageBlockTable value="{!products}" var="a">
            <apex:column headerValue="quantity">
                <apex:inputField value="{!a.ali.Quantity}" required="true"/>
            </apex:column>
        </apex:pageBlockTable>
    </apex:pageBlock>

    <apex:commandbutton value="save me" action="{!save}" rerender=""/>

</apex:form>

Related Topic