1

I have a requirement to show map values and list values simultaneously in a page. Can someone suggest a wayout? Apex controller:

public class IncidentDetailExtnController{
    Incident__c oInc {get;set;}
    Id recId;
    public List<Incident_Transaction__c> oIncTranListHCPDetail {get;set;}
    public String profName{get;set;}
    public Map<id,String> MapIncTxn{get;set;}

    public IncidentDetailExtnController(ApexPages.StandardController controller) 
    {
        recId =ApexPages.CurrentPage().getparameters().get('Id');
        oInc = (Incident__c) controller.getRecord();
        oIncTranListHCPDetail = [SELECT 
                                Call_Detail_abv__c , Call_Detail_abv__r.Product_vod__c , Call_Detail_abv__r.Call2_vod__c , Call_Detail_abv__r.Call2_vod__r.Call_Date_vod__c , 
                                Call_Detail_abv__r.Call2_vod__r.Associated_Prescriber_ABS_CIA__c , Call_Detail_abv__r.Call2_vod__r.Associated_Prescriber_CIA__r.Name , 
                                Call_Detail_abv__r.Call2_vod__r.Account_vod__r.Name , Call_Detail_abv__r.Call2_vod__r.Address_vod__c , Call_Detail_abv__r.Type_vod__c , 
                                Call_Sample_abv__c , Call_Sample_abv__r.Account_vod__r.Name , Call_Sample_abv__r.Call2_vod__c , Call_Sample_abv__r.Call2_vod__r.Activity_Type__c , 
                                Call_Sample_abv__r.Call2_vod__r.Address_vod__c , Call_Sample_abv__r.Call2_vod__r.Associated_Prescriber_ABS_CIA__c , id,
                                Call_Sample_abv__r.Call2_vod__r.Associated_Prescriber_CIA__r.Name , Call_Sample_abv__r.Call2_vod__r.Call_Date_vod__c , 
                                Call_Sample_abv__r.Quantity_vod__c , Call_Sample_abv__r.Product_vod__c , Call_Sample_abv__r.Product_vod__r.Product_Type_vod__c , 
                                Date_of_Last_Sync_abv__c , Date_of_Prev_Sync_abv__c , Days_bwn_Sync_abv__c , Outcome_abv__c , Transaction_type__c , Call__c , 
                                Call__r.Account_vod__r.Name , Call__r.Associated_Prescriber_ABS_CIA__c , Call__r.Associated_Prescriber_CIA__r.Name , Call__r.Call_Date_vod__c , 
                                Call__r.Address_vod__c, Call__r.Detailed_Products_vod__c,Prof_First_Name_abv__c,Prof_Middle_Name_abv__c,Prof_Last_Name_abv__c,Prof_Address_Line1_abv__c
                                 FROM Incident_Transaction__c where Incident__c = :recId ];

   Map<id,String> MapIncTxn= new Map<id,String>();   

   for(   Incident_Transaction__c IT :oIncTranListHCPDetail )
   {
   profName=IT.Prof_First_Name_abv__c+' '+IT.Prof_Middle_Name_abv__c+' '+IT.Prof_Last_Name_abv__c;
   MapIncTxn.put(IT.id,profName);

   }                              
    }   //constr ends  
    public PageReference attest()
    {

        return null;
    }   
}

Apex page snippet:

<apex:pageBlockSection title="HCP Calls" columns="4" collapsible="true">
    <apex:pageBlockTable columns="4" value="{!oIncTranListHCPDetail}" border="true" var="itr" cellPadding="4" >
    <apex:column headerValue="Professional Name" />
    <apex:column value="{!itr.Prof_First_Name_abv__c}" />  <!-- Need to put Profname string here from the map -->
    <apex:column headerValue=" ABS on Call" value="{!itr.Prof_First_Name_abv__c}" />  
    <apex:column headerValue=" Professional Address" value="{!itr.Prof_First_Name_abv__c}" />
    <apex:column headerValue=" Associated Prescriber" value="{!itr.Prof_First_Name_abv__c}" />
    <apex:column headerValue=" Product" value="{!itr.Prof_First_Name_abv__c}" />
    <apex:column headerValue=" Activity Date" value="{!itr.Prof_First_Name_abv__c}" />
    <apex:column headerValue=" Outcome - LOV" value="{!itr.Prof_First_Name_abv__c}" />
  • If I got what you need correctly, best way I would say to have a wrapper class(inner class) with the fields to feed your pageBlockTable – highfive Nov 20 '14 at 10:01
  • Hi highfive,Can you please change my code here? – Testing_SFDC Nov 20 '14 at 10:31
1

You can directly reference the map in your page like this (see e.g. Binding Values of Map on Visualforce Page):

<apex:column headerValue="..." value="{!MapIncTxn[itr.Id]}" />

but personally for this case I would avoid the map altogether and just do the formatting directly in the Visualforce:

<apex:column headerValue="..." value="{!itr.Prof_First_Name_abv__c} {!itr.PProf_Middle_Name_abv__c} {!itr.Prof_Last_Name_abv__c}" />
  • Well cleared! but @KeithC, isn't it good enough to have an inner class structure in this kind of situation? So that we can modify the structure whenever we want? – highfive Nov 20 '14 at 11:07
  • @highfive The inner class approach is sometimes necessary (e.g. adding row checkboxes) or preferable (e.g. when the derived value logic is complicated or slow when executed in Visualforce). But in this example it is purely simple presentation logic being done and Visualforce is a good place to do that. In my view clarity and simplicity wins over adding features that might never be used. Add the inner class when its needed, not before its needed. – Keith C Nov 20 '14 at 11:27
  • Of course, got the difference and the usage. Sorry for hijacking the thread, but may helpful for readers :) – highfive Nov 20 '14 at 11:32
  • 1
    @highfive Agreed. One of my pet hates is the phrase "best practice" being used to justify a design choice: I'd much rather see the benefits and costs described explicitly. Few things in software are absolute. – Keith C Nov 20 '14 at 11:52
1

I did something similar not long ago. What you need is an inner class in your controller that will contain the objects that you need. An example:

public class yourController {
    public class yourInnerClass {
        //some variables and objects that you need
        Object1__c yourObject1;
        Object2__c yourObject2;
        public yourInnerClass(Object1__c o1, Object1__c o2) {
            //that is your constructor
        }
        public Object1__c getYourObject1() {
            return yourObject1;
        }
        public Object2__c getYourObject2() {
            return yourObject2;
        }
        //some other methods
    }
    List<yourInnerClass> yourInnerClasses;
    //your controller other methods, constructor(s), etc.
    public yourInnerClass getYourInnerClasses() {
        return yourInnerClasses;
    }
}

And visualforce page:

<apex:page controller="yourController">
    <apex:form>
        <apex:pageBlock >
            <apex:pageblockTable value="{!yourInnerClasses}" var="someVariableName">

Then you could access these objects in your visualforce page. For example:

<apex:column value="{!someVariableName.yourObject1.Name}"/>
<apex:column value="{!someVariableName.yourObject2.Name}"/>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.