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

Trigger invocation Error

Need some help on an issue that I am currently facing in a trigger.

In the OpportunityTrigger, I am trying to assign the Account Owner as the owner of the opportunity if the opportunity has specific list of record types. My sample code is below.
List<String> strRecordTypes = new List<String>() {'Rtype 1', 'Rtype 2', 'Rtype 3'};
        List<Id> lstAccountIds = new List<Id>();
        Map<Id, Id> mapAccountOwnerIds =  new map<Id, Id>();
        for (Opportunity oppty : {
            if(oppty.AccountId != null) {
        for (Account acc : [Select a.Id, a.OwnerId FROM Account a Where Id in :lstAccountIds AND a.Owner.IsActive = true]) {
            mapAccountOwnerIds.put(acc.Id, acc.OwnerId);
        for (Opportunity oppty :  {
            String strAcctOwnerId = '';
            if (oppty.AccountId != null && MapSupportedRecType.size() > 0 && strRecordTypeNames.contains(oppty.RecordType__c)) {            
                **strAcctOwnerId = mapAccountOwnerIds.get(oppty.AccountId);**
                if(strAcctOwnerId != null && strAcctOwnerId !='') {
                    oppty.OwnerId = strAcctOwnerId;

RecordType__c is a custom field on the Opportunity object that contains the name of the Record type.
However, when I test this code, I am getting the below error at line where strAccountOwnerId is getting assigned from the map.
> USER_DEBUG []|ERROR|==========> Trigger New Invocation is [2] which exceeds the limit of [2].  The logic will not run.

Can someone help?
ANUTEJANUTEJ (Salesforce Developers) 
Hi Balaji,

Have you checked if there is any other trigger that is getting invoked when this is trigger is running??

Thanks for your response. I figured the issue. The error was not coming from the above code. There is only one trigger on the Opportunity object. We have an Apex class with a method that is getting called from 4 different triggers. To ensure that only one instance of the method is running, we developed some special code that uses a static variable which gets incremented when entering the method and gets decremented when exiting the method. Any call to the method first checks the value of the static variable before doing anything. The above code somehow broke and the value of the static  variable has become 2 and hence the debug statement was coming from that method.

The root cause of the issue appears to be using the RecordType__c which is the value of RecordType.DeveloperName where as the List has the names of the Recordtype. Once I changed the IF condition to use the variable oppty.RecordTypeName__c (another custom field on the Opportunity object which stores the RecordType.Name), the issue is resolved.

I am still not sure how the static variable for the Apex method was incremented before of the above code. If I find the issue, I will post an update.