0

Need some help on an issue that I am currently facing in a trigger. This piece of code is getting executed in the "Before Insert" action of the 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 : trigger.new) {
        if(oppty.AccountId != null) {
            lstAccountIds.add(oppty.AccountId);
        }
    }
    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 : trigger.new)  {
        String strAcctOwnerId = ''; 
        if (oppty.AccountId != null && strRecordTypes.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?

4
  • 1
    Could you edit the question and add the detail as to what event and phase this code is executed against? (e.g. "before insert").
    – Phil W
    Commented Apr 16, 2020 at 22:01
  • This code should not be failing as is. Are you sure this is where the error occurs? What else is going on? Commented Apr 16, 2020 at 22:26
  • Little curious do you have any multiple triggers on the opportunity object?
    – Pavan tej
    Commented Apr 17, 2020 at 8:57
  • Provide more code like ..event ..if it's running in after event - it will throw error.
    – Ayub
    Commented Apr 17, 2020 at 10:34

1 Answer 1

0

Thanks for your responses. 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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .