0

I have problem showing my map value to the visualforce page, in my controller I populate the maps and create below method

public Map<String, Decimal> getmapTargetCurrentMonth(){
    return mapTargetCurrentMonth;
 }

that will be invoked in VF page in apex:repeat class.

    public with sharing class SignedRevenueTargetController{

    //Set up all getter and setter method
    public transient List<User> liactiveusers {get;set;}
    public Date thismonth {get;set;}
    public Date lastmonth {get;set;}
    public String selectedmonth {get;set;}
    public String selectedyear {get;set;}  

    //
    PageReference homepage = new PageReference('/home/home.jsp');

    //Set up all class variables
    String country_iso = [SELECT License_Country__c FROM User WHERE ID =: UserInfo.getUserId()].License_Country__c;
    Id signedTargetRecordType = Contract__c.getSObjectType().getDescribe().getRecordTypeInfosByName().get('Signed Revenue Target').getRecordTypeId();
    Id contractRecordType = Contract__c.getSObjectType().getDescribe().getRecordTypeInfosByName().get('Contract').getRecordTypeId();
    String currency_iso = [SELECT DefaultCurrencyIsoCode FROM User WHERE ID =: UserInfo.getUserId()].DefaultCurrencyIsoCode;

    public Map<String, Decimal> mapTargetCurrentMonth {get;set;}
    public Map<String, Decimal> mapTargetLastMonth {get;set;}
    public Map<String, Decimal> mapCollectedLastMonth {get;set;}

    List<Contract__c> listcurrentsignedtargets = new List<Contract__c>();
    List<Contract__c> listprevioussignedtargets = new List<Contract__c>();
    List<Contract__c> listpreviousactualsigned = new List<Contract__c>();

    public void updateMapValues(Date thismonth){

        mapTargetCurrentMonth = new Map<String, Decimal>();
        mapTargetLastMonth = new Map<String, Decimal>();
        mapCollectedLastMonth = new Map<String, Decimal>();

        liactiveusers = [SELECT Id, Name FROM User WHERE License_Country__c =: country_iso AND isActive = TRUE AND TargetAccount__c <> null AND TargetContract__c <> null ORDER BY Name ASC];
        FOR(User u : liactiveusers){
            mapUserNames.put(u.Name, u.Id);
        }
        system.debug(liactiveusers);


        //Sum up total invoices from active contract within this month
        listcurrentsignedtargets = [SELECT TotalInvoiced__c, Attributedto__r.Name, Attributedto__c FROM Contract__c
                                                        WHERE RecordTypeId =: contractRecordType AND Activation_Date__c =: thismonth AND Attributedto__c in: liactiveusers];

        //query orevious month target                                                
        listprevioussignedtargets = [SELECT TotalInvoiced__c, Attributedto__r.Name, Attributedto__c FROM Contract__c 
                                                        WHERE RecordTypeId =: signedTargetRecordType AND Activation_Date__c =: lastmonth AND Attributedto__c in: liactiveusers];

        //Sum up total actual signed revenue from previous month                                               
        listpreviousactualsigned = [SELECT TotalInvoiced__c, Attributedto__r.Name, Attributedto__c FROM Contract__c 
                                                        WHERE RecordTypeId =: contractRecordType AND Activation_Date__c >=: lastmonth AND Activation_Date__c <: thismonth AND Attributedto__c in: liactiveusers];


        IF(listcurrentsignedtargets.size() > 0){
                    FOR(Contract__c sign :listcurrentsignedtargets ){ 
                        mapTargetCurrentMonth.put(sign.Attributedto__r.Name, sign.TotalInvoiced__c); 
                }

                IF(listprevioussignedtargets.size() > 0){
                    FOR(Contract__c sign :listprevioussignedtargets ){ 
                        mapTargetLastMonth.put(sign.Attributedto__r.Name, sign.TotalInvoiced__c);  
                    }
                }

                IF(listpreviousactualsigned.size() > 0){
                    FOR(User u : liactiveusers){
                        Decimal signedByUser = 0;
                        FOR(Contract__c sign :listpreviousactualsigned){
                            system.debug(sign.TotalInvoiced__c);
                            IF(u.Id == sign.Attributedto__c){
                                signedByUser = signedByUser + sign.TotalInvoiced__c;
                            }
                        }
                        mapCollectedLastMonth.put(u.Name,signedByUser);
                    }
                }

                FOR(User u : liactiveusers){
                    IF(!mapTargetCurrentMonth.containsKey(u.Name)){
                        mapTargetCurrentMonth.put(u.Name, 0);
                    }
                    IF(!mapTargetLastMonth.containsKey(u.Name)){
                        mapTargetLastMonth.put(u.Name, 0);
                    }
                    IF(!mapCollectedLastMonth.containsKey(u.Name)){
                        mapCollectedLastMonth.put(u.Name, 0);
                    }
                }    
            }
    }

}

I want to show value from the map

public Map<String, Decimal> mapTargetCurrentMonth {get;set;}
public Map<String, Decimal> mapTargetLastMonth {get;set;}
public Map<String, Decimal> mapCollectedLastMonth {get;set;}

and this is my visualforce page,

here the apex:repeat does not show the value I want and even <apex:inputText value="{!mapTargetCurrentMonth[person]}"/> doesnt show the input field

<apex:page controller="SignedRevenueTargetController">
    <apex:form >
    <apex:pageBlock title="Target Setting for {!selectedmonth}.{!selectedyear}." mode="edit">
       <!-- <apex:pageBlockButtons >
            <apex:commandButton action="{!submitFieldData}" value="Submit" id="button" rerender="box"/>      
        </apex:pageBlockButtons>-->



    <apex:outputPanel id="lalala" layout="block">
    <apex:pageBlockSection columns="4">

      <apex:pageBlockSectionItem >
          <apex:outputtext value="User" style="font-weight:600; font-size:14px"/>
      </apex:pageBlockSectionItem>
      <apex:pageBlockSectionItem >
          <apex:outputtext value="Target {0, date, MMMM ','  yyyy}" style="font-weight:600; font-size:14px">
              <apex:param value="{!lastmonth}" /> 
          </apex:outputText>
      </apex:pageBlockSectionItem>
      <apex:pageBlockSectionItem >
          <apex:outputtext value="Actual Collected {0, date, MMMM ','  yyyy}" style="font-weight:600; font-size:14px">
              <apex:param value="{!lastmonth}" /> 
          </apex:outputText>
      </apex:pageBlockSectionItem>
      <apex:pageBlockSectionItem >
          <apex:outputtext value="Target {0, date, MMMM ','  yyyy} (Including Tax!)" style="font-weight:600; font-size:14px">
              <apex:param value="{!thismonth}" /> 
          </apex:outputText>
      </apex:pageBlockSectionItem>      
                <apex:repeat value="{!mapTargetCurrentMonth}" var="person">

                            <apex:outputText value="{!person}" style="font-weight:600"/>

                            <apex:outputText value="{0,number,#,##0}">
                                <apex:param value="{!mapTargetLastMonth[person]}" />
                            </apex:outputText>

                            <apex:outputText value="{0,number,#,##0}">
                                <apex:param value="{!mapCollectedLastMonth[person]}" />
                            </apex:outputText>

                            <apex:inputText value="{!mapTargetCurrentMonth[person]}"/> 

                </apex:repeat>
             </apex:pageBlockSection>   
 </apex:outputPanel>



 </apex:pageBlock>
   </apex:form>-->

</apex:page>

my maps do have some values as seen in developer console, so in theory I should be able to refer the map in visualforce but apparently its not working. enter image description here

enter image description here

1 Answer 1

1

The problem is with declaration of your Map variables. Any apex variable to be used in Visualforce page must be declared with {get;set;}.

So ideally it should be declared like this the way you've declared lastMonth in your class.:

public Map<String, Decimal> mapTargetCurrentMonth {get;set;}
public Map<String, Decimal> mapTargetLastMonth {get;set;}
public Map<String, Decimal> mapCollectedLastMonth {get;set;}

Once it is declare, instantiate this in your method where you're populating values in these maps.

5
  • But I have get method for those map , wouldnt it be enough for VF page to recognize that I will invoke getmapTargetCurrentMonth when adding apex:repeat value="{!mapTargetCurrentMonth}" var="person">? this is what I understand, but still nothing show up in my VF Commented Jan 17, 2020 at 4:35
  • No. That won’t help. You need to add getters and setters for each of them. Commented Jan 17, 2020 at 4:37
  • Strange, I try to populate list variables in method updateMapValues using different Sobject and the maps get populated and I can invoke my methods in Apex:Repeat without get set, I modified my question so you can see the differences Commented Jan 17, 2020 at 6:28
  • Yes, I can see the difference. The thumb rule goes like if there are any variables which needs to be used on VF page then they must be declared with {get;set;}. If you're not doing it and trying to achieve using Apex method getYourVariable() this will also work. I would suggest you to go through this thread: developer.salesforce.com/docs/atlas.en-us.apexcode.meta/… Commented Jan 17, 2020 at 6:43
  • Hi Rohit, I have updated my code to have getters setters and instantiate the maps in the method and get no error, but still have no value in VF page, but I debug all the maps and they appear to have some values Commented Jan 17, 2020 at 10:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .