• Ashley 6
  • NEWBIE
  • 30 Points
  • Member since 2018

  • Chatter
    Feed
  • 1
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
Hi, I'm New to Apex...

I'd like to create a Trigger that fires when --> I create an Opportunity record on a custom object. The Custom object has a list of related Contact record. I would like the Trigger to automatically create  Opportunity Contact Roles, each Contact Roles with the related Contact record (related to the custom object) as the Contact Id, and the newly created Opportunity record as the Opportunity Id. 

The custom object is almost use as in an Account (custom object)--Opportunity--Contact format. 

The custom recod field is --> a__Co_Sum__c

Provided below is fairly similar Apex trigger, but it is design for the Account. Where the trigger will add all account contacts to the contact roles when creating a new opportunity. I'd love to have this code written but replacing Account with the custom record a__Co_Sum__c
trigger CreateContactRole on Opportunity (after insert, after update) {

    //get the id of all involved accounts
    Set<ID> accountIds = new Set<ID>();
    for(Opportunity opt:Trigger.New){
        accountIds.add(opt.AccountId);
    }
    
    //get all contacts for those accounts
    list<Contact> contacts = new list<Contact>();
    contacts = [select id, AccountId from Contact where AccountId in: accountIds order by createddate Limit 5000];
    
    //organize these contacts by account
    Map<Id,List<Contact>> contactsByAccount = new Map<ID,List<Contact>>();
    for(Contact c:contacts){
        if(contactsByAccount.get(c.AccountId) == null){
            contactsByAccount.put(c.AccountId,new List<Contact>());
        }
        contactsByAccount.get(c.AccountId).add(c);
    }
    
    // check to see if the Opportunity already has a contact role.  If it does, add to a set of Ids to exclude
    List<OpportunityContactRole> existingOCR = new List<OpportunityContactRole>();
    Set<Id> existingOCRIds = new Set<Id>();
    existingOCR = [select OpportunityId from OpportunityContactRole where OpportunityId in:Trigger.newMap.keySet() limit 5000];
    for(OpportunityContactRole ocr:existingOCR) if(!existingOCRIds.contains(ocr.OpportunityId)) existingOCRIds.add(ocr.OpportunityId);
    
    //create the OpportunityContactRole objects
    list<OpportunityContactRole> lstOCR = new list<OpportunityContactRole>();
    for(Opportunity opt:Trigger.New){
        if(!existingOCRIds.contains(opt.Id) && contactsByAccount.get(opt.AccountId) != null){
            Boolean firstContact = true;
            for(Contact c: contactsByAccount.get(opt.AccountId)){
                OpportunityContactRole ocr = new OpportunityContactRole(OpportunityId=opt.id, ContactId=c.id);
                if(firstContact) {
                    ocr.IsPrimary = TRUE;
                    firstContact = FALSE;
                }
                lstOCR.add(ocr);
            }
        }
    }
    insert lstOCR;
}

 
  • May 01, 2018
  • Like
  • 0
Hi, I'm New to Apex...

I'd like to create a Trigger that fires when --> I create an Opportunity record on a custom object. The Custom object has a list of related Contact record. I would like the Trigger to automatically create  Opportunity Contact Roles, each Contact Roles with the related Contact record (related to the custom object) as the Contact Id, and the newly created Opportunity record as the Opportunity Id. 

The custom object is almost use as in an Account (custom object)--Opportunity--Contact format. 

The custom recod field is --> a__Co_Sum__c

Provided below is fairly similar Apex trigger, but it is design for the Account. Where the trigger will add all account contacts to the contact roles when creating a new opportunity. I'd love to have this code written but replacing Account with the custom record a__Co_Sum__c
trigger CreateContactRole on Opportunity (after insert, after update) {

    //get the id of all involved accounts
    Set<ID> accountIds = new Set<ID>();
    for(Opportunity opt:Trigger.New){
        accountIds.add(opt.AccountId);
    }
    
    //get all contacts for those accounts
    list<Contact> contacts = new list<Contact>();
    contacts = [select id, AccountId from Contact where AccountId in: accountIds order by createddate Limit 5000];
    
    //organize these contacts by account
    Map<Id,List<Contact>> contactsByAccount = new Map<ID,List<Contact>>();
    for(Contact c:contacts){
        if(contactsByAccount.get(c.AccountId) == null){
            contactsByAccount.put(c.AccountId,new List<Contact>());
        }
        contactsByAccount.get(c.AccountId).add(c);
    }
    
    // check to see if the Opportunity already has a contact role.  If it does, add to a set of Ids to exclude
    List<OpportunityContactRole> existingOCR = new List<OpportunityContactRole>();
    Set<Id> existingOCRIds = new Set<Id>();
    existingOCR = [select OpportunityId from OpportunityContactRole where OpportunityId in:Trigger.newMap.keySet() limit 5000];
    for(OpportunityContactRole ocr:existingOCR) if(!existingOCRIds.contains(ocr.OpportunityId)) existingOCRIds.add(ocr.OpportunityId);
    
    //create the OpportunityContactRole objects
    list<OpportunityContactRole> lstOCR = new list<OpportunityContactRole>();
    for(Opportunity opt:Trigger.New){
        if(!existingOCRIds.contains(opt.Id) && contactsByAccount.get(opt.AccountId) != null){
            Boolean firstContact = true;
            for(Contact c: contactsByAccount.get(opt.AccountId)){
                OpportunityContactRole ocr = new OpportunityContactRole(OpportunityId=opt.id, ContactId=c.id);
                if(firstContact) {
                    ocr.IsPrimary = TRUE;
                    firstContact = FALSE;
                }
                lstOCR.add(ocr);
            }
        }
    }
    insert lstOCR;
}

 
  • May 01, 2018
  • Like
  • 0