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
Temple SutfinTemple Sutfin 

Trigger doesnt fire on update of record

I have a trigger that works awesome when I insert a record.  The problem is when I update the parent record, the child records do not populate.

I am quite new to triggers.

 

The scenario:

   A Renewal may / may not be ammoritzed over "x" amount of months.

   If renewal is amoritzed, then "Number of months to amortize over" must be entered. (Validation rule covers this)

   On save, the trigger inserts the number of child records into Renewal Payments equal to the number of months to amoritze over.

 

The problem:

    This works fantastic if I am inserting a new record.  But if later, I decide to edit an existing record to amortize over, the trigger doesnt fire.  I know that this is because of how I have my current trigger defined (see below) but I dont get how to do an after (or before?) update to solve the problem.  Can someone help me out?

 

Existing trigger:

trigger renewalAmortPayments on Renewal_Year__c (after insert) {
   List<Renewal_Payment__c> listInsertPayments = new List<Renewal_Payment__c>();
   for(Renewal_Year__c objPayment:Trigger.New) {
       if(objPayment.Number_of_Months_to_Amortize_over__c != NULL) {
           Decimal decVal = objPayment.Number_of_Months_to_Amortize_over__c -1;
           Integer iCount = Integer.ValueOf(decVal);
           for(integer i = 0; i <= iCount; i++){
               Renewal_Payment__c objRenPay = New Renewal_Payment__c();
               objRenPay.Renewal_Year__c = objPayment.id;
               objRenPay.Payment_Number__c = i;
               listInsertPayments.add(objRenPay);
           }
       }
    }
    if(listInsertPayments.size()>0) { insert listInsertPayments; }
}

 Some extra info:

 

 There is a date calc field on the existing child object that requires me to use the "Decimal decVal = objPayment.Number_of_Months_to_Amortize_over__c -1;" line.  I need to be able to enter the records on INSERT of original record on UPDATE of existing Renewal_Year__c record, and possibly to delete Renewal_Payments__c records if there is an error discovered after user saves initial record.

 

Anyone have some help for me?  I would appreciate it. 

 

Thanks,

Temple

SimonJaiSimonJai
trigger renewalAmortPayments on Renewal_Year__c ( after insert, after update ) {

You have only specified the trigger to work after an insert, not an update. You'll need to add "after update".

Temple SutfinTemple Sutfin

SimonJai,

 

  That is what I thought too, but when I have the "after update" in the trigger I get this error when I save the record:

 

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger renewalAmortPayments caused an unexpected exception, contact your administrator: renewalAmortPayments: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, renewalAmortPayments: maximum trigger depth exceeded Renewal_Year trigger event AfterUpdate for [a23J0000000CzVM] Renewal_Year trigger event AfterUpdate for [a23J0000000CzVM] Renewal_Year trigger event AfterUpdate for [a23J0000000CzVM] Renewal_Year trigger event AfterUpdate for [a23J0000000CzVM] Renewal_Year trigger event AfterUpdate for [a23J0000000CzVM] Renewal_Year trigger event AfterUpdate for [a23J0000000CzVM] Renewal_Year trigger event AfterUpdate for [a23J0000000CzVM] Renewal_Year trigger event AfterUpdate for [a23J0000000CzVM] Renewal_Year trigger event AfterUpdate for [a23J0000000CzVM] Renewal_Year trigger event AfterUpdate for [a23J0000000CzVM] Renewal_Year trigger event AfterUpdate for [a23J0000000CzVM] Renewal_Year trigger event AfterUpdate for [a23J0000000CzVM] Renewal_Year trigger event AfterUpdate for [a23J0000000CzVM] Renewal_Year trigger event AfterUpdate for [a23J0000000CzVM] Renewal_Year trigger event AfterUpdate for [a23J0000000CzVM] Renewal_Year trigger event AfterUpdate for [a23J0000000CzVM]: []: Trigger.renewalAmortPayments: line 15, column 1

 

(I should have mentioned that before, I apologize).

Temple SutfinTemple Sutfin

I have also come across another problem.  I wrote a custom Clone button for my Renewal_Year__c object.  When I clone the record using the custom clone, and then save it, the trigger doesnt fire.  I do not understand why this is happening.  Do I need to have three separate triggers here?  One to handle an update, one to handle an insert, and one to handle an insert after clone?

SimonJaiSimonJai
"CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY" indicates the user does not have the correct permission for the object.

http://kb.omni-ts.com/entry/68/
Jeff MayJeff May

I think you might be running into a scenario where you are updating a Detail record of a Master-detail, which is then triggering a Master record update, which is firing the trigger again.  That is called a 'recursive trigger'. You can read about how to control them here:  http://www.salesforce.com/docs/developer/cookbook/Content/apex_controlling_recursive_triggers.htm

Temple SutfinTemple Sutfin

SimonJai-

  Its not a premissions issue, I have gone through every object to make sure that I have full CRUD capabilties.  And I am using the System Administrator Profile, so I have been able to cross that off the list of possibilities.

 

JeffM -

   I have the trigger on a master-detail relationship.  If the Master record is saved for the first time, it will insert the Detail records as defined by the trigger.  That works properly.  If I update the Master record to include the fields necessary for the trigger to fire, creating the detail records, the trigger doesnt insert the records and gives me the error i listed above.  If I clone the Master record with my custom clone button / code, the clone works properly, but the trigger does not fire to create the detail records.  I understand the idea behind recursion and the trigger tripping over itself, but not how to correct it, nor how to account for a clone.  I read through the page you provided, and I still am not sure how to fix this.  Please bear in mind that I am a new developer, and I am self-teaching 100% of what I am doing.  I appreciate all the help, but I do not know what to do in this instance.