1

I am trying to build a map

Map<ID,Post_Chat_Reponse__c>

Where ID is master record id and Post_Chat_Reponse__c is details object.

Here is my code

Getting null for ID

No idea why is this coming null

Here is the code:

List<Post_Chat_Response_Items__c> pcrimstext = new List<Post_Chat_Response_Items__c>();
List<Post_Chat_Response_Items__c> pcrimsradio= new List<Post_Chat_Response_Items__c>();
Map<ID,Post_Chat_Reponse__c> Mapofidandpostresponse = new Map<ID,Post_Chat_Reponse__c>();
List<Post_Chat_Reponse__c> postchatresponse = new List<Post_Chat_Reponse__c>();

for(Post_Chat_Question__c poqc: [Select id,Name,Question_Type__c,Sequence__c,(Select Id,Name,PostChatQuestion__c from Post_Chat_Reponse__r) from Post_Chat_Question__c Where Question_Type__c=:'Radio' OR Question_Type__c=:'Text' ORDER BY Sequence__c]){
    Post_Chat_Reponse__c pcr = new Post_Chat_Reponse__c();
    pcr.name=poqc.Name+'001';
    pcr.PostChatQuestion__c=poqc.id;
    pcr.Chat_Key__c='Demokey';
    pcr.Response_type__c=poqc.Question_Type__c;
    postchatresponse.add(pcr);
}

insert postchatresponse;
system.debug('###postchatresponse'+postchatresponse);
for(Post_Chat_Reponse__c pcrc:postchatresponse){
     system.debug('PARENTID'+pcrc.PostChatQuestion__r.Id);
     if(pcrc.PostChatQuestion__c.Id!=null){
        Mapofidandpostresponse.put(pcrc.PostChatQuestion__r.Id,pcrc);
        system.debug('###Mapofidandpostresponse'+Mapofidandpostresponse);
    }
}
//for(QuestionRadioWrapper qrw: qRadios ){
Post_Chat_Response_Items__c pcri = new Post_Chat_Response_Items__c();
pcri.Name=Mapofidandpostresponse.get('a3a560000000Rye').Name+'Line Item';
pcri.Post_Chat_Reponse__c=Mapofidandpostresponse.get('a3a560000000Rye').id;
pcri.Response_Line_Item_1__c='qrw.selectedRadio';
pcrimsradio.add(pcri);  


insert pcrimsradio;
1

Your conditional's written incorrectly here:

 if(pcrc.PostChatQuestion__c.Id!=null){
    Mapofidandpostresponse.put(pcrc.PostChatQuestion__r.Id,pcrc);
    system.debug('###Mapofidandpostresponse'+Mapofidandpostresponse);
}

Whenever you try to reference a field within a nested sObject, you need the __r form, not __c. However, you don't need to (and should not) refer to a nested sObject here, because you did not create or query this record with a nested sObject value. Instead, just use pcrc.PostChatQuestion__c, which your code explicitly populates.

| improve this answer | |
  • Thanks David It worked – Amit Rajput Mar 26 '19 at 11:56
1

You can directly use PostChatQuestion__c field of Post_Chat_Reponse__c record as key i.e. it will be same as the master's record ID field and use the instance of Post_Chat_Reponse__c as value.

Modified code should be something like below

for(Post_Chat_Reponse__c pcrc:postchatresponse){
     if(pcrc.PostChatQuestion__c !=null){
        Mapofidandpostresponse.put(pcrc.PostChatQuestion__c,pcrc);
    }
}

Also, note that you are creating a map of ParentId and only one child as value in your map, hence if your master has multiple Post_Chat_Reponse__c, then it will be overwritten in your map.

| improve this answer | |
  • Yes Vijay It is working now. – Amit Rajput Mar 26 '19 at 12:08

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.