1

I have below snippet of apex class where I face below error to compile. Can someone please help?

Error

Incompatible value type Survey_Question_vod__c for Map>

Code Snippet

  List<Survey_Question_vod__c> oldques = [select Id, Name,Question_vod__c,Survey_vod__c from Survey_Question_vod__c where Survey_vod__c in : oldsurvey];
    List<Survey_Question_vod__c> newques = [select Id, Name,Question_vod__c,Survey_vod__c from Survey_Question_vod__c where Survey_vod__c in : newsurvey];
    Map<Id,Survey_Question_vod__c> oldmapques = new map<Id,Survey_Question_vod__c>();
    for(Survey_Question_vod__c sq: oldques.values())
    {oldmapques.put(sq.Question_vod__c,sq);

    }  

     Map<Id,List<Survey_Question_vod__c>> newmapques = new map<Id,List<Survey_Question_vod__c>>();
    for(Survey_Question_vod__c sq: newques)
    {newmapques.put(sq.Question_vod__c,sq);

    }  
7
  • you map should be Map<Id,Survey_Question_vod__c> and iterate over oldques.values()
    – Ratan Paul
    Commented May 5, 2016 at 5:49
  • But oldques is a list of Survey_Question_vod__c. It throws now this error - Method does not exist or incorrect signature: [List<Survey_Question_vod__c>].values() Commented May 5, 2016 at 5:52
  • Do you want a list or single record as the value?
    – Adrian Larson
    Commented May 5, 2016 at 5:53
  • Please find the updated snippet. Commented May 5, 2016 at 5:54
  • 1
    Well for list there is no method values().your first for loop must be like this: for(Survey_Question_vod__c sq: oldques
    – Mr.Frodo
    Commented May 5, 2016 at 5:59

1 Answer 1

2

Updated

Since oldques is a list just iterate over it and assign it to map

Completed code

Map<Id,Survey_Question_vod__c> oldmapques = new map<Id,Survey_Question_vod__c>();
 for(Survey_Question_vod__c sq: oldques)
 {  
       oldmapques.put(sq.Question_vod__c,sq);

}

Updates

Looks like you want to map parentId to all child records

Map<Id,List<Survey_Question_vod__c>> newmapques = new map<Id,List<Survey_Question_vod__c>>();
    for(Survey_Question_vod__c sq: newques)
    {
        if(!newmapques.containsKey(sq.Question_vod__c))
            newmapques.put(sq.Question_vod__c, new list<Survey_Question_vod__c>{sq});
        else
            newmapques.get(sq.Question_vod__c).add(sq);
    }

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .