You need to sign in to do that
Don't have an account?
E.J.R
Apex trigger only fires when developer console is open or trace flags are set.
This has happened on several occasions in the past and it's happening again today.
I created a fairly simple 'before insert' trigger for opportunity products. When I test the trigger under the administrator account, with the developer console on, the code runs perfectly as intended. But when I close the developer console window, the trigger no longer fires.
The same thing happens for different users as well. If I set a trace flag for a particular user, the trigger fires just fine when logged in as that user. But after deleting the trace flag, the trigger stops firing. I can duplicate this behavior back and forth over and over.
If anyone could point me in the right direction, I would really appreciate it. Right now it's a struggle finding any relevant info.
Note sure if this will help but here's the code for the trigger:
I created a fairly simple 'before insert' trigger for opportunity products. When I test the trigger under the administrator account, with the developer console on, the code runs perfectly as intended. But when I close the developer console window, the trigger no longer fires.
The same thing happens for different users as well. If I set a trace flag for a particular user, the trigger fires just fine when logged in as that user. But after deleting the trace flag, the trigger stops firing. I can duplicate this behavior back and forth over and over.
If anyone could point me in the right direction, I would really appreciate it. Right now it's a struggle finding any relevant info.
Note sure if this will help but here's the code for the trigger:
trigger OpportunityProductInsert on OpportunityLineItem (before insert) { // List of all line items IDs before being inserted List<Id> oliIds = new List<Id>(); // List of all opportunity IDs of the line items List<Id> oliOppIds = new List<Id>(); // List of all product IDs associated to the line items List<Id> oliProductIds = new List<Id>(); for ( OpportunityLineItem newOli : trigger.new ) { oliIds.add(newOli.Id); oliOppIds.add(newOli.OpportunityId); oliProductIds.add(newOli.Product2Id); } // Map to retrieve the account ID for each opportunity Map<Id, Id> oliAccountIdMap = new Map<Id, Id>(); List<Opportunity> opps = [SELECT Id, AccountId FROM Opportunity WHERE Id IN: oliOppIds]; for ( Opportunity opp : opps ) { oliAccountIdMap.put(opp.Id, opp.AccountId); } // List of all product IDs that are inventory items List<Id> inventoryProductIds = new List<Id>(); List<Product2> products = [SELECT Id FROM Product2 WHERE IsNonInventory__c = false AND Id IN: oliProductIds]; for ( Product2 product : products ) { inventoryProductIds.add(product.Id); } // Map to retrieve a list of available products for each account Map<Id, List<Id>> accProductMap = new Map<Id, List<Id>>(); List<Id> accProductIds; List<AccountProduct__c> accProducts = [SELECT Account__c, Product__c FROM AccountProduct__c WHERE Account__c IN: oliAccountIdMap.values()]; for ( AccountProduct__c accProduct : accProducts ) { accProductIds = new List<Id>(); if ( accProductMap.containsKey(accProduct.Account__c) ) { accProductIds = accProductMap.get(accProduct.Account__c); } accProductIds.add(accProduct.Product__c); accProductMap.put(accProduct.Account__c, accProductIds); } // Check that the product is available on the account for ( OpportunityLineItem checkOli : trigger.new ) { accProductIds = new List<Id>(); Id accId; // Retrieve the account ID from the line item ID if ( oliAccountIdMap.containsKey(checkOli.OpportunityId) ) { accId = oliAccountIdMap.get(checkOli.OpportunityId); } if ( accId != null ) { // If the product ID on the line item is in the list of inventory items if ( inventoryProductIds.contains(checkOli.Product2Id) ) { accProductIds = new List<Id>(); // Retrieve list of available products on the account if ( accProductMap.containsKey(accId) ) { accProductIds = accProductMap.get(accId); } // If the product ID is not in the list of account product IDs if ( !accProductIds.contains(checkOli.Product2Id) ) { checkOli.addError('This product isn\'t available for this account.'); } } } } }
My code is little longer but it is bulkified and must work in case of massive data. Take a look and let me know if you see any issue anywhere.
All Answers
Sure
Not sure what you mean it work fine through developer console. What are you trying in Developer Console what make you feel it is workign fine. Can you share the code?
The issue is still happening after fixing the problem with the ID above. The trigger only fires when the trace flag is set for the user. Please see the screenshots below.
1) Without the trace flag set on the user, I'm able to add the product just fine and the trigger doesn't seem to fire:
2) Here you can see the product was added to the opportunity:
3) After that, I added the trace flag for the user:
4) Once the trace flag is set, the trigger fires properly and prevents the product from being added to the opportunity:
Here's the updated trigger code:
Working trigger code:
My code is little longer but it is bulkified and must work in case of massive data. Take a look and let me know if you see any issue anywhere.