+ Start a Discussion
Sami ShakithSami Shakith 

How to get multiple variable for inputfield within pageBlockTable

Hi,

I am trying to create a VF page which calculates the NetAmount based on Buying Price and Discount in listed record. 

My VF page is 
 
<apex:page StandardController="Opportunity" extensions="EQuoteController">
  <apex:form > 
      <apex:pageBlock >
          <apex:pageBlockSection columns="1">
              <apex:outputField label="User Name" value="{!opportunity.Name}"/>
              <apex:outputField label=" Account Name" value="{!Opportunity.AccountId}"/>
              <apex:outputField label="Email Id" value="{!Opportunity.Email__c}"/>
              <apex:outputText Label="Net Amount" Value="{!NetAmount}"/>
          </apex:pageBlockSection>
          
           Related price details for {!Opportunity.name}:    
          <apex:pageblockSection title="Price Details" collapsible="false" columns="2">
                <apex:pageBlockTable value="{!selectedlist}" var="price" id="table2" title="Selected Price Details">
                             
                   <apex:column value="{!price.Name}" headerValue="Name"/>
                   <apex:column value="{!price.Buying_Price__c}" headerValue="Buying Price"/>
                   <apex:column headerValue="Discount">
                   <apex:inputText value="{!Discount}"/>
                   </apex:column>
                   <apex:column headerValue="Action">
                   <apex:commandButton value="ok" action="{!OkDiscount}"/>
                   </apex:column>
                </apex:pageBlockTable>
        </apex:pageBlockSection>
      </apex:pageBlock>
  </apex:form>
         
          
</apex:page>

Ok button method is like that
 
public void OkDiscount(){
price = new price_details__c();
    DisAmount=0;
    NetAmount=0;
    selectedList = new List<Price_Details__c>();
    for(wrapPrice wrapPriceObj : wrapPriceList) {
            if(wrapPriceObj.selected == true) {
            selectedList.add(wrapPriceObj.pds);
            pid=wrapPriceObj.pds.id;
            price=[select id, name, Buying_price__c from Price_details__c where id=:pid];
            NetAmount=NetAmount+price.Buying_price__c;
            DisAmount=(Discount*price.Buying_price__c)/100;
            NetAmount=NetAmount-DisAmount;
            }
    }
}

I am using Wrapper class for list the records.
That VF page will display the details of the opportunity and Net amount for the listed record below. I gave PageBlockTable for getting records and also I include a textfield to get discount amount. Here i gave single variable for the textfield. If only one record is listed means it works fine. But for multiple records it will take only the last value for Net amount calculation. I am getting multiple values using single variable name. I dont know how to get the multiple variables. I need someone help. Suggestion please.
Best Answer chosen by Sami Shakith
William TranWilliam Tran
Sami,

You already stated what your problem is, you have one discount per item yet you only have one discount variable.

The best solution is to add discount to your Price object (or even the wrapPriceList object if you like).

Bind the discount in the VF page to the Price.discount.

Then in your formula

Change DisAmount=(Discount*price.Buying_price__c)/100;

to

DisAmount=(price.Discount*price.Buying_price__c)/100;

then you should be set.

Thx

By the way, you should post your VF code so I can see what is the discount bound to now,
I am guessing all of it is bound to {!Discount} via inputtext tag.

It looks like you are using Pageblock with Pageblocktable and apex:column to show your 3 columns iterating through the Price_details__c object in the Pageblocktable.
 

All Answers

Robert_StrunkRobert_Strunk
Hi Sami, 
   How are you setting the wrapPrice.selected member?  I do not see an input column for that in your Price Details pageBlockTable but your OkDiscount() action method is checking for it.  Unless you have some other methods setting that value to true then I do not see any way this code would ever output the desired results.  I think it would be best for you to post the full controller code, the full wrapper class code as well as any page code you may have taken out before posting this.  
William TranWilliam Tran
Sami,

You need to post the whole code if you want help.

extensions="EQuoteController"? where's the code?

wrapPriceList --> where is this populated?

I still am not sure what you are asking for? What is the issue?  What are you getting? vs What are you expecting?

Screen prints or values will be helpful

