You need to sign in to do that
Don't have an account?
Kavita Kale
Unable to call a method from visualforce page
Hi All,
I am trying to create a simple calculator performing total = quantity * price. But my method written in controller is not getting called. ANy help would be appreciated.
visualforce code-
<apex:page id="ProductCalculator" controller="ProductCalculator">
<apex:pageBlock id="pbProdCalc">
<apex:form id="fProdCalc">
<apex:dataTable id="dtProdCalc" columns="4" value="{!Products}" var="con" styleclass="list">
<apex:column id="prodList" headerValue="Product">
<apex:selectList id="slProdList" value="{!product}" size="1" multiselect="false">
<apex:selectOptions id="soProdList" value="{!items}"/>
</apex:selectList>
</apex:column>
<apex:column id="prodQuantity" headerValue="Quantity">
<apex:inputText id="Qty" value="{!quantity}"/>
</apex:column>
<apex:column id="prodPrice" headerValue="Price">
<apex:inputText id="Price" value="{!prodPrice}">
<apex:actionSupport action="{!calculateTotal}" event="onchange" reRender="Total"/>
</apex:inputText>
</apex:column>
<apex:column id="prodTotal" headerValue="Total">
<apex:outputText id="Total" value="{!prodTotal}"/>
</apex:column>
</apex:dataTable>
</apex:form>
</apex:pageBlock>
</apex:page>
Controller code-
public class ProductCalculator {
Integer qty; Integer price; Integer total;
public PageReference calculateTotal() {
system.debug('calculate total called' );
calcTotal();
return null;
}
public Integer calcTotal()
{
total = qty * price;
system.debug('total is: ' + total);
return total;
}
public Integer getQuantity() {
return qty;
}
public void setQuantity(Integer q) {
qty = q;
}
public Integer getProdPrice() {
return price;
}
public void setProdPrice(Integer p)
{
price = p;
}
public Integer getProdTotal() {
return total;
}
public void setProdTotal(Integer t)
{
total = t;
}
public List<SelectOption> getItems() {
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('Product1','Product 1'));
options.add(new SelectOption('Product2','Product 2'));
options.add(new SelectOption('Product3','Product 3'));
return options;
}
String[] prod = new String[]{};
public String[] getProduct() {
return prod;
}
public void setProduct(String[] prod)
{
this.prod = prod;
}
String ProductType;
List<Product2> products;
public List<Product2> getProducts() {
if(products == null)
products = [select id, name from Product2 Limit 1];
return products;
}
}
I am trying to create a simple calculator performing total = quantity * price. But my method written in controller is not getting called. ANy help would be appreciated.
visualforce code-
<apex:page id="ProductCalculator" controller="ProductCalculator">
<apex:pageBlock id="pbProdCalc">
<apex:form id="fProdCalc">
<apex:dataTable id="dtProdCalc" columns="4" value="{!Products}" var="con" styleclass="list">
<apex:column id="prodList" headerValue="Product">
<apex:selectList id="slProdList" value="{!product}" size="1" multiselect="false">
<apex:selectOptions id="soProdList" value="{!items}"/>
</apex:selectList>
</apex:column>
<apex:column id="prodQuantity" headerValue="Quantity">
<apex:inputText id="Qty" value="{!quantity}"/>
</apex:column>
<apex:column id="prodPrice" headerValue="Price">
<apex:inputText id="Price" value="{!prodPrice}">
<apex:actionSupport action="{!calculateTotal}" event="onchange" reRender="Total"/>
</apex:inputText>
</apex:column>
<apex:column id="prodTotal" headerValue="Total">
<apex:outputText id="Total" value="{!prodTotal}"/>
</apex:column>
</apex:dataTable>
</apex:form>
</apex:pageBlock>
</apex:page>
Controller code-
public class ProductCalculator {
Integer qty; Integer price; Integer total;
public PageReference calculateTotal() {
system.debug('calculate total called' );
calcTotal();
return null;
}
public Integer calcTotal()
{
total = qty * price;
system.debug('total is: ' + total);
return total;
}
public Integer getQuantity() {
return qty;
}
public void setQuantity(Integer q) {
qty = q;
}
public Integer getProdPrice() {
return price;
}
public void setProdPrice(Integer p)
{
price = p;
}
public Integer getProdTotal() {
return total;
}
public void setProdTotal(Integer t)
{
total = t;
}
public List<SelectOption> getItems() {
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('Product1','Product 1'));
options.add(new SelectOption('Product2','Product 2'));
options.add(new SelectOption('Product3','Product 3'));
return options;
}
String[] prod = new String[]{};
public String[] getProduct() {
return prod;
}
public void setProduct(String[] prod)
{
this.prod = prod;
}
String ProductType;
List<Product2> products;
public List<Product2> getProducts() {
if(products == null)
products = [select id, name from Product2 Limit 1];
return products;
}
}
<apex:page id="ProductCalculator" controller="ProductCalculatorNew">
<apex:pageBlock id="pbProdCalc">
<apex:form id="fProdCalc">
<apex:actionFunction name="calculateTotal" action="{!calculateTotal}" reRender="counter1,msg"/>
<!-- <apex:pageMessages id="msg"></apex:pageMessages> -->
<apex:outputPanel id="counter1">
<apex:dataTable id="dtProdCalc" columns="4" value="{!Products}" var="con" styleclass="list">
<apex:column id="prodList" headerValue="Product">
<apex:selectList id="slProdList" value="{!prod}" size="1" multiselect="false">
<apex:selectOptions id="soProdList" value="{!items}"/>
</apex:selectList>
</apex:column>
<apex:column id="prodQuantity" headerValue="Quantity">
<apex:inputText id="Qty" value="{!quantity}"/>
</apex:column>
<apex:column id="prodPrice" headerValue="Price">
<apex:inputText id="Price" value="{!prodPrice}" onchange="calculateTotal()"/>
</apex:column>
<apex:column id="prodTotal" headerValue="Total">
<apex:outputpanel id="counter">
<apex:outputText id="ShowTotal" value="{!ProdTotal}"/>
</apex:outputpanel>
</apex:column>
</apex:dataTable>
</apex:outputPanel>
</apex:form>
</apex:pageBlock>
</apex:page>
/*********** Controller *****************/
public class ProductCalculatorNew {
Integer qty; Integer price; Integer total;
public void calculateTotal()
{
system.debug('function called');
total = qty * price;
// ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Invalid Input.');
// ApexPages.addMessage(myMsg);
}
public Integer getQuantity() {
return qty;
}
public void setQuantity(Integer q) {
qty = q;
}
public Integer getProdPrice() {
return price;
}
public void setProdPrice(Integer p)
{
price = p;
}
public Integer getProdTotal() {
return total;
}
public void setProdTotal(Integer t)
{
total = t;
}
public List<SelectOption> getItems() {
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('Product1','Product 1'));
options.add(new SelectOption('Product2','Product 2'));
options.add(new SelectOption('Product3','Product 3'));
return options;
}
public String prod {get; set;}
String ProductType;
List<Product2> products;
public List<Product2> getProducts() {
if(products == null)
products = [select id, name from Product2 Limit 1];
return products;
}
}
All Answers
thanks,
http://www.forcexplore.com/2014/01/salesforce-interview-questions-2.html
I tried your code in my org. But i can't get the solution. The issue related to renderering.
Is this ok for you if i write new code related to your functionality So that you can easily calculate total.
Please replace this code with your existing one.
<apex:page id="ProductCalculator" controller="ProductCalculator">
<script>
function calTotal() {
var quan = document.getElementById("ProductCalculator:pbProdCalc:fProdCalc:dtProdCalc:0:Qty").value;
var proPrice = document.getElementById("ProductCalculator:pbProdCalc:fProdCalc:dtProdCalc:0:Price").value;
var total = quan * proPrice;
console.log('##########' + quan);
console.log('##########' + total);
console.log('##########' + proPrice);
document.getElementById("ProductCalculator:pbProdCalc:fProdCalc:dtProdCalc:0:Total").innerText = total;
}
</script>
<apex:pageBlock id="pbProdCalc">
<apex:form id="fProdCalc">
<apex:dataTable id="dtProdCalc" columns="4" value="{!Products}" var="con" styleclass="list">
<apex:column id="prodList" headerValue="Product">
<apex:selectList id="slProdList" value="{!product}" size="1" multiselect="false">
<apex:selectOptions id="soProdList" value="{!items}"/>
</apex:selectList>
</apex:column>
<apex:column id="prodQuantity" headerValue="Quantity">
<apex:inputText id="Qty" value="{!quantity}"/>
</apex:column>
<apex:column id="prodPrice" headerValue="Price">
<apex:inputText id="Price" value="{!prodPrice}" onblur="calTotal();">
</apex:inputText>
</apex:column>
<apex:column id="prodTotal" headerValue="Total">
<apex:outputText id="Total" value="{!prodTotal}"/>
</apex:column>
</apex:dataTable>
</apex:form>
</apex:pageBlock>
</apex:page>
I will calculate total amount using javascript there is no need to write method in controller. When you entered value in Price field and click on anywhere in the screen at that time amount will be calculated. Hope this wil be help you.
/************************ Visualforce**********************************/
<apex:page id="ProductCalculator" controller="ProductCalculator">
<apex:variable value="{!0}" var="total2"/>
<apex:pageBlock id="pbProdCalc">
<apex:form id="fProdCalc">
<apex:dataTable id="dtProdCalc" columns="4" value="{!productData}" var="con" styleclass="list">
<apex:column id="prodList" headerValue="Product">
<apex:selectList id="slProdList" size="1" multiselect="false">
<apex:selectOptions id="soProdList" value="{!con.items}"/>
</apex:selectList>
</apex:column>
<apex:column id="prodQuantity" headerValue="Quantity">
<apex:inputText id="Qty" value="{!con.quantity}"/>
</apex:column>
<apex:column id="prodPrice" headerValue="Price">
<apex:inputText id="Price" value="{!con.prodPrice}">
<apex:actionSupport action="{!con.calculateTotal}" event="onchange"/>
</apex:inputText>
</apex:column>
<apex:column id="prodTotal" headerValue="Total">
<apex:outputText id="Total" value="{!con.prodTotal}"/>
<apex:variable var="total2" value="{!total2 + con.prodTotal}" />
<apex:facet name="footer">
Total: <span class="t2"></span>
</apex:facet>
</apex:column>
</apex:dataTable>
<apex:commandButton value="Add Product" action="{!addRow}"/>
<apex:outputText ></apex:outputText>
</apex:form>
</apex:pageBlock>
<script>
// Here we will set the generated subtotals to the footer cells
document.getElementsByClassName('t1')[0].innerHTML = '{!ROUND(total2,0)}';
</script>
</apex:page>
/************************ controller **********************************/
public class ProductCalculator {
public void addRow() {
productData.add(new multiRowProduct());
}
public List<multiRowProduct> productData { get; set; }
public ProductCalculator()
{
loadData();
}
public void loadData()
{
productData = new List<multiRowProduct>();
for (List<Product2> p : [select id, name from Product2 Limit 1])
{
for (Product2 prows : p)
{
multiRowProduct mp = new multiRowProduct();
mp.getQuantity();
mp.getProdPrice();
mp.getProdTotal();
mp.getItems();
productData.add(mp);
}
}
}
public class multiRowProduct
{
Integer qty; Integer price; Integer total;
public PageReference calculateTotal() {
system.debug('calculate total called' );
calcTotal();
return null;
}
public Integer calcTotal()
{
total = qty * price;
system.debug('total is: ' + total);
return total;
}
public Integer getQuantity() {
return qty;
}
public void setQuantity(Integer q) {
qty = q;
}
public Integer getProdPrice() {
return price;
}
public void setProdPrice(Integer p)
{
price = p;
}
public Integer getProdTotal() {
return total;
}
public void setProdTotal(Integer t)
{
total = t;
}
public List<SelectOption> getItems() {
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('Product1','Product 1'));
options.add(new SelectOption('Product2','Product 2'));
options.add(new SelectOption('Product3','Product 3'));
return options;
}
String[] prod = new String[]{};
public String[] getProduct() {
return prod;
}
public void setProduct(String[] prod)
{
this.prod = prod;
}
}
}
<apex:page id="ProductCalculator" controller="ProductCalculatorNew">
<apex:pageBlock id="pbProdCalc">
<apex:form id="fProdCalc">
<apex:actionFunction name="calculateTotal" action="{!calculateTotal}" reRender="counter1,msg"/>
<!-- <apex:pageMessages id="msg"></apex:pageMessages> -->
<apex:outputPanel id="counter1">
<apex:dataTable id="dtProdCalc" columns="4" value="{!Products}" var="con" styleclass="list">
<apex:column id="prodList" headerValue="Product">
<apex:selectList id="slProdList" value="{!prod}" size="1" multiselect="false">
<apex:selectOptions id="soProdList" value="{!items}"/>
</apex:selectList>
</apex:column>
<apex:column id="prodQuantity" headerValue="Quantity">
<apex:inputText id="Qty" value="{!quantity}"/>
</apex:column>
<apex:column id="prodPrice" headerValue="Price">
<apex:inputText id="Price" value="{!prodPrice}" onchange="calculateTotal()"/>
</apex:column>
<apex:column id="prodTotal" headerValue="Total">
<apex:outputpanel id="counter">
<apex:outputText id="ShowTotal" value="{!ProdTotal}"/>
</apex:outputpanel>
</apex:column>
</apex:dataTable>
</apex:outputPanel>
</apex:form>
</apex:pageBlock>
</apex:page>
/*********** Controller *****************/
public class ProductCalculatorNew {
Integer qty; Integer price; Integer total;
public void calculateTotal()
{
system.debug('function called');
total = qty * price;
// ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Invalid Input.');
// ApexPages.addMessage(myMsg);
}
public Integer getQuantity() {
return qty;
}
public void setQuantity(Integer q) {
qty = q;
}
public Integer getProdPrice() {
return price;
}
public void setProdPrice(Integer p)
{
price = p;
}
public Integer getProdTotal() {
return total;
}
public void setProdTotal(Integer t)
{
total = t;
}
public List<SelectOption> getItems() {
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('Product1','Product 1'));
options.add(new SelectOption('Product2','Product 2'));
options.add(new SelectOption('Product3','Product 3'));
return options;
}
public String prod {get; set;}
String ProductType;
List<Product2> products;
public List<Product2> getProducts() {
if(products == null)
products = [select id, name from Product2 Limit 1];
return products;
}
}