0

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);
}
  • You have to write a save method that iterates over the wrapper to grab the values and update as needed. What are you still using inputtext and not input field? – Eric Sep 15 '16 at 15:24
  • sorry copy and paste, i am using inputField, will update the post – Nick Kahn Sep 15 '16 at 15:25
  • i would use WrapperClassList in the controller? – Nick Kahn Sep 15 '16 at 15:27
  • You already have it, it is called products. So your save method would iterate of it and grab the applicable values – Eric Sep 15 '16 at 15:29
  • its not holding the latest data in the products wrapperclass and i have system.debug(products) i see the quantity null, even though i have entered value and as soon as i hit the save button page refresh and clear the values from inputfield – Nick Kahn Sep 15 '16 at 15:44
1

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>

  • let me try and get back to you. – Nick Kahn Sep 15 '16 at 16:02
  • Eric, I have implement your code and test it and as soon as i click the save button the pageblockTable disappears – Nick Kahn Sep 15 '16 at 17:02
  • One thing at a time. Does the debug output the values at you expect? The code I have would not cause the PBT to disappear – Eric Sep 15 '16 at 17:04
  • nope, its empty – Nick Kahn Sep 15 '16 at 17:04
  • @NickKahn - See updated code. I used OLI by you can replace with your custom object – Eric Sep 15 '16 at 17:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.