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
SFDCDevQASFDCDevQA 

Trouble pushing contact ID via Opportunity Contact Role

I've been trying and trying to get this code to work and it seems that no matter which way I format it I keep getting errors regarding pulling the contact ID via the contact role.  Just for background when a quote is sent I'm trying to create a course for each Contact in the associated opportunity contact roles section.  Here's what I have that gives me the least amount of errors but still tells me I can't associate the Contact ID that way.

 

If anyone could help I would appreciate it very much.

Thank you,

Amanda

 

trigger QuoteAdoptionAgreementSentCreateCourses on Quote (after update) {
   // map tracks records on OpportunityID

    Map<String, Quote> QuotetoOpportunityIDMap = new Map<String, Quote>();

    for( Quote record : Trigger.new )
    {
        if( record.Adoption_Agreement_Sent__c == date.today())

        {
              QuotetoOpportunityIDMap.put( record.OpportunityID, record );            
        }
        
    }

 //map to keep track of the opportunity contact roles
    map<Id, OpportunityContactRole> OppIDtoContactRolesMap = new map<Id, OpportunityContactRole>();

    List<Contact> AllContacts = new List<Contact>();

    //select OpportunityContactRoles for the opportunities with contact role required

    List<OpportunityContactRole> CourseContacts = [select OpportunityID, ContactID from OpportunityContactRole where OpportunityID in :QuotetoOpportunityIDMap.keySet() ];


    for (OpportunityContactRole ocr : CourseContacts) {
        //puts the contact roles in the map with the Opportunity ID as the key
        OppIDtoContactRolesMap.put(ocr.ContactId,ocr);
    }          
List<Course__c> Course = new List<Course__c>();
    for (Quote record: Trigger.New)
         if( record.Adoption_Agreement_Sent__c == date.today())
{

Course.add (new Course__c(
                     Contact__c =  [Select Id From Contact Where ID IN : OppIDtoContactRolesMap.keySet()],
                     Adoption_Agreement__c = record.Id,
                     Opportunity__c = record.OpportunityID));
         }
   insert course;
 }

Best Answer chosen by Admin (Salesforce Developers) 
Coco_SdyneyCoco_Sdyney

 OppIDtoContactRolesMap.get(Contact.id) will return an opportunitycontactrole, and OppIDtoContactRolesMap.put(Contact.id) syntax is wrong.

 

Try code something as below:

Map<String, Quote> QuotetoOpportunityIDMap = new Map<String, Quote>();

for( Quote record : Trigger.new )
{
if( record.Adoption_Agreement_Sent__c == date.today())

{
QuotetoOpportunityIDMap.put( record.OpportunityID, record );
}

}


map<Id, Set<Id>> OppIDtoContactIdsMap = new map<Id, set<Id>>();
List<OpportunityContactRole> CourseContacts = [select OpportunityID, ContactID from OpportunityContactRole where OpportunityID in :QuotetoOpportunityIDMap.keySet() ];

for (OpportunityContactRole ocr : CourseContacts) {
set<Id> set_conIds = new set<Id>();

if(OppIDtoContactIdsMap.containsKey(ocr.OpportunityId))
{
set_conIds = OppIDtoContactIdsMap.get(ocr.OpportunityId);
set_conIds.add(ocr.ContactId);
OppIDtoContactIdsMap.put(ocr.opportunityId, set_conIds);
}
else
{
set_conIds = new set<Id>();
set_conIds.add(ocr.ContactId);
OppIDtoContactIdsMap.put(ocr.opportunityId, set_conIds);
}

}

List<Course__c> list_newCourse = new List<Course__c>();

for (Quote record: Trigger.New)
{
if(record.Adoption_Agreement_Sent__c == date.today())
{
if(OppIDtoContactIdsMap.containsKey(record.OpportunityId))
{
for(Id conId: OppIDtoContactIdsMap.get(record.OpportunityId))
{
Course__c newCourse = new Course__c();
newCourse.Contact__c = conId;
newCourse.Adoption_Agreement__c = record.Id;
newCourse.Opportunity__c = record.OpportunityId;
list_newCourse.add(newCourse);
}
}
}

}
insert list_newCourse;

 

 

 

All Answers

Coco_SdyneyCoco_Sdyney

In your Course__c object , is expecting Contact__c field to receive a ContactId, but what you provided is [Select Id from Contact where Id in: ...], this is a List<Contact>, will not be accepted by Contact__c field.

 

 

SFDCDevQASFDCDevQA

I've tried using

Contact__c =  OppIDtoContactRolesMap.get(Contact.id), which gives me the error of Compile Error: Incompatible key type Schema.SObjectField for MAP<Id,OpportunityContactRole> at line 36 column 36

 

and

Contact__c =  OppIDtoContactRolesMap.put(Contact.id), which gives me the error of Compile Error: Method does not exist or incorrect signature: [MAP<Id,OpportunityContactRole>].put(Schema.SObjectField) at line 36 column 36

Coco_SdyneyCoco_Sdyney

 OppIDtoContactRolesMap.get(Contact.id) will return an opportunitycontactrole, and OppIDtoContactRolesMap.put(Contact.id) syntax is wrong.

 

Try code something as below:

Map<String, Quote> QuotetoOpportunityIDMap = new Map<String, Quote>();

for( Quote record : Trigger.new )
{
if( record.Adoption_Agreement_Sent__c == date.today())

{
QuotetoOpportunityIDMap.put( record.OpportunityID, record );
}

}


map<Id, Set<Id>> OppIDtoContactIdsMap = new map<Id, set<Id>>();
List<OpportunityContactRole> CourseContacts = [select OpportunityID, ContactID from OpportunityContactRole where OpportunityID in :QuotetoOpportunityIDMap.keySet() ];

for (OpportunityContactRole ocr : CourseContacts) {
set<Id> set_conIds = new set<Id>();

if(OppIDtoContactIdsMap.containsKey(ocr.OpportunityId))
{
set_conIds = OppIDtoContactIdsMap.get(ocr.OpportunityId);
set_conIds.add(ocr.ContactId);
OppIDtoContactIdsMap.put(ocr.opportunityId, set_conIds);
}
else
{
set_conIds = new set<Id>();
set_conIds.add(ocr.ContactId);
OppIDtoContactIdsMap.put(ocr.opportunityId, set_conIds);
}

}

List<Course__c> list_newCourse = new List<Course__c>();

for (Quote record: Trigger.New)
{
if(record.Adoption_Agreement_Sent__c == date.today())
{
if(OppIDtoContactIdsMap.containsKey(record.OpportunityId))
{
for(Id conId: OppIDtoContactIdsMap.get(record.OpportunityId))
{
Course__c newCourse = new Course__c();
newCourse.Contact__c = conId;
newCourse.Adoption_Agreement__c = record.Id;
newCourse.Opportunity__c = record.OpportunityId;
list_newCourse.add(newCourse);
}
}
}

}
insert list_newCourse;

 

 

 

This was selected as the best answer
SFDCDevQASFDCDevQA

This is the wording and order that ended up working.

Thanks so much,

Amanda

 

trigger QuoteAdoptionAgreementSentCreateCourses on Quote (after update) {

  // gather quotes with agreement sent and their related opptyids
  List<Quote> quotesWithAgreementSent = new List<Quote>();
  Set<Id> relatedOpptyIds = new Set<Id>();
  for(Quote quote : Trigger.new) {
    if(quote.Adoption_Agreement_Sent__c == Date.today()) {
      relatedOpptyIds.add(quote.opportunityId);
      quotesWithAgreementSent.add(quote);
    }
  }

  // query contacts related to opportunities and map by oppty id
  Map<Id, Set<Id>> contactsByOpptyMap = new Map<Id, Set<Id>>();
  for(OpportunityContactRole ocr : [
    select opportunityId, contactId
    from OpportunityContactRole
    where opportunityId in :relatedOpptyIds
  ]) {
    if(!contactsByOpptyMap.containsKey(ocr.opportunityId)) {
      contactsByOpptyMap.put(ocr.opportunityId, new Set<Id>());
    }
    contactsByOpptyMap.get(ocr.opportunityId).add(ocr.contactId);
  }

  // create course records
  List<Course__c> courses = new List<Course__c>();
  for(Quote quote : quotesWithAgreementSent) {
    if(contactsByOpptyMap.containsKey(quote.opportunityId)) {
      for(Id contactId : contactsByOpptyMap.get(quote.opportunityId)) {
        courses.add(new Course__c(
            contact__c = contactId
          , adoption_agreement = quote.id
          , opportunity = quote.opportunityId
        ));
      }
    }
  }
  insert courses;
}