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);

    }  
  • you map should be Map<Id,Survey_Question_vod__c> and iterate over oldques.values() – Ratan Paul May 5 '16 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() – Testing_SFDC May 5 '16 at 5:52
  • Do you want a list or single record as the value? – Adrian Larson May 5 '16 at 5:53
  • Please find the updated snippet. – Testing_SFDC May 5 '16 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 May 5 '16 at 5:59
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);
    }
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.