You need to sign in to do that
Don't have an account?
Tyler Harris
Increment a count else add item to list Apex
Hi All,
I'm trying to increment a counter if a user tries to add the same merchandise item to the shopcart. If its a new merchandise item, add the item with it's correct total.
I'm trying to increment a counter if a user tries to add the same merchandise item to the shopcart. If its a new merchandise item, add the item with it's correct total.
public class StoreFront2 { public String message { get; set; } DisplayMerchandise[] products; DisplayMerchandise[] cart; public PageReference shop(){ for(DisplayMerchandise c :products){ if(c.id == cart.merchandise.id){ cart.count++; } else{ cart.add(c); } } message = 'You bought: '; for (DisplayMerchandise p:products){ if(p.count > 0){ message += p.merchandise.name + ' (' + p.count + ') ' ; } } return null; } public DisplayMerchandise[] getCart() { if(cart == null){ cart = new DisplayMerchandise[]{}; } return cart; } public class DisplayMerchandise { public Merchandise__c merchandise{get; set;} public Decimal count{get; set;} public DisplayMerchandise(Merchandise__c item){ this.merchandise = item; } } public DisplayMerchandise[] getProducts() { if (products == null){ products = new DisplayMerchandise[]{}; for (Merchandise__c item : [SELECT id, name, description__c, price__c FROM Merchandise__c WHERE Total_Inventory__c > 0]) { products.add(new DisplayMerchandise(item)); } } return products; } }
You are using {!carti[carti].count} instead of {!cart[carti].count}. Correct it and it should be fixed.
It should be - { ! c a r t [ c a r t i ] . c o u n t } without the spaces.
You may also copy the mark-up I had pasted earlier.
All Answers
At line 13, it should be:
13 if(c.merchandise.id == cart.merchandise.id){
Please try and let me know, if you are still facing issue.
At line 13 below code should be working fine, i think the error is due to some other line of code. Can you just add this line and share screenshot of error?
13 if(c.merchandise.id == cart.merchandise.id){
Looks like your advice clear the initial error, but now it's throwing it for an inner variable.
there is no need to create new variable.
Replace "increse" with "cart"
While line #14 suggests it should be a local variable of Id type, line # 15 suggests its a instance of DisplayMerchandise.
Make it -
DisplayMerchandise increase = c;
at line #14.
Ideally you should declare 'increase' based on the scope you want to live it for.
for(DisplayMerchandise c :products){
if(c.merchandise.id == cart.merchandise.id){
DisplayMerchandise increase = c;
cart.count += increase.count;
}
You should have a nested for loop to compare an instance of DisplayMerchandise with an element in the list - cart.
Or to improve performance have Maps instead to retrieve required instances based on Id.
for(DisplayMerchandise c :products){
if(c.merchandise.id == cart.merchandise.id){
for(DisplayMerchandise increase:c){
cart.count += increase.count;
}
for(DisplayMerchandise c :products){
if(c.merchandise.id == cart.merchandise.id){
cart.count += cart.count;
}
This should work for you.
It's still throwing a Field expression must be a concrete SObject:List <StoreFront.DisplayMerchandise> on line 14
Primarily modified 'cart' to a map instead of a list. Its copied from Notepad++, regret any inconvenience in reading.
---------------------------------
public class StoreFront2 {
public String message { get; set; }
List<DisplayMerchandise> products;
Map<Id, DisplayMerchandise> cart;
public PageReference shop(){
for(DisplayMerchandise c : products){
if(cart.containsKey(c.merchandise.Id)){
cart.get(c.Id).count = count++;
}
else{
cart.put(c.merchandise.Id, c);
}
}
message = 'You bought: ';
for (DisplayMerchandise p:products){
if(p.count > 0){
message += p.merchandise.name + ' (' + p.count + ') ' ;
}
}
return null;
}
public Map<Id, DisplayMerchandise> getCart() {
if(cart == null){
cart = new Map<Id, DisplayMerchandise>{};
}
return cart;
}
public class DisplayMerchandise {
public Merchandise__c merchandise{get; set;}
public Decimal count{get; set;}
public DisplayMerchandise(Merchandise__c item){
this.merchandise = item;
}
}
public List<DisplayMerchandise> getProducts() {
if (products == null){
products = new List<DisplayMerchandise>{};
for (Merchandise__c item :
[SELECT id, name, description__c, price__c
FROM Merchandise__c
WHERE Total_Inventory__c > 0]) {
products.add(new DisplayMerchandise(item));
}
}
return products;
}
}
cart.get(c.Id).count = cart.get(c.Id).count + c.count;
instead of
cart.get(c.Id).count = count++;
If it does, please do select the best answer (the one you think helped you the most) and mark the question as resolved.
It's now showing a "Variable does not exist:Id" Error on line 9.
cart.get(c.Id).count should be cart.get(c.merchandise.Id).count.
If I have not already forgotten dealing with Maps on VF, probably following snippet should work -
<apex:dataTable id="cart" value="{!cart}" var="carti" rowClasses="odd,even">
<apex:column headerValue="Product">
<apex:outputText value="{!cart[carti].merchandise.name}" />
</apex:column>
<apex:column headervalue="Price">
<apex:outputText value="{!cart[carti].merchandise.Price__c}" />
</apex:column>
<apex:column headerValue="#Items">
<apex:outputText value="{!cart[carti].count}"/>
</apex:column>
</apex:dataTable>
Let me know if it doesn't and I have a work-around (but that requires tweaking the controller as well a bit).
It's now throwing a "Expression of type Text cannot be subscripted"
You are using {!carti[carti].count} instead of {!cart[carti].count}. Correct it and it should be fixed.
It should be - { ! c a r t [ c a r t i ] . c o u n t } without the spaces.
You may also copy the mark-up I had pasted earlier.