function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Tyler HarrisTyler Harris 

Grab Output Text ID in Visualforce, Compare it to Map in Apex

I'm trying to grab the ID from the Visualforce page, pass it into a list, and if ID is in the list remove it from my cart. I'm getting a "Incompatible key type List<Id> for Map<Id,StoreFront2.DisplayMerchandise>" on line 26.

Visualforce
<apex:page standardStylesheets="false" showHeader="false" sidebar="false" controller="StoreFront2">
  <apex:stylesheet value="{!URLFOR($Resource.styles)}"/>
  <h1>Store Front</h1>
    <apex:variable var="rowDel" value='{!0}'/>
  <apex:form >
      <apex:dataTable value="{!products}" var="pitem" rowClasses="odd,even">
          
          <apex:column headerValue="Product">
              <apex:outputText value="{!pitem.merchandise.name}" />
          </apex:column>
          <apex:column headervalue="Price">
              <apex:outputText value="{!pitem.merchandise.Price__c}" />
          </apex:column>
          <apex:column headerValue="#Items">
              <apex:inputText value="{!pitem.tempCount}"/>
          </apex:column>
   
      </apex:dataTable>
      
      <br/>
      <apex:commandButton action="{!shop}" value="Add to Cart" reRender="msg,cart" >
      
      </apex:commandButton>
  </apex:form>
  
    <apex:outputPanel id="msg">{!message}</apex:outputPanel><br/>    
    <h1>Your Basket</h1>
    
<apex:form>    
     <apex:dataTable id="cart" value="{!cart}" var="carti" rowClasses="odd,even">
			
   <apex:column headerValue="ID" rendered="false">
       <apex:outputText value="{!cart[carti].merchandise.Id}" >
         	<apex:param name='rowDel' value="{!cart[carti].merchandise.Id}" />
       </apex:outputText>
          </apex:column>
          <apex:column headerValue="Product">
              <apex:outputText value="{!cart[carti].merchandise.name}">
          	
         </apex:outputText>
          </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:column headerValue="Remove?">
             <apex:commandButton action="{!Remove}" value='Remove' reRender="cart" />
         </apex:column>
                     

      </apex:dataTable>
    </apex:form>
    
  
</apex:page>
Apex
public class StoreFront2 {    
    public String message { get; set; }
    List<DisplayMerchandise> products;
    Map<Id, DisplayMerchandise> cart;
    public List<Id> row2Del{get;set;}
    private Id rowz;
    public id rowDel{get;set;}
    
    public PageReference shop(){
   
        handleTheBasket();
        message = 'You bought: ';
        for (DisplayMerchandise p:products){
            if(p.tempCount > 0){
               message += p.merchandise.name + ' (' + p.tempCount + ') ' ;
               }
            }
        return null;
    }
    
    public void remove(){
     rowz = ApexPages.currentPage().getParameters().get(rowDel);
        row2Del.add(rowz);
        
        if(cart.containsKey(row2Del)){
            cart.remove(row2Del);
            
        }
     
        
         
    }

    public void handleTheBasket(){
        for(DisplayMerchandise c : products){
            if(c.tempCount > 0){
            if(cart.containsKey(c.merchandise.Id)){
                
                cart.get(c.merchandise.Id).count += c.tempCount;
                
            }
            else{
                cart.put(c.merchandise.Id, c);
                cart.get(c.merchandise.Id).count = c.tempCount;
             
          		

            } 
        
        }
        }
        
    }
    
    
    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 Decimal tempCount{get;set;}
        public Integer rowNumber { 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;
    }

}


 
Best Answer chosen by Tyler Harris
Ajay Nagar 7Ajay Nagar 7
Actually the problem is null id is passed in remove function.Try this code:
public class StoreFront2 {    
    public String message { get; set; }
    List<DisplayMerchandise> products;
    Map<Id, DisplayMerchandise> cart;
    public List<Id> row2Del{get;set;}
    private Id rowz;
    public id rowDel{get;set;}
    
    public PageReference shop(){
   
        handleTheBasket();
        message = 'You bought: ';
        for (DisplayMerchandise p:products){
            if(p.tempCount > 0){
               message += p.merchandise.name + ' (' + p.tempCount + ') ' ;
               }
            }
        return null;
    }
    
    public void remove(){
        row2Del = new List<id>();
        row2Del.add(rowDel);
        if(cart.containsKey(rowDel)){
            cart.remove(rowDel);
        }  
         
    }

