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 

Get variable value held within Map?

Hi All,

I'm trying to figure out how to total the count of various DisplayMerchandise objects I have contained in a Map. I need to scan all DisplayMerchandise in the cart Map and total the counts.

Apex:
public virtual class StoreFront2 {

 List<DisplayMerchandise> products;
    Map<Id, DisplayMerchandise> cart;


}


public PageReference buy(){
        
        List<Merchandise__c> toMerch = new List<Merchandise__c>();
        List<Merchandise__c> updateMerch = new List<Merchandise__c>();
        
        if(ct != null && cart !=null ){
            
            for(Decimal i=0; i < cart.size(); i++){
                
            }
            
            insert ct;
            
            for(ID merch:cart.keySet()){
               toMerch = [SELECT id, name FROM Merchandise__c WHERE id = :merch];
                for(Merchandise__c mer:toMerch){
                    mer.Purchases__c = ct.id;
                    updateMerch.add(mer);
                }    
            } 
            update updateMerch;
        }
        
        return null;     
    }

public Map<Id, DisplayMerchandise> getCart() {
        if(cart == null){
            cart = new Map<Id, DisplayMerchandise>();
            incart = false;
                }
      
        return cart;
    }
    
    public class DisplayMerchandise {
        public Merchandise__c merchandise{get; set;}
        public Decimal count{get; set;}
        public Decimal tempCount{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;
    }

Visualforce
<apex:page controller="StoreFront2" showHeader="false" sidebar="false" >
    <apex:stylesheet value="{!URLFOR($Resource.styles)}"/>
    <h1>Confirm Page</h1>
  <apex:form >
      <apex:outputPanel id="changem">
    
      	<apex:pageBlock id="thePageBlock" rendered="{!flag}">
            
          		Find Existing Contact <apex:inputField required="false" value="{!ct.Purchases__c}" ></apex:inputField> <br/> <br/>
          
      	</apex:pageBlock>
      </apex:outputPanel> 
      
          <apex:outputPanel >
          <apex:pageBlock >
              Find Existing Buyer<Apex:actionRegion ><apex:inputCheckbox value="{!flip}" ><apex:actionSupport event="onchange" reRender="changem,change2" action="{!doShow}" status="StatusChange"/><apex:actionStatus startText=" Updating Page" id="StatusChange"/></apex:inputCheckbox></Apex:actionRegion><br/>
          </apex:pageBlock>
          </apex:outputPanel>
      
      <apex:outputPanel id="change2" >
      <apex:pageBlock rendered="{!flag2}">
          
          	First Name: <apex:inputText title="First Name" value="{!ct2.FirstName}"/><br/>
          	Last Name: <apex:inputText title="Last Name" value="{!ct2.LastName}" /><br/>
          Email Address: <apex:inputText title="Email Address" value="{!ct2.Email}"/><br/>
          Account: <apex:inputField value="{!ct2.Accountid}"/>
      		</apex:pageBlock>
          </apex:outputPanel>
      
     <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: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:dataTable><br/>
      <apex:commandButton action="{!back}" value="Back"/>
      
      <apex:commandButton action="{!buy}" value="Buy" rendered="{!incart}" />
    </apex:form>
  
</apex:page>

 
Best Answer chosen by Tyler Harris
SKolakanSKolakan

Hi,

You can get the cart merchandise from cartMap into a list and loop the list to get count.

 List<Merchandise__c> cartMerchandise = new List<Merchandise__c>();
cartMerchandise = cartMap.values();

//now loop the cartMerchandise list to get count