You need to sign in to do that
Don't have an account?
Vetriselvan 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.
Thanks,
Vetri
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
You may want to do something like this:
This way, you are bulding up two lists of ideas, each mapped to their boolean key.
All Answers
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
You may want to do something like this:
This way, you are bulding up two lists of ideas, each mapped to their boolean key.
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
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
Can you help me?
Thanks,
Vetri