0

enter image description here

I am able to pass single value from vf to controller. But here are list of quantity values.

Here is my visualforce code -

<apex:page standardController="Billing__c"  extensions="medicinesController1" sidebar="false">
 <apex:variable value="{!0.00}" var="subtotal"/>

<apex:form >
    <apex:pageBlock >
     <apex:pageMessages />
        <apex:actionStatus id="loadingStatus" startText="Please Wait. Processing..." />
     <apex:pageBlockButtons location="Top">
            <apex:commandButton value="Add Medicines" action="{!processSelected}" rerender="table2" />
     </apex:pageBlockButtons>

        <apex:pageBlockSection >
            <apex:pageBlockSectionItem >
                <apex:outputLabel value="Name" />
                <apex:inputText value="{!name}" />
            </apex:pageBlockSectionItem> 
            <apex:pageBlockSectionItem >
                <apex:commandButton value="Search" action="{!searchMedicines}" reRender="table" />
                <apex:commandButton value="Clear" action="{!clearMedicines}" reRender="table" />
            </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
        
        
        <apex:pageblockSection title="All Medicines" collapsible="false" columns="2">
         
            <!-- Iteration component to loop over the Wrapper Accounts by referencing the property "wrapAccountList" -->
            <apex:pageBlockTable value="{!wrapMedicineList}" var="medWrap" id="table" title="All Medicines">
                <apex:column >  
                
                    <!-- selected property is only 1 level deep while Account Fields are 2 levels deep -->
                    <apex:inputCheckbox value="{!medWrap.selected}" id="inputId"/>
                </apex:column>
                <apex:column value="{!medWrap.med.Name}" />
                <apex:column value="{!medWrap.med.Type__c}" />
                <apex:column value="{!medWrap.med.Values__c}" headerValue="UM" />
                <apex:column value="{!medWrap.med.Expiry_Date__c}" />
                <apex:column value="{!medWrap.med.Selling_Price__c}" />
            </apex:pageBlockTable>          
                   
            <!-- id attribute below helps to rerender the pageblock, referenced via rerendered attribute of CommandButton component -->       
            <apex:pageBlockTable value="{!selectedMedicines}" var="c" id="table2" title="Selected Medicines">
                <apex:column value="{!c.Name}" headerValue="Medicine Name"/>
                <apex:column value="{!c.Type__c}" headerValue="Type"/>
                <apex:column value="{!c.Values__c}" headerValue="UM"/>
          <!--      <apex:column value="{!c.Expiry_Date__c}" headerValue="Expiry Date"/> -->
                <apex:column value="{!c.Selling_Price__c}" headerValue="Selling Price"/>
                 <apex:column headerValue="Quantity">
                   <apex:inputText value="{!quantity}" id="quant" onchange="Multiplication('{!c.Selling_Price__c}','{!$Component.quant}','{!$Component.unitpric}');" />  
                     <apex:facet name="footer"> Total Amount: </apex:facet> 
                 </apex:column>
    
               <apex:column headerValue="Subtotal">
                  <apex:inputText styleClass="pricevalue" id="unitpric" />
                  <apex:facet name="footer">
                    <apex:outputText id="totalPrice" styleClass="outputPrice" />
                  </apex:facet>  
               </apex:column>
            </apex:pageBlockTable>
            
            <apex:inputHidden value="{!total}" id="a_id"/>
        </apex:pageblockSection>
         <br/><br/>
         &nbsp;&nbsp;&nbsp;
        <apex:OutputLabel value="Please Click the appropriate button below to save or cancel the order."> </apex:OutputLabel> <br/><br/>
        &nbsp;&nbsp;&nbsp;
        <apex:commandButton action="{!confirm}" styleClass="buttonStyle" style="vertical-align:left;width:80px;height:20px;background:green;" value="Confirm"/>       
        <apex:commandButton action="{!cancel}" styleClass="buttonStyle" style="vertical-align:left;width:80px;height:20px;background:red;" value="Cancel" />
        <br/><br/>
    </apex:pageBlock>
   
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script>
        $(document).ready(function () {
         $(document).on("focus", ".pricevalue", function (e) {
            var sum = 0;
            $('.pricevalue').each(function(){
                var num = $(this).val();
                if(num != 0 && !isNaN(num)) {
                    sum += parseFloat(num);
                }
            });
            
            $("[id$='a_id']").val(sum);
            $(".outputPrice").text(sum.toFixed(2));
        });
    });
   </script>
   <script type="text/javascript">
   function Multiplication(field1, field2Id, subtotalId) {
         var field2= document.getElementById(field2Id);
         var subtotal= document.getElementById(subtotalId);
         subtotal.value = Number(field1) *  Number(field2.value);
         console.log(field2.value);
         document.getElementById(subtotalId).value = subtotal.value ; 
   }     
