0

I have the below implementation:

Map<Id, Contact> contIdsMap = new Map<Id, Contact>(conts);
Map<Id, Opportunity> relatedOppMap = new Map<Id, Opportunity>([Select Id, Name 
                                                              From Opportunity Where Id IN 
                                                              (Select OpportunityId From OpportunityContactRole Where ContactId =: contIdsMap.keySet()));
for(Opportunity o: relatedOppMap.values()){
   System.debug('o: ' +o); //Returns an Opp.
   List<OpportunityContactRole> ocrList = o.OpportunityContactRoles; 
   System.debug('ocrList: ' +o.OpportunityContactRoles); //Returns empty.
   for(OpportunityContactRole ocr: o.OpportunityContactRoles){
      //Not entering to this for loop as o.OpportunityContactRoles is always empty.
   }
}

On the Salesforce UI, I can see two Opportunity Contact Roles for the opportunity o that I have in my loop. But in my list, I cannot get any. Am I missing anything here? Should I need to do any further query/debug to identify I am not getting any OCRs?

Main requirement I am trying to achieve: Trying to find if there is a OCR on a particular contact and it's respective related contact. If there is no OCR for the related contact, I need to insert a OCR to that contact.

for(Opportunity o: relatedOppMap.values()){
        System.debug('o: ' +o);
        System.debug('Insider Opps For loop:');
        List<OpportunityContactRole> ocrList = o.OpportunityContactRoles;
        System.debug('ocrList: ' +o.OpportunityContactRoles);
        for(OpportunityContactRole ocr: ocrList){
            System.debug('Insider ocr1 For loop:');
            if(combinedContIdsMap.containsKey(ocr.ContactId)){
                System.debug('If Statement: for combined Contacts map');
                Contact secCon = new Contact();
                Contact priCon = new Contact();
                String secContsOcrRole;
                if(combinedContIdsMap.get(ocr.ContactId).Primary_Contact__c !=null && combinedContIdsMap.containskey(combinedContIdsMap.get(ocr.ContactId).Primary_Contact__c)){
                    System.debug('If Statement: for identifying sec Contact');
                    secCon = combinedContIdsMap.get(ocr.ContactId); 
                    priCon = combinedContIdsMap.get(combinedContIdsMap.get(ocr.ContactId).Primary_Contact__c); 
                    secContsOcrRole = ocr.Role; //Role of Secondary Contact's OCR

                    for(OpportunityContactRole ocr2: ocrList){
                        System.debug('Insider ocr2 For loop:');
                        if(ocr2.ContactId ==  priCon.Id && ocr2.Role == secContsOcrRole + '_New'){
                            System.debug('Skip here');
                        } else if(ocr2.ContactId ==  priCon.Id && ocr2.Role != secContsOcrRole + '_New)'){
                            System.debug('Need to insert here');
                            OpportunityContactRole newPriaryContOcr = new OpportunityContactRole(OpportunityId = ocr.OpportunityId, 
                                                                                                 Role = secContsOcrRole + '_New', 
                                                                                                 ContactId = priCon.Id);
                            newPriaryContOCRList.add(newPriaryContOcr);
                        } else if(ocr2.ContactId !=  priCon.Id){
                            System.debug('Need to insert here');
                            OpportunityContactRole newPriaryContOcr = new OpportunityContactRole(OpportunityId = ocr.OpportunityId, 
                                                                                                 Role = secContsOcrRole + '_New', 
                                                                                                 ContactId = priCon.Id);
                            newPriaryContOCRList.add(newPriaryContOcr);
                        }
                    }
                }
            }
        }
    }
1
  • Is your apex class with sharing or without sharing? if not can you try using without sharing keyword?7 Commented Sep 4, 2018 at 15:04

1 Answer 1

2

You're not querying the OpportunityContactRoles in your SELECT clause.

Map<Id, Opportunity> relatedOppMap = new Map<Id, Opportunity>(
    [Select Id, Name 
     From Opportunity 
     Where Id IN (Select OpportunityId 
                  From OpportunityContactRole 
                  Where ContactId =: contIdsMap.keySet())]
);

Your OpportunityContactRole SELECT is in a semi-join sub-select, which doesn't return data to you. It's only used to limit the records returned by the main query.

You need to include a parent-to-child subquery in your main SELECT clause in order to obtain data for the child objects.:

    [Select Id, Name, (SELECT Id, ContactId FROM OpportunityContactRoles)
     From Opportunity 
     Where Id IN (Select OpportunityId 
                  From OpportunityContactRole 
                  Where ContactId =: contIdsMap.keySet())]
6
  • Wow, what a miss on my end? Cool. This really helps. For me, its a learning curve here. Commented Sep 4, 2018 at 15:10
  • @AustinEvans, there might even be an easier way to do what you're aiming at. If you want to edit a little more information into your question about the logic you need to build around this data, I'd be happy to expand.
    – David Reed
    Commented Sep 4, 2018 at 15:12
  • Sure. I would love to get some great eyes to take a look at my implementation. Commented Sep 4, 2018 at 15:15
  • Please find the updated question. Commented Sep 4, 2018 at 15:25
  • 1
    There's a lot of complexity there that I'm not sure I can address right away (I was hoping I could offer you a quick optimization by flipping the primary and secondary query objects). See if you can get it working like this, though.
    – David Reed
    Commented Sep 4, 2018 at 15:46

You must log in to answer this question.

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