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
jcohanallroundsjcohanallrounds 

Trigger question / error

I am trying to write a basic trigger to update the value of a field CliffLength__c with the following:

 

trigger FillInVestingValues on SecurityTransaction__c ( before update) {
          
     SecurityTransaction__c[] secTransactions = [select CliffLength__c from SecurityTransaction__c where CliffLength__c = null];
    for(SecurityTransaction__c onetransaction : secTransactions) {
         onetransaction.CliffLength__c = 6.2;
    }
  update secTransactions;
}

 

 

when I try to save the record expecting CliffLength field to get the value 6.2 I get the following error, what am I doing wrong?

 

Apex trigger FillInVestingValues caused an unexpected exception, contact your administrator: FillInVestingValues: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id a0K50000000D2wYEAS; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = a0K50000000D2wY) is currently in trigger FillInVestingValues, therefore it cannot recursively update itself: Trigger.FillInVestingValues: line 7, column 3

Best Answer chosen by Admin (Salesforce Developers) 
BritishBoyinDCBritishBoyinDC

Not quite sure what you're trying to do, but it looks like you are trying to set CliffLength to 6.2 if CliffLength is null for the record[s] being updated. If so, your code won't work because it is trying to update the same values as the trigger is processing. The values being processed by the trigger are referenced by a standard sObject array called trigger.new. So rather than create a second list, you can just code it like this:

 

 

trigger FillInVestingValues on SecurityTransaction__c ( before update) { for (SecurityTransaction__c sc : Trigger.New) {//if no CliffLength__c was supplied, set to 6.2 if (c.CliffLength__c == NULL) { //set the Cliff Length value to the default c.CliffLength__c = 6.2;

}}}

 

 

 

All Answers

BritishBoyinDCBritishBoyinDC

Not quite sure what you're trying to do, but it looks like you are trying to set CliffLength to 6.2 if CliffLength is null for the record[s] being updated. If so, your code won't work because it is trying to update the same values as the trigger is processing. The values being processed by the trigger are referenced by a standard sObject array called trigger.new. So rather than create a second list, you can just code it like this:

 

 

trigger FillInVestingValues on SecurityTransaction__c ( before update) { for (SecurityTransaction__c sc : Trigger.New) {//if no CliffLength__c was supplied, set to 6.2 if (c.CliffLength__c == NULL) { //set the Cliff Length value to the default c.CliffLength__c = 6.2;

}}}

 

 

 

This was selected as the best answer
jcohanallroundsjcohanallrounds
Thanks I did not realize that Trigger.new was records that were not just 'new' but ones that had been updated as well.  I appreciate the help. it worked.
BritishBoyinDCBritishBoyinDC
Glad it worked. Also, FYI - trigger.old is available in updates if you want to compare the value before the update to one you are about to change it to...
jcohanallroundsjcohanallrounds

That is pretty sweet.  There is definately a learning curve but at the same time the system is quite powerful.

 

Thanks again.