</script>
</apex:form>
</apex:page>

Controller code -

public with sharing class medicinesController1{

// Collection of the class/wrapper objects wrapAccount 
public List<wrapMedicine> wrapMedicineList{get; set;} 
public List<Medicine__c> selectedMedicines{get; set;}
public String name { get; set; }
public Integer quantity {get; set;}
public Decimal total { get; set; }
public Id billingId {get;set;}

public medicinesController1(ApexPages.StandardController controller) {

        billingId  = ApexPages.CurrentPage().getparameters().get('id');
        System.debug(billingId);
         if(wrapMedicineList == null) {
        wrapMedicineList = new List<wrapMedicine>();
        for(Medicine__c a: [select Id, Name,Type__c, Values__c, Expiry_Date__c,Selling_Price__c from Medicine__c]) {
            // As each Account is processed, create a new wrapAccount object and add it to the wrapAccountList
            wrapMedicineList.add(new wrapMedicine(a));
        }
    }
    
     selectedMedicines = new List<Medicine__c>();
 }
 
public void processSelected() {

    for(wrapMedicine wrapMedicineObj : wrapMedicineList) {
        if(wrapMedicineObj.selected == true) {
            selectedMedicines.add(wrapMedicineObj.med);
        }
    }
}

public void clearMedicines()
{
        wrapMedicineList = new List<wrapMedicine>();
        for(Medicine__c a: [select Id, Name,Type__c, Values__c, Expiry_Date__c,Selling_Price__c from Medicine__c]) {
            // As each Account is processed, create a new wrapAccount object and add it to the wrapAccountList
            wrapMedicineList.add(new wrapMedicine(a));
        }
}

public PageReference searchMedicines()
{
       string str='%'+name+'%';
        if(name==null || name==''){
            //errormessage ='Name cannot be null or empty. Please Try Again.';
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please Enter Medicine Name'));
        }else{
                wrapMedicineList = new List<wrapMedicine>();
                for(Medicine__c a: [select Id, Name,Type__c, Values__c, Expiry_Date__c,Selling_Price__c from Medicine__c  where Name LIKE:str]) {
                    // As each Account is processed, create a new wrapAccount object and add it to the wrapAccountList
                    wrapMedicineList.add(new wrapMedicine(a));
                }
        }
       return null;
}


// This is wrapper/container class 
// A container class is a class, a data structure, or an abstract data type whose instances are collections of other objects 
// In this example a wrapper class contains both the standard salesforce object Account and a Boolean value

public class wrapMedicine {
    public Medicine__c med {get; set;}
    public Boolean selected {get; set;}

    // This is the contructor method 
    // When we create a new wrapAccount object we pass a Account that is set to the acc property
    // We also set the selected value to false
    
    public wrapMedicine(Medicine__c a) {
        med = a;
        selected = false;
    }
}

 public PageReference confirm() {
 
   Billing__c record = [SELECT Id,Name,Medicines__c,Billing_Date__c,Customer_Name__c,Total_price__c,Quantity__c from Billing__c where Id=:billingId LIMIT 1];   
   record.Total_price__c = total;
   System.debug(record);
   
   String medicines='';
   for(Medicine__c med:selectedMedicines)
   {
       medicines += med.Name+',';
   }

    record.Medicines__c = medicines;
   //    record.Quantity__c = quantity1;
       update record;
       
   PageReference requestPage = new pagereference('/'+billingId);
   requestPage.setRedirect(true);
   return requestPage;  
}

 public PageReference cancel() {
   
 PageReference requestPage = new pagereference('/'+billingId);
 requestPage.setRedirect(true);
 return requestPage;  
}
    
}

I want to pass all the user entered quantity values to the apex controller so that I can reduce it from the available stock.

Thanks in advance.

1 Answer 1

0

Can you please tell me the property {!quantity} in your second pageblocktabel from where it is coming is there any getter setter set for the same in controller of this page ?

You can create a wrapper class combining medicine object and an integer field called quantity. Then in table you can iterate over the list of that wrapper objects then if you set the getter setter for that wrapper object list you should be easily access the quantity entered from table in apex controller.

2
  • In the second table, I am taking the selected records from the first table. In the second table, I am taking quantity value which is not part of the database record from the user and calculating the total of the subtotal and I have added the apex controller code. please check that also. Commented Jul 26, 2020 at 10:02
  • Thank you so much for the answer. I have solved this problem using wrapper class. Commented Jul 26, 2020 at 11:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .