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
Evgeny ZhdanovEvgeny Zhdanov 

Opportunity Contact Role (auto-creation) fires Trigger not in the bulk context

There is an out-of-the-box feature that creates an Opportunity Contact Role in case of Opportunity.ContactId isn't blank.
I have a trigger on OpportunityContactRole that performs two SOQL queries and sets Opportunity Details based on Contact.
If I try to insert 51 Opportunity (insert List<Opportunity>) it will fire the SOQL 101 error. The reason is that the out-of-the-box feature creates Opportunity Contact Roles one by one in the same transaction (not as a bulk of records). Is there any workaround?
SwethaSwetha (Salesforce Developers) 
HI Evgeny,
You mentioned that you have a trigger on OpportunityContactRole that performs two SOQL queries and sets Opportunity Details based on Contact. It would be helpful to understand the specifics of your trigger logic to provide a workaround or optimization suggestions.

On a general note you can use a future method to create Opportunity Contact Roles asynchronously, which will allow you to avoid hitting the SOQL query limit

You can use a batch process to create Opportunity Contact Roles in batches, which will also help you avoid hitting the SOQL query limit

You can create a trigger on the Opportunity object to create Opportunity Contact Roles, which will allow you to control the order of execution and avoid hitting the SOQL query limit

Related:
https://salesforce.stackexchange.com/questions/296280/automatically-create-contact-role-on-opportunity-creation
https://www.salesforceben.com/the-drip/introduction-to-salesforce-opportunity-contact-roles/

Thanks
Evgeny ZhdanovEvgeny Zhdanov

There is standard SF feature that creates an Opportunity Contact Role in case of Opportunity.ContactId isn't blank. And if try to insert, in my case, 51 Opportunity with ContactId is not blank it will hit the governor limits SOQL 101.
NOTE: I have only one trigger on OpportunityContactRole (see below). I don't have any triggers, flows, or processes on the Opportunity object.

Trigger on OpportunityContactRole:

trigger OpportunityContactRoleTrigger on OpportunityContactRole (after insert) {
    System.debug('es debug opportunity contact role trigger size ' + Trigger.new.size());
    Set<Id> opportynityIds = new Set<Id>();
    Set<Id> contactIds = new Set<Id>();
    for (OpportunityContactRole ocr :Trigger.new) {
        if (ocr.IsPrimary) {
            opportynityIds.add(ocr.OpportunityId);
            contactIds.add(ocr.ContactId);
        }
    }
    if (opportynityIds.size() > 0 && contactIds.size() > 0) {
        List<Opportunity> opportunities = new List<Opportunity>([
            SELECT Id
            FROM Opportunity
            WHERE Id IN :opportynityIds
        ]);
        List<Contact> contacts = new List<Contact>([
            SELECT Id
            FROM Contact
            WHERE Id IN :contactIds
        ]);
    }
}

For instance. I execute the following code to insert 51 Opportunities:

List<Opportunity> opportunitiesToInsert = new List<Opportunity>();
Account acc = [select id from Account LIMIT 1];
Contact con = [select id from Contact LIMIT 1];
for (Integer i = 0; i < 51; i++) {
    opportunitiesToInsert.add(
        new Opportunity(
            RecordTypeId = '0125f000001quzZAAQ',
            Name = 'Test Opp ' + i + ' ' + DateTime.now(),
            AccountId = acc.Id,
            ContactId = con.Id,
            CloseDate = Date.today(),
            StageName = 'Open'
        )
    );
}
insert opportunitiesToInsert

In Debug Logs I see the following:

User-added image

It means that the standard SF feature inserts 51 Opportunity Contact Roles one by one in the same transaction (not as a bulk of records). You can try to reproduce the same at your dev org.