+ Start a Discussion
Vetriselvan ManoharanVetriselvan Manoharan 

Maps not adding up values

Hi,

I am using a Map<Boolean, Idea> . I am trying to adding up values in the map in a for loop. But only the last added values only saved in the map. I can't able to identify it. 
 
listofIdeas = new Map<Boolean, Idea>();
        ideaList = [ SELECT Title,Body,Id,VoteScore,VoteTotal FROM Idea Order By CreatedDate desc ];
        for(Idea ids: ideaList)
        {
            ideaIds.add(ids.Id);
        }
        List<Vote> userVote = [SELECT CreatedById, ParentId FROM Vote WHERE CreatedById = :Userinfo.getUserId() AND ParentId = :ideaIds];
        System.debug('Hi' +userVote);
        for(Idea ideas : ideaList)
        {
            for(Vote votes: userVote)
            {
                if(votes.ParentId == ideas.Id)
                {    
                    isVoted = true;              
                }
                else
                {
                    notVoted = true; 
                }                
            }
            System.debug('Voted'+ ideas.Title + isVoted); 
            if(isVoted == true)
            {
                listofIdeas.put(true,ideas);
            }
            else if(notVoted == true)
            {
                listofIdeas.put(false,ideas);
            }  
        }
        System.debug('Ideas List' + listofIdeas);
I want to compare if the user already voted on particular idea. I am facing problems on comparing it and send it to VF page and based on that I should the promote and demote button.

Thanks,
Vetri
 
Best Answer chosen by Vetriselvan Manoharan
SForceBeWithYouSForceBeWithYou
The key of a map can only occur once... Your map should be Map<Idea, Boolean>.  Otherwise, only one true mapping and one false mapping will occur... and the last mapping you inserted will be the one left.

You may want to do something like this:
listofIdeas = new Map<Boolean, List<Idea>>();
listofIdeas.put(true, new List<Idea>());
listofIdeas.put(false, new List<Idea>());

        ideaList = [ SELECT Title,Body,Id,VoteScore,VoteTotal FROM Idea Order By CreatedDate desc ];
        for(Idea ids: ideaList)
        {
            ideaIds.add(ids.Id);
        }
        List<Vote> userVote = [SELECT CreatedById, ParentId FROM Vote WHERE CreatedById = :Userinfo.getUserId() AND ParentId = :ideaIds];
        System.debug('Hi' +userVote);
        for(Idea iterIdea : ideaList)
        {
            for(Vote votes: userVote)
            {
                if(votes.ParentId == iterIdea.Id)
                {    
                    isVoted = true;              
                }
                else
                {
                    notVoted = true; 
                }                
            }
            System.debug('Voted'+ iterIdea.Title + isVoted); 
            if(isVoted == true)
            {
                listofIdeas.get(true).add(iterIdea);
            }
            else if(notVoted == true)
            {
                listofIdeas.get(false).add(iterIdea);
            }  
        }
        System.debug('Ideas List' + listofIdeas);

This way, you are bulding up two lists of ideas, each mapped to their boolean key.

All Answers

Ravi NarayananRavi Narayanan

      List<Vote> userVote = [SELECT CreatedById, ParentId FROM Vote WHERECreatedById = :Userinfo.getUserId() AND ParentId IN  :ideaIds];
Please change the query on the line 07.

 

Mark as best answer if it solves your problem

SForceBeWithYouSForceBeWithYou
The key of a map can only occur once... Your map should be Map<Idea, Boolean>.  Otherwise, only one true mapping and one false mapping will occur... and the last mapping you inserted will be the one left.

You may want to do something like this:
listofIdeas = new Map<Boolean, List<Idea>>();
listofIdeas.put(true, new List<Idea>());
listofIdeas.put(false, new List<Idea>());

        ideaList = [ SELECT Title,Body,Id,VoteScore,VoteTotal FROM Idea Order By CreatedDate desc ];
        for(Idea ids: ideaList)
        {
            ideaIds.add(ids.Id);
        }
        List<Vote> userVote = [SELECT CreatedById, ParentId FROM Vote WHERE CreatedById = :Userinfo.getUserId() AND ParentId = :ideaIds];
        System.debug('Hi' +userVote);
        for(Idea iterIdea : ideaList)
        {
            for(Vote votes: userVote)
            {
                if(votes.ParentId == iterIdea.Id)
                {    
                    isVoted = true;              
                }
                else
                {
                    notVoted = true; 
                }                
            }
            System.debug('Voted'+ iterIdea.Title + isVoted); 
            if(isVoted == true)
            {
                listofIdeas.get(true).add(iterIdea);
            }
            else if(notVoted == true)
            {
                listofIdeas.get(false).add(iterIdea);
            }  
        }
        System.debug('Ideas List' + listofIdeas);

This way, you are bulding up two lists of ideas, each mapped to their boolean key.
This was selected as the best answer
Ajay Nagar 7Ajay Nagar 7
Hi vetriselvan,

Map saves values with reference to its keys means Key should be unique,here you have used boolean value for key which is not unique thats why it is updating the value for Key[Boolean].Instead of the above map you can use Set for the above issue to collect ids of Ideas which are already voted.

Thanks
Ajay
Vetriselvan ManoharanVetriselvan Manoharan
Hi,

That worked out. I want to bind the map in VF page. Based on the boolean field in the map, I should display a link in VF page. Can you help me with it?

Actually below is the repeat control with list. Now need to convert it as map. If the Boolean in Map is true, then only I need to display promote and demote link
 
<div>
        <apex:pageBlock title="Ideas Listing">
            <apex:repeat value="{!ideaList}" var="i">
                <apex:pageBlockTable value="{!ideaList[i]}" var="idea">
                <div style="margin-top:15px;">
                    <apex:inputHidden id="IdeaId" value="{!idea.Id}"/>
                    <div>Points:{!idea.VoteTotal}</div>
                    <div>Title:{!idea.Title}</div>
                    <div>Description:{!idea.Body}</div>
                    <a href="#" onClick="promoteIdea('{!idea.id}')">Promote</a>
                    <a href="#" onClick="demoteIdea('{!idea.id}')">Demote</a>
                </div>
                </apex:pageBlockTable>
            </apex:repeat>
           </apex:pageBlock>
       </div>

Can you help me?

Thanks,
Vetri