Skip to main content
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

 
4 réponses
  1. 28 mai 2015, 06:20
    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.