thx.
Sami ShakithSami Shakith
Thanku for your replies Robert and william. Here is my full controler class
public class EQuoteController{
public Integer Quantity{get;set;}
public Decimal Discount{get;set;}
public Decimal DisAmount{get;set;}
public Boolean OkDiscount{get;set;}
public Boolean GoToCart{get;set;}
public Boolean HyperLink{get;set;}
public string pid{get;set;}
public Boolean region{get;set;}
public Boolean typef{get;set;}
public Boolean table{get;set;}
public String typeSelected{get;set;}
public decimal NetAmount{get;set;}
public price_details__c price{get;set;}
public Price_details__c SearchCriteria{get;set;}
public List<Price_Details__c> selectedList{get;set;}
public List<wrapPrice> wrapPriceList{get;set;}
public opportunity opp{get;set;}
public String cid = ApexPages.currentPage().getParameters().get('id');
public EQuoteController(ApexPages.StandardController controller) 
 { 
   SearchCriteria = new Price_details__c();
   HyperLink=false; 
 } 
public void go(){

region = true;
typef = true;
table = true;

            wrapPriceList = new List<wrapPrice>();
            
                    if(SearchCriteria.Operating_Model__c!=null && Quantity!=null && typeSelected==null && SearchCriteria.Region__c==null){
                          for(Price_Details__c pd: [SELECT Id, Name, Region__c, MSRP__c, Operating_Model__c, Buying_Price__c, QuantityStart__c,QuantityEnd__c,Type__c 
                            FROM Price_details__c WHERE Operating_Model__c =: SearchCriteria.Operating_Model__c and QuantityStart__c<=:Quantity 
                            and QuantityEnd__c>=:Quantity]){
                        wrapPriceList.add(new wrapPrice(pd));
                    }    
                    if(wrapPriceList.size() == 0) 
                    { 
                        Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,''+'No records to Display'));  
                    }
                    
                     
                     }          
HyperLink=true;

}
public pageReference GoToCart(){
    NetAmount=0;
    selectedList = new List<Price_Details__c>();
    for(wrapPrice wrapPriceObj : wrapPriceList) {
            if(wrapPriceObj.selected == true) {
            selectedList.add(wrapPriceObj.pds);
            NetAmount=NetAmount+wrapPriceObj.pds.Buying_price__c;
            }
    }
    pageReference ref;
    ref = new pagereference('/apex/EsCart?id='+cid);
    //ref.setredirect(true);
    return ref; 
}
public void OkDiscount(){
price = new price_details__c();
    DisAmount=0;
    NetAmount=0;
    selectedList = new List<Price_Details__c>();
    for(wrapPrice wrapPriceObj : wrapPriceList) {
            if(wrapPriceObj.selected == true) {
            selectedList.add(wrapPriceObj.pds);
            pid=wrapPriceObj.pds.id;
            price=[select id, name, Buying_price__c from Price_details__c where id=:pid];
            NetAmount=NetAmount+price.Buying_price__c;
            DisAmount=(Discount*price.Buying_price__c)/100;
            NetAmount=NetAmount-DisAmount;
           }
    }
}
public class wrapPrice {
        public Price_Details__c pds {get; set;}
        public Boolean selected {get; set;}
 
        public wrapPrice(Price_Details__c pd) {
            pds = pd;
            selected = false;
        }
}
}

Please tell a solution.
Sami ShakithSami Shakith
Here im attaching a capture of the VF page. Price details is a custom object.Records of Price details are listed below the price details table. Initially i am calculating the Net amount by adding all the buying price of listed record. After entering the dicount for each record the net amount will be changed based on it. I am giving varies value for discount field in each record. Here i am giving 10 for 1st record so that the discount will be 220 for 1st record, for the second record the discount will be 50. So after clicking ok button these discount values should be deducted from the Net amount. But my controller takes only the last discount value to calculating the discount amount for all record. In this case it takes 5 for both the record. Because i am getting all the discount values with single variable.So it is taking the final value(i.e 5) to calculating discount for each record. I dont know how to get all the variable. please answer me.

User-added image
 
William TranWilliam Tran
Sami,

You already stated what your problem is, you have one discount per item yet you only have one discount variable.

The best solution is to add discount to your Price object (or even the wrapPriceList object if you like).

Bind the discount in the VF page to the Price.discount.

Then in your formula

Change DisAmount=(Discount*price.Buying_price__c)/100;

to

DisAmount=(price.Discount*price.Buying_price__c)/100;

then you should be set.

Thx

By the way, you should post your VF code so I can see what is the discount bound to now,
I am guessing all of it is bound to {!Discount} via inputtext tag.

It looks like you are using Pageblock with Pageblocktable and apex:column to show your 3 columns iterating through the Price_details__c object in the Pageblocktable.
 
This was selected as the best answer
Sami ShakithSami Shakith

Hi william,

Thanks for your reply
You are rite. But i want to get the discount values without creating any field for it. Should i give any iterating loop for it? if it is means how can i?
Robert_StrunkRobert_Strunk
Hi Sami, 
   What William said is spot on.  Also, based on your code as well as your screenshot there is no reason to use a wrapper class.  If you just add the discount field to your price details object then a lot of issues can be resolved here and you wouldn't even need code to get this done.
  1. Create discount field on price details object.
  2. Create rollup summary field on opportunity to SUM the buying price.
  3. Create rollup summary field on opportunity to SUM the discounts.
  4. Create a formula field on the opportunity to calculate the net amount based on the total discount and the total buying price. 
Sami ShakithSami Shakith
Hi Robert,

