+ Start a Discussion
anshuanshu 

Unable to get values from map

Hi All,

 

I need to insert question and all associated answer with those questions , questions could be multiple and answer for single question could be multiple.

I am having a map and I am putiing values to that map, but when I tried to get the value and debug those keyset values then those values have changed.

Here , is the code snippet:

map<Questions__c,list<Answers__c>> quesans= new map<Questions__c,list<Answers__c>>();

 

//in this map , I am putting the values. Here ques is question instance and allans is list of answers

  quesans.put(ques,allans); 

 

Then , i need to insert questions first as there's master-detail relationship between question and answers:But this statement is coming as null , blow is the code:

 

ist<Questions__c>qq=new list<Questions__c>();
qq.addall(quesans.keyset());

insert qq;

for(Questions__c q:qq){
if(quesans.get(q)!= Null){   // but this is coming null while map is returning correct values???
for(Answers__c a : quesans.get(q)){
a.Questions__r = q ;
anss.add(a);
}
}

 

if any one found/got the issue please help me out.Thanks in advance

 

Sridhar VenkateswaraluSridhar Venkateswaralu

Can u try this?

a.Questions__c = q.Id;

 

instead of a.Questions__r = q;

 

Apart from this I dont see anything wrong. Also can u paste the Debug

 

 

HariDineshHariDinesh

Hi,

Here if I understand correctly, the below condition

if(quesans.get(q)!= Null){ ----> is not satisfied?

If yes means here you are getting NULL as per your Explanation.

 

So if my guess is correct, there are not values in the MAP itself. (quesans)

Check that map Values through Debug Logs First.

And one more point I want to know is if there is master detail relation is there between these two object how can you have the list of answers(childs) without having any relation to Question(Master)?

And in for Loop you are trying to assign the master ID to the Existing Childs which is wrong.

So first recap what you are doing here and try to check everything with Debug Logs

anshuanshu

Hi HariDinesh,

 

 

In quesans map , I am getting velues , its not returning null values

and the list of answers I am getting from Api response.

Both questions and correspondn answers  we are capturing from Api response and thats not at all a problem.

and after getting response , i need to insert those response in Question and Answe custom object.