    public void handleTheBasket(){
        for(DisplayMerchandise c : products){
            if(c.tempCount > 0){
            if(cart.containsKey(c.merchandise.Id)){
                
                cart.get(c.merchandise.Id).count += c.tempCount;
                
            }
            else{
                cart.put(c.merchandise.Id, c);
                cart.get(c.merchandise.Id).count = c.tempCount;
             
                

            } 
        
        }
        }
        
    }
    
    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 Decimal tempCount{get;set;}
        public Integer rowNumber { 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;
    }

}
 
<apex:page standardStylesheets="false" showHeader="false" sidebar="false" controller="StoreFront2">
 <apex:stylesheet value="{!URLFOR($Resource.styles)}"/>

  <h1>Store Front</h1>
   
  <apex:form >
      <apex:dataTable value="{!products}" var="pitem" rowClasses="odd,even">
          
          <apex:column headerValue="Product">
              <apex:outputText value="{!pitem.merchandise.name}" />
          </apex:column>
          <apex:column headervalue="Price">
              <apex:outputText value="{!pitem.merchandise.price__c}" />
          </apex:column>
          <apex:column headerValue="#Items">
              <apex:inputText value="{!pitem.tempCount}"/>
          </apex:column>
   
      </apex:dataTable>
      
      <br/>
      <apex:commandButton action="{!shop}" value="Add to Cart" reRender="msg,cart" >
      
      </apex:commandButton>
  </apex:form>
  
    <apex:outputPanel id="msg">{!message}</apex:outputPanel><br/>    
    <h1>Your Basket</h1>
    
<apex:form >    
     <apex:dataTable id="cart" value="{!cart}" var="carti" rowClasses="odd,even">
            
   <apex:column headerValue="ID" rendered="false">
       <apex:outputText value="{!cart[carti].merchandise.Id}" >
            <apex:param name="rowDel" value="{!cart[carti].merchandise.Id}" />
       </apex:outputText>
          </apex:column>
          <apex:column headerValue="Product">
              <apex:outputText value="{!cart[carti].merchandise.name}">
            
         </apex:outputText>
          </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:column headerValue="Remove?">
             <a onclick="Delete('{!cart[carti].merchandise.Id}');" style="cursor:pointer;" >Remove</a>
         </apex:column>
                     

      </apex:dataTable>
     <apex:actionFunction name="Delete" Action="{!Remove}" reRender="cart"  >
     <apex:param name="deleteParameter" value="" assignTo="{!rowDel}"/>
     </apex:actionFunction>
    </apex:form>
    
  
</apex:page>

 

All Answers

Ajay Nagar 7Ajay Nagar 7
Instead of passing list "row2Del" in line no 25,26 pass Id "rowz".
Tyler HarrisTyler Harris
Thank you Ajay! It's still not removing the row when I hit remove. Any idea why?

Visualforce
<apex:page standardStylesheets="false" showHeader="false" sidebar="false" controller="StoreFront2">
  <apex:stylesheet value="{!URLFOR($Resource.styles)}"/>
  <h1>Store Front</h1>
   
  <apex:form >
      <apex:dataTable value="{!products}" var="pitem" rowClasses="odd,even">
          
          <apex:column headerValue="Product">
              <apex:outputText value="{!pitem.merchandise.name}" />
          </apex:column>
          <apex:column headervalue="Price">
              <apex:outputText value="{!pitem.merchandise.Price__c}" />
          </apex:column>
          <apex:column headerValue="#Items">
              <apex:inputText value="{!pitem.tempCount}"/>
          </apex:column>
   
      </apex:dataTable>
      
      <br/>
      <apex:commandButton action="{!shop}" value="Add to Cart" reRender="msg,cart" >
      
      </apex:commandButton>
  </apex:form>
  
    <apex:outputPanel id="msg">{!message}</apex:outputPanel><br/>    
    <h1>Your Basket</h1>
    
<apex:form>    
     <apex:dataTable id="cart" value="{!cart}" var="carti" rowClasses="odd,even">
			
   <apex:column headerValue="ID" rendered="false">
       <apex:outputText value="{!cart[carti].merchandise.Id}" >
         	<apex:param name='rowDel' value="{!cart[carti].merchandise.Id}" />
       </apex:outputText>
          </apex:column>
          <apex:column headerValue="Product">
              <apex:outputText value="{!cart[carti].merchandise.name}">
          	
         </apex:outputText>
          </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:column headerValue="Remove?">
             <apex:commandButton action="{!Remove}" value='Remove' reRender="cart" />
         </apex:column>
                     

      </apex:dataTable>
    </apex:form>
    
  
</apex:page>

Apex
public class StoreFront2 {    
    public String message { get; set; }
    List<DisplayMerchandise> products;
    Map<Id, DisplayMerchandise> cart;
    public List<Id> row2Del{get;set;}
    private Id rowz;
    public id rowDel{get;set;}
    
    public PageReference shop(){
   
        handleTheBasket();
        message = 'You bought: ';
        for (DisplayMerchandise p:products){
            if(p.tempCount > 0){
               message += p.merchandise.name + ' (' + p.tempCount + ') ' ;
               }
            }
        return null;
    }
    
    public void remove(){
        row2Del = new List<id>();
     rowz = ApexPages.currentPage().getParameters().get(rowDel);
       
        row2Del.add(rowz);
        
        if(cart.containsKey(rowz)){
            cart.remove(rowz);
            
        }  
         
    }

    public void handleTheBasket(){
        for(DisplayMerchandise c : products){
            if(c.tempCount > 0){
            if(cart.containsKey(c.merchandise.Id)){
                
                cart.get(c.merchandise.Id).count += c.tempCount;
                
            }
            else{
                cart.put(c.merchandise.Id, c);
                cart.get(c.merchandise.Id).count = c.tempCount;
             
          		

            } 
        
        }
        }
        
    }
    
    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 Decimal tempCount{get;set;}
        public Integer rowNumber { 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;
    }

}

 
Ajay Nagar 7Ajay Nagar 7
Actually the problem is null id is passed in remove function.Try this code:
public class StoreFront2 {    
    public String message { get; set; }
    List<DisplayMerchandise> products;
    Map<Id, DisplayMerchandise> cart;
    public List<Id> row2Del{get;set;}
    private Id rowz;
    public id rowDel{get;set;}
    
    public PageReference shop(){
   
        handleTheBasket();
        message = 'You bought: ';
        for (DisplayMerchandise p:products){
            if(p.tempCount > 0){
               message += p.merchandise.name + ' (' + p.tempCount + ') ' ;
               }
            }
        return null;
    }
    
    public void remove(){
        row2Del = new List<id>();
        row2Del.add(rowDel);
        if(cart.containsKey(rowDel)){
            cart.remove(rowDel);
        }  
         
    }

    public void handleTheBasket(){
        for(DisplayMerchandise c : products){
            if(c.tempCount > 0){
            if(cart.containsKey(c.merchandise.Id)){
                
                cart.get(c.merchandise.Id).count += c.tempCount;
                
            }
            else{
                cart.put(c.merchandise.Id, c);
                cart.get(c.merchandise.Id).count = c.tempCount;
             
                

            } 
        
        }
        }
        
    }
    
    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 Decimal tempCount{get;set;}
        public Integer rowNumber { 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;
    }

}
 
<apex:page standardStylesheets="false" showHeader="false" sidebar="false" controller="StoreFront2">
 <apex:stylesheet value="{!URLFOR($Resource.styles)}"/>

  <h1>Store Front</h1>
   
  <apex:form >
      <apex:dataTable value="{!products}" var="pitem" rowClasses="odd,even">
          
          <apex:column headerValue="Product">
              <apex:outputText value="{!pitem.merchandise.name}" />
          </apex:column>
          <apex:column headervalue="Price">
              <apex:outputText value="{!pitem.merchandise.price__c}" />
          </apex:column>
          <apex:column headerValue="#Items">
              <apex:inputText value="{!pitem.tempCount}"/>
          </apex:column>
   
      </apex:dataTable>
      
      <br/>
      <apex:commandButton action="{!shop}" value="Add to Cart" reRender="msg,cart" >
      
      </apex:commandButton>
  </apex:form>
  
    <apex:outputPanel id="msg">{!message}</apex:outputPanel><br/>    
    <h1>Your Basket</h1>
    
<apex:form >    
     <apex:dataTable id="cart" value="{!cart}" var="carti" rowClasses="odd,even">
            
   <apex:column headerValue="ID" rendered="false">
       <apex:outputText value="{!cart[carti].merchandise.Id}" >
            <apex:param name="rowDel" value="{!cart[carti].merchandise.Id}" />
       </apex:outputText>
          </apex:column>
          <apex:column headerValue="Product">
              <apex:outputText value="{!cart[carti].merchandise.name}">
            
         </apex:outputText>
          </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:column headerValue="Remove?">
             <a onclick="Delete('{!cart[carti].merchandise.Id}');" style="cursor:pointer;" >Remove</a>
         </apex:column>
                     

      </apex:dataTable>
     <apex:actionFunction name="Delete" Action="{!Remove}" reRender="cart"  >
     <apex:param name="deleteParameter" value="" assignTo="{!rowDel}"/>
     </apex:actionFunction>
    </apex:form>
    
  
</apex:page>

 
This was selected as the best answer
Tyler HarrisTyler Harris
I caught that, but it's still not removing the Merchandise from the cart. When I clic remove the screen refreshes, but nothing happens.