I am developing a website on Salesforce. I have two main pages - login, home. On the home page, I just rerender different blocks with products of various types, information about the site and shopping cart. I am able to add some products to my shopping cart by clicking add to cart button.

On the Shopping cart page (actually, it is a block) I have a short summary about each added product. So I am trying to write some validation for adding to cart operation. Because I have custom object Product__c with fields Name, Price__c, Quantity__c, Description__c.

So I want to check if the quantity of products is more than zero, and only then add to the cart. But if the quantity of product is zero, I want to print some error message. I tried to implement this for laptops block, but it did not work. Also, I tried to rerender error message. But every time I click Add to cart button I get an error message and all items are still added to the list. I would really appreciate some help!

Here is few methods from Controller:

 public Product__c getProduct(Id itemId) {
    Product__c item = [
            SELECT Id
                    , Name
                    , Price__c
                    , Description__c
                    , Image__c
                    , Quantity__c
            FROM Product__c
            WHERE Id = :itemId
    return item;

  public void addProductToShoppingCart(){

        chosenItemId = null;
        ApexPages.addmessage(new ApexPages.message(ApexPages.Severity.ERROR,'You can not buy products of this type'));

 public boolean checkProduct(){

    for(Product__c product : productsList){
        if(product.Quantity__c > 0){
            return true;
            return false;
    return false;

Snippet of Visualforce page code:

   <apex:pageBlock id="laptopsBlock" rendered="{!renderLaptop}">

                <apex:pageBlockTable value="{!laptopsList}" var="laptop">
                    <apex:column >
                        <apex:commandLink value="{!laptop.Name}">
                            <apex:param name="id" value="{!laptop.Id}"/>
                        <apex:pageBlockSection >
                            <apex:image value="{!URLFOR($Resource.Laptop, laptopsImages[laptop.Name])}"
                        <apex:pageBlockSection >
                            <apex:outputText value="{!laptop.Price__c}" id="laptopPrice"/>
                            <apex:outputText value="{!laptop.Description__c}"/>
                        <apex:pageMessages id="msgs"></apex:pageMessages>
                        <apex:commandButton value="Add to cart" action="{!addProductToShoppingCart}"
                                            reRender="cartFrom" >
                            <apex:param name="itemsId" value="{!laptop.Id}" assignTo="{!chosenItemId}"></apex:param>

                <apex:pageBlockSection rendered="{!renderLaptopsNumber}">
                    <apex:outputText value="There are {!laptopsQuantity} products in this category"
                </apex:pageBlockSection><apex:pageMessages rendered="true"></apex:pageMessages>
  • 1
    Problem is in checking method. It just check first item (always) and return true/false according to availability of that. YOu need to find product based on chosenItemId and check quantity for that. About error message -- your page message should be within block, that is being rerendered on button click. Probably , It make some sense to use dedicated page message (developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/…) – kurunve Mar 10 '17 at 9:51
  • @kurunve Thank you very much!! I got stuck with this problem for hours, you really helped me :) – Cassie Mar 10 '17 at 10:26

Your Answer

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

Browse other questions tagged or ask your own question.