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
apex_keenapex_keen 

Binding values of Map on VF page

I was browsing on my problem in this community and saw many faced similar issue but not found any exact solution .

 

Problem is simple..I've a map like this 

Map<id,integer> MapAppstoRating = new Map<id,integer>();

 

Controller populates this Map. Now what I want is to show values of this map in my VF page. Other thing is..I want to display name of the associated custom object 'record Name' instead of 'record ID' and integer value.

 

Now thing is, I was able to populate id with integer value with something like :

 

<apex:pageblockTable var="c" value="{!MapAppstoRating }" >
<apex:repeat value="{!MapAppstoRating [c]}" var="temp">
<apex:column value="{!c}" />
</apex:repeat>

<apex:repeat value="{!MapAppstoRating [c]}" var="temp">
<apex:column value="{!temp}" />
</apex:repeat>
</apex:pageblockTable>

 

But it has 2 problems..

 

Firstly..it is populating just first element of associated map. Other rows are not getting populated. But major problem is..I 've to populate name 

instead of IDs... Is that possible somehow. or I need to change my map to get names instead of ids?( changing that will be more cumbersome for me ) 

 

thanks for help!

 

Best Answer chosen by Admin (Salesforce Developers) 
LakshmanLakshman

Alright, I think the below code will work for you:

 

For(review__c rv : allrevs)
{
 ratingsum = MapAppstoRating.get(rv.Job_Application__c);

 ratingsum  = ratingsum + Integer.valueOf(rv.rating__c);
 MapAppstoRating.put(rv.Job_Application__c,ratingsum );

}
Map<Job_Application__c, Integer> finalMapAppstoRating = new Map<Job_Application__c, Integer>();
for(Job_Application__c ja: [Select Id,Name from Job_Application__c where Id in :MapAppstoRating.keySet()])
{
 finalMapAppstoRating.put(ja,MapAppstoRating.get(ja.Id));
}

 Use finalMapAppstoRating in your visualforce page. I am pretty mych sure that it should work.

 

Regards,

Lakshman

 

All Answers

apex_keenapex_keen

thanks for your reply....but I've already seen that post before posting my question. It actually didn't answer my question. I've to print names associated with Ids which is the problem. My basic question : is that possible or not or I've to change my maps to reach solution ?

 

Thanks !

 

LakshmanLakshman

Please try as below and let me know if it works:

 

//Change Controller map from Id to SObject
Map<SObject,integer> MapAppstoRating = new Map<SObject,integer>();

//Below is the VFP
<apex:pageblockTable var="c" value="{!MapAppstoRating }" >
<apex:column value="{!c.Name}"  headerValue="Name"/>
<apex:column headerValue="Count">
<apex:repeat value="{!MapAppstoRating [c]}" var="temp">
<apex:column value="{!temp}" />
</apex:repeat>
</apex:column>
</apex:pageblockTable>

 

 

Regards,

Lakshman

apex_keenapex_keen

Hi Lakhsman, Thanks for reply. But it is giving problem

 

Actually, let me give little more detail,

During the process of  populating the given map(i.e. MapAppstoRating) , I need to get  existing value from it. and then add new value in it. Giving dummy example  below :

For(review__c rv : allrevs)

{

 ratingsum = MapAppstoRating.get(rv.Job_Application__c);

 ratingsum  = ratingsum + Integer.valueOf(rv.rating__c);
 MapAppstoRating.put(rv.Job_Application__c,ratingsum );

}

 

Now first line inside 'for' loop is working only , if map has 'id' as 'key' . Whatever else(sobject, actual object name etc)  I'm using, it is not working.

 

But while printing values of Map, I need to print names..instead of Id...(here name of job_application__c object)

 

Any help on this is appreciated!

 

 

 

LakshmanLakshman

One quick question, How are you getting this: rv.Job_Application__c, is it through SOQL? If yes can you try doing it like: rv.Job_Application__r.Name. But doing so we will need unique names.

 

Regards,

Lakshman

LakshmanLakshman

Alright, I think the below code will work for you:

 

For(review__c rv : allrevs)
{
 ratingsum = MapAppstoRating.get(rv.Job_Application__c);

 ratingsum  = ratingsum + Integer.valueOf(rv.rating__c);
 MapAppstoRating.put(rv.Job_Application__c,ratingsum );

}
Map<Job_Application__c, Integer> finalMapAppstoRating = new Map<Job_Application__c, Integer>();
for(Job_Application__c ja: [Select Id,Name from Job_Application__c where Id in :MapAppstoRating.keySet()])
{
 finalMapAppstoRating.put(ja,MapAppstoRating.get(ja.Id));
}

 Use finalMapAppstoRating in your visualforce page. I am pretty mych sure that it should work.

 

Regards,

Lakshman

 

This was selected as the best answer
apex_keenapex_keen

Thanks a lot for you help Lakshman , this actually worked for me :)

LakshmanLakshman

I am glad that it worked for you :)

 

Regards,

Lakshman