• Patricia Mozzoni 9
  • NEWBIE
  • 10 Points
  • Member since 2020
  • System Admin
  • Parallon

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 3
    Replies
Thanks to some help from this forum, I was able to get my first trigger written, tested and in production however, now it does not work!  Looking at comments in the forum, perhaps it is something to do with recursion which I am unsure how to fix.  I have tried to fix all day and now my trigger is  not working and the error I get is:

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, oliUpdate: execution of AfterInsert

caused by: System.DmlException: Update failed. First exception on row 0 with id 00k2i000003mRZaAAM; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, oliUpdate: maximum trigger depth exceeded
OpportunityLineItem trigger event AfterInsert
OpportunityLineItem trigger event AfterUpdate
OpportunityLineItem trigger event AfterUpdate
OpportunityLineItem trigger event AfterUpdate
OpportunityLineItem trigger event AfterUpdate
OpportunityLineItem trigger event AfterUpdate
OpportunityLineItem trigger event AfterUpdate
OpportunityLineItem trigger event AfterUpdate
OpportunityLineItem trigger event AfterUpdate
OpportunityLineItem trigger event AfterUpdate
OpportunityLineItem trigger event AfterUpdate
OpportunityLineItem trigger event AfterUpdate
OpportunityLineItem trigger event AfterUpdate
OpportunityLineItem trigger event AfterUpdate
OpportunityLineItem trigger event AfterUpdate
OpportunityLineItem trigger event AfterUpdate: []

Trigger.oliUpdate: line 21, column 1: []

I would really appreciate any help as I am supposed to have this live this week, ugh!

 I have a formula field (Net_Amount__c) on OpportunityLineItem that calculates my UnitPrice.  The formula for Net_Amount__c is  ((Quantity * Annual_Amount__c) * Revenue_Years__c).  Because the field I want to update is the UnitPrice, I have to do an after trigger that calculates the formula field value of Net_Amount__c and updates UnitPrice. 



Trigger:
trigger oliUpdate on OpportunityLineItem (after insert, after update) {

    Set <String> oliID = New Set <String> (); 
    For (OpportunityLineItem oli: Trigger.new) { 
        if (oli.OpportunityId != Null ) { 
        oliID.add (oli.Id); 
        } 
    } 
    If (oliID.size ()> 0) { 
        List<OpportunityLineItem> upOpiList = new List <OpportunityLineItem>();
        For (OpportunityLineItem ol: [SELECT Id, OpportunityID, Product2Id, PricebookEntryId, Quantity, Net_Amount__c, UnitPrice FROM OpportunityLineItem WHERE id in: oliID AND Net_Amount__c >0.00]){
            
      
             Formula.recalculateFormulas(new List<OpportunityLineItem>{ol});
                ol.UnitPrice = ol.Net_Amount__c; 
                UpOpiList.add (ol); 
            }
           
        
        If (upOpiList.size ()> 0) 
            update upOpiList; 
}
}

Test:
@isTest 
public class oliUpdateTest 
{
     static testMethod void TestoliUpdate()
    {
        
        List<Account> testAccounts = new List<Account>();
        Account testAccount0 = new Account(
            Name = 'Arbor'
        );
        testAccounts.add(testAccount0);
        Account testAccount1 = new Account(
            Name = 'Arbor Villa'
        );
        testAccounts.add(testAccount1);
        insert testAccounts;

   
        Opportunity testOpportunity = new Opportunity(
            Name                 = 'Arbor - Cypress Tree Opportunity',
            StageName            = 'Strategy',
            CloseDate            = Date.today(),
            AccountId            = testAccount0.Id
            
        );
          insert testOpportunity;

        Product2 testProduct2 = new Product2(
            Name                  = 'Cypress Tree',
            IsActive              = true,
            CanUseRevenueSchedule = true
        );
        insert testProduct2;

 

       

        PricebookEntry testPricebookEntry = new PricebookEntry(
            IsActive     = true,
            Pricebook2Id = Test.getStandardPricebookId(),
            Product2Id   = testProduct2.Id,
            UnitPrice    = 0
        );
        insert testPricebookEntry;

        OpportunityLineItem testOpportunityLineItem = new OpportunityLineItem(
            OpportunityId          = testOpportunity.Id,
            PricebookEntryId       = testPricebookEntry.Id,
            Quantity               = 1,
            Revenue_Years__c       = 3,
            Annual_Amount__c       = 5000,
            ServiceDate            = Date.newInstance(2020, 1, 1)
        );
        insert testOpportunityLineItem;   
        System.assertEquals(NULL, testOpportunityLineItem.Net_Amount__c, 'should be null, formula is not evaluated yet');
        
        Test.startTest();
        
        OpportunityLineItem item = [SELECT Id, Net_Amount__c, UnitPrice
                                  FROM OpportunityLineItem WHERE Id = :testOpportunityLineItem.Id];
        Formula.recalculateFormulas(new List<OpportunityLineItem>{item});
        Test.stopTest();
      
       
       System.assertNotEquals(0, testOpportunityLineItem.UnitPrice, 'UnitPrice should equal Net_Amount__c');
 

}
}

​​​​​​​
 
Thanks for taking the time to read this! This is my first trigger and the purpose is to update the UnitPrice field on Opportunity Line Items with the value of a formula field Net_Amount__c.  I don't even understand the error message I received and will share below.

Greatly appreciate any help!

My Trigger:
 
trigger oliUpdate on OpportunityLineItem (after insert, after update) {

    Set <String> oliID = New Set <String> (); 
    For (OpportunityLineItem oli: Trigger.new) { 
        if (oli.OpportunityId != Null ) { 
        oliID.add (oli.Id); 
        } 
    } 
    If (oliID.size ()> 0) { 
        List <OpportunityLineItem> upOpiList = new List <OpportunityLineItem> (); 
        For (OpportunityLineItem ol: 
[SELECT Id, Net_Amount__c, UnitPrice FROM OpportunityLineItem WHERE id in: oliID AND Net_Amount__c > 0.0]) { 
            ol.UnitPrice = ol.Net_Amount__c; 
            UpOpiList.add (ol); 
        } 
        If (upOpiList.size ()> 0) 
            update upOpiList; 
    } 
}

User-added image
Thanks to some help from this forum, I was able to get my first trigger written, tested and in production however, now it does not work!  Looking at comments in the forum, perhaps it is something to do with recursion which I am unsure how to fix.  I have tried to fix all day and now my trigger is  not working and the error I get is:

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, oliUpdate: execution of AfterInsert

caused by: System.DmlException: Update failed. First exception on row 0 with id 00k2i000003mRZaAAM; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, oliUpdate: maximum trigger depth exceeded
OpportunityLineItem trigger event AfterInsert
OpportunityLineItem trigger event AfterUpdate
OpportunityLineItem trigger event AfterUpdate
OpportunityLineItem trigger event AfterUpdate
OpportunityLineItem trigger event AfterUpdate
OpportunityLineItem trigger event AfterUpdate
OpportunityLineItem trigger event AfterUpdate
OpportunityLineItem trigger event AfterUpdate
OpportunityLineItem trigger event AfterUpdate
OpportunityLineItem trigger event AfterUpdate
OpportunityLineItem trigger event AfterUpdate
OpportunityLineItem trigger event AfterUpdate
OpportunityLineItem trigger event AfterUpdate
OpportunityLineItem trigger event AfterUpdate
OpportunityLineItem trigger event AfterUpdate
OpportunityLineItem trigger event AfterUpdate: []

Trigger.oliUpdate: line 21, column 1: []

I would really appreciate any help as I am supposed to have this live this week, ugh!

 I have a formula field (Net_Amount__c) on OpportunityLineItem that calculates my UnitPrice.  The formula for Net_Amount__c is  ((Quantity * Annual_Amount__c) * Revenue_Years__c).  Because the field I want to update is the UnitPrice, I have to do an after trigger that calculates the formula field value of Net_Amount__c and updates UnitPrice. 



