function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
GailGail 

Map of Opp ID and Contact Owner ID from Opp ID

I need to create a map of the Opp ID and the primary contact owner id when I have the opp id.

So far, I have:
for (OpportunityContactRole OCR: [Select ContactId from OpportunityContactRole where OpportunityID in :newOpp and IsPrimary = TRUE]){
                OppAndContact.put(OCR.id, OCR.contactId);
        }

I'm kind of stuck now - is there a way I can get the map in one step? 
Best Answer chosen by Gail
Raj VakatiRaj Vakati
Try this 
 
Map<Id,List<Id>> OppAndContact = new Map<Id,List<ID>>();

for (OpportunityContactRole OCR: [Select ContactId ,Contact.OwnerId , from OpportunityContactRole where OpportunityID in :newOpp and IsPrimary = TRUE]){
			OppAndContact.put(OCR.id, OCR.Contact.OwnerId );
			
			if(OppAndContact.containsKey(OCR.id){
	OppAndContact.get(OCR.id).add( OCR.Contact.OwnerId );
}
else{
	OppAndContact.put(CR.id, OCR.Contact.OwnerId );
}
}

Or
 
Map<Id,List<Id>> OppAndContact = new Map<Id,List<ID>>();

for (OpportunityContactRole OCR: [Select ContactId ,Contact.Owner.Name , from OpportunityContactRole where OpportunityID in :newOpp and IsPrimary = TRUE]){
			OppAndContact.put(OCR.id, OCR.Contact.Owner.Name);
			
			if(OppAndContact.containsKey(OCR.id){
	OppAndContact.get(OCR.id).add( OCR.Contact.Owner.Name);
}
else{
	OppAndContact.put(CR.id, OCR.Contact.Owner.Name);
}
}

 

All Answers

Raj VakatiRaj Vakati
Try this
 
Map<Id,List<Id>> OppAndContact = new Map<Id,List<ID>>();

for (OpportunityContactRole OCR: [Select ContactId from OpportunityContactRole where OpportunityID in :newOpp and IsPrimary = TRUE]){
			OppAndContact.put(OCR.id, OCR.contactId);
			
			if(OppAndContact.containsKey(OCR.id){
	OppAndContact.get(OCR.id).add( OCR.contactId);
}
else{
	OppAndContact.put(CR.id, OCR.contactId);
}
}

 
GailGail
this doesn't get me the Contact Owner. What I need to end up with is a map of Opportunity ID and the full name of the Primary Contact's Owner. 
Raj VakatiRaj Vakati
Try this 
 
Map<Id,List<Id>> OppAndContact = new Map<Id,List<ID>>();

for (OpportunityContactRole OCR: [Select ContactId ,Contact.OwnerId , from OpportunityContactRole where OpportunityID in :newOpp and IsPrimary = TRUE]){
			OppAndContact.put(OCR.id, OCR.Contact.OwnerId );
			
			if(OppAndContact.containsKey(OCR.id){
	OppAndContact.get(OCR.id).add( OCR.Contact.OwnerId );
}
else{
	OppAndContact.put(CR.id, OCR.Contact.OwnerId );
}
}

Or
 
Map<Id,List<Id>> OppAndContact = new Map<Id,List<ID>>();

for (OpportunityContactRole OCR: [Select ContactId ,Contact.Owner.Name , from OpportunityContactRole where OpportunityID in :newOpp and IsPrimary = TRUE]){
			OppAndContact.put(OCR.id, OCR.Contact.Owner.Name);
			
			if(OppAndContact.containsKey(OCR.id){
	OppAndContact.get(OCR.id).add( OCR.Contact.Owner.Name);
}
else{
	OppAndContact.put(CR.id, OCR.Contact.Owner.Name);
}
}

 
This was selected as the best answer
GailGail
Thanks! I actually came to that on my own in the end:

    for (OpportunityContactRole OCR: [Select Opportunityid, ContactId, Contact.OwnerId from OpportunityContactRole where OpportunityID in :OppIds and IsPrimary = TRUE and Contact.Owner.UserRole.Name like '%SDR%']){
            //add Opp ID and Owner name to a Map of opp id and contact name string
            OppAndContactOwner.put(OCR.Opportunityid, OCR.Contact.OwnerId);
            }

Needed to put in the Opp Id, not the OCR ID and didn't realize at first I could traversed down to Contact Owner ID.