
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,VetriThe 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:
This way, you are bulding up two lists of ideas, each mapped to their boolean key.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);