0

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(){

    if(checkProduct()){
        shoppingCart.add(getProduct(chosenItemId));
        chosenItemId = null;
    }else{
        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;
        }else{
            return false;
        }
    }
    return false;
}

Snippet of Visualforce page code:

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

                <br/>
                <apex:pageBlockTable value="{!laptopsList}" var="laptop">
                    <apex:column >
                        <apex:commandLink value="{!laptop.Name}">
                            <apex:param name="id" value="{!laptop.Id}"/>
                        </apex:commandLink>
                        <apex:pageBlockSection >
                            <apex:image value="{!URLFOR($Resource.Laptop, laptopsImages[laptop.Name])}"
                                        id="laptopImage"/>
                        </apex:pageBlockSection>
                        <apex:pageBlockSection >
                            <apex:outputText value="{!laptop.Price__c}" id="laptopPrice"/>
                            <br/>
                            <apex:outputText value="{!laptop.Description__c}"/>
                        </apex:pageBlockSection>
                        <br/>
                        <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:commandButton>

                        <br/><br/>
                    </apex:column>
                </apex:pageBlockTable>
                <apex:pageBlockSection rendered="{!renderLaptopsNumber}">
                    <apex:outputText value="There are {!laptopsQuantity} products in this category"
                                     id="productsNumber"/>
                    <br/>
                </apex:pageBlockSection><apex:pageMessages rendered="true"></apex:pageMessages>
            </apex:pageBlock>
  • 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.