Is it not possible to calculate a discout without creating a field for discount? Because i dont want to create a field. Actually im redirecting this page from another VF page. So that only i used wraper class.
Robert_StrunkRobert_Strunk
Yes it is absolutely possible to calculate a discount using a wrapper class but why would you not want to store the discounted value with a field?  The discount being applied will only persist during the page's lifecyle and without using a bit of data analysis your admins/stakeholders will not have immediate transparency into who gave a discount and the amount of discount they gave.

If you do not want a new field then you must add a new value to your inner wrapper class. 
public class wrapPrice {
	
	public Price_Details__c pds			{get; set;}
	public Boolean 			selected 	{get; set;}
	public Decimal 			discount 	{get; set;}

	public wrapPrice(Price_Details__c pd) {
		pds = pd;
		selected = false;
		discount = 0;
	}
}

Once you have that updated you just need to change your selectedList collection to be a List<wrapPrice> and have your pageblock iterate over that.  

Now, just update your apex column to point to the wrapPrice.discount instead of the discount member.  

Once that is done you just need to tweak your OkDiscount() method to sum the wrapPrice.discount values and then use that to calculate your netAmount value.  


 
William TranWilliam Tran
Sami,  as I stated:

The best solution is to add discount to your Price object (or even the wrapPriceList object if you like).

but if you don't want change the price object then you can bind your VF page to the wrapper object like Robert showed you above.

Thx
William TranWilliam Tran
Sami,  let us know if you still have questions.

Otherwise, as a common practice, if your question is answered, please choose 1 best answer. 
But you can give every answer a thumb up if that answer is helpful to you. 

This will help keep the forum clean and help future users determine what answers are useful
and what answer was the best in resolving the user's issue. 

Thanks
Sami ShakithSami Shakith
Wiliam I'm still have issues in that. I created a discount field in price detail object and also created one formula field for calculating discount amount. Now its working fine. But after clicking the ok button to recalculate the net amount the list showing duplicate records. Why i'm getting like this? can you please tell me ?

Controller class is here
public class EQuoteController{
public Integer Quantity{get;set;}
public Boolean OkDiscount{get;set;}
public Boolean GoToCart{get;set;}
public Boolean HyperLink{get;set;}
public string pid{get;set;}
public Boolean region{get;set;}
public Boolean typef{get;set;}
public Boolean table{get;set;}
public String typeSelected{get;set;}
public decimal NetAmount{get;set;}
public price_details__c price{get;set;}
public Price_details__c SearchCriteria{get;set;}
public List<Price_Details__c> selectedList{get;set;}
public List<wrapPrice> wrapPriceList{get;set;}
public opportunity opp{get;set;}
public String cid = ApexPages.currentPage().getParameters().get('id');
public EQuoteController(ApexPages.StandardController controller) 
 { 
   SearchCriteria = new Price_details__c();
   HyperLink=false; 
 } 
public void go(){

region = true;
typef = true;
table = true;

            wrapPriceList = new List<wrapPrice>();
            
                    if(SearchCriteria.Operating_Model__c!=null && Quantity!=null && typeSelected==null && SearchCriteria.Region__c==null){
                          for(Price_Details__c pd: [SELECT Id, Name, Region__c, Discount__c, MSRP__c, Operating_Model__c, Buying_Price__c, QuantityStart__c,QuantityEnd__c,Type__c 
                            FROM Price_details__c WHERE Operating_Model__c =: SearchCriteria.Operating_Model__c and QuantityStart__c<=:Quantity 
                            and QuantityEnd__c>=:Quantity]){
                        wrapPriceList.add(new wrapPrice(pd));
                    }    
                    if(wrapPriceList.size() == 0) 
                    { 
                        Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,''+'No records to Display'));  
                    }
                    
                     
                     }          
HyperLink=true;

}
public pageReference GoToCart(){
    NetAmount=0;
    selectedList = new List<Price_Details__c>();
    for(wrapPrice wrapPriceObj : wrapPriceList) {
            if(wrapPriceObj.selected == true) {
            selectedList.add(wrapPriceObj.pds);
            NetAmount=NetAmount+wrapPriceObj.pds.Buying_price__c;
            }
    }
    pageReference ref;
    ref = new pagereference('/apex/EsCart?id='+cid);
    //ref.setredirect(true);
    return ref; 
}
public void OkDiscount(){
price = new price_details__c();
    NetAmount=0;
    for(wrapPrice wrapPriceObj : wrapPriceList){
        if(wrapPriceObj.selected == true) {
            selectedList.add(wrapPriceObj.pds);
            pid=wrapPriceObj.pds.id;
            price=[select id, name, Buying_price__c, Discount__c, DiscountAmount__c from Price_details__c where id=:pid];
            price.Discount__c=wrapPriceObj.pds.Discount__c;
            update price;
            NetAmount=NetAmount+price.DiscountAmount__c;
            }
    }
}
public class wrapPrice {
        public Price_Details__c pds {get; set;}
        public Boolean selected {get; set;}
 
        public wrapPrice(Price_Details__c pd) {
            pds = pd;
            selected = false;
        }
}
}

Thanks