Trigger:
trigger oliUpdate on OpportunityLineItem (after insert, after update) {

    Set <String> oliID = New Set <String> (); 
    For (OpportunityLineItem oli: Trigger.new) { 
        if (oli.OpportunityId != Null ) { 
        oliID.add (oli.Id); 
        } 
    } 
    If (oliID.size ()> 0) { 
        List<OpportunityLineItem> upOpiList = new List <OpportunityLineItem>();
        For (OpportunityLineItem ol: [SELECT Id, OpportunityID, Product2Id, PricebookEntryId, Quantity, Net_Amount__c, UnitPrice FROM OpportunityLineItem WHERE id in: oliID AND Net_Amount__c >0.00]){
            
      
             Formula.recalculateFormulas(new List<OpportunityLineItem>{ol});
                ol.UnitPrice = ol.Net_Amount__c; 
                UpOpiList.add (ol); 
            }
           
        
        If (upOpiList.size ()> 0) 
            update upOpiList; 
}
}

Test:
@isTest 
public class oliUpdateTest 
{
     static testMethod void TestoliUpdate()
    {
        
        List<Account> testAccounts = new List<Account>();
        Account testAccount0 = new Account(
            Name = 'Arbor'
        );
        testAccounts.add(testAccount0);
        Account testAccount1 = new Account(
            Name = 'Arbor Villa'
        );
        testAccounts.add(testAccount1);
        insert testAccounts;

   
        Opportunity testOpportunity = new Opportunity(
            Name                 = 'Arbor - Cypress Tree Opportunity',
            StageName            = 'Strategy',
            CloseDate            = Date.today(),
            AccountId            = testAccount0.Id
            
        );
          insert testOpportunity;

        Product2 testProduct2 = new Product2(
            Name                  = 'Cypress Tree',
            IsActive              = true,
            CanUseRevenueSchedule = true
        );
        insert testProduct2;

 

       

        PricebookEntry testPricebookEntry = new PricebookEntry(
            IsActive     = true,
            Pricebook2Id = Test.getStandardPricebookId(),
            Product2Id   = testProduct2.Id,
            UnitPrice    = 0
        );
        insert testPricebookEntry;

        OpportunityLineItem testOpportunityLineItem = new OpportunityLineItem(
            OpportunityId          = testOpportunity.Id,
            PricebookEntryId       = testPricebookEntry.Id,
            Quantity               = 1,
            Revenue_Years__c       = 3,
            Annual_Amount__c       = 5000,
            ServiceDate            = Date.newInstance(2020, 1, 1)
        );
        insert testOpportunityLineItem;   
        System.assertEquals(NULL, testOpportunityLineItem.Net_Amount__c, 'should be null, formula is not evaluated yet');
        
        Test.startTest();
        
        OpportunityLineItem item = [SELECT Id, Net_Amount__c, UnitPrice
                                  FROM OpportunityLineItem WHERE Id = :testOpportunityLineItem.Id];
        Formula.recalculateFormulas(new List<OpportunityLineItem>{item});
        Test.stopTest();
      
       
       System.assertNotEquals(0, testOpportunityLineItem.UnitPrice, 'UnitPrice should equal Net_Amount__c');
 

}
}

​​​​​​​
 
Thanks for taking the time to read this! This is my first trigger and the purpose is to update the UnitPrice field on Opportunity Line Items with the value of a formula field Net_Amount__c.  I don't even understand the error message I received and will share below.

Greatly appreciate any help!

My Trigger:
 
trigger oliUpdate on OpportunityLineItem (after insert, after update) {

    Set <String> oliID = New Set <String> (); 
    For (OpportunityLineItem oli: Trigger.new) { 
        if (oli.OpportunityId != Null ) { 
        oliID.add (oli.Id); 
        } 
    } 
    If (oliID.size ()> 0) { 
        List <OpportunityLineItem> upOpiList = new List <OpportunityLineItem> (); 
        For (OpportunityLineItem ol: 
[SELECT Id, Net_Amount__c, UnitPrice FROM OpportunityLineItem WHERE id in: oliID AND Net_Amount__c > 0.0]) { 
            ol.UnitPrice = ol.Net_Amount__c; 
            UpOpiList.add (ol); 
        } 
        If (upOpiList.size ()> 0) 
            update upOpiList; 
    } 
}

User-added image