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
S SaiS Sai 

Automatically assign contact roles to opportunities

AFTER LEAD CONVERT IN OPPORTUNITY RECORD CREATE TWO CONACT ROLE RECORDS USING THIS TRIGGER.

I WANT TO CREATE ONLY ONE RECORD .

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 = FALSE;
                    firstContact = FALSE;
                }
                lstOCR.add(ocr);
            }
        }
    }
    insert lstOCR;
}

THANKS IN ADVANCED 
 
Vivian Charlie 1208Vivian Charlie 1208

Hi,

 

As an immediate solution I would recommend you run the trigger only for after update and remove the after insert scenario. Please test after that and confirm if now only 1 record in created instead of 2. If Yes, then look for workflows/process builders/triggers that maybe firing on Opportunity insert and updating it in the backend.

If you need to use both insert and update then use a static boolean variable to handle that the trigger only fires for insert and not updates.

public class P {
    public static boolean firstRun = true;
}

trigger T1 on Account (after insert, after update) {
    if(Trigger.isInsert){
        if(Trigger.isUpdate){
            if(p.firstRun){
                // your functionality here
                p.firstRun=false;
              }
        }
    }
}

 

Thanks

Vivian

S SaiS Sai
Not woring 
Vivian Charlie 1208Vivian Charlie 1208

Hi,

Did you try the 1st approach I suggested?

I would recommend you run the trigger only for after update and remove the after insert scenario. Does this scenario create 2 records?

 

Thanks

Vivian