You need to sign in to do that
Don't have an account?
Keegan
Trigger updating fields to wrong value every other time it is run
I'm working with a trigger that pulls the value from formula fields and adds it to other fields. The trigger runs when a date is changed on a parent record. The weird thing is that when I change the date the first time, the trigger works perfectly. When I change the date a second time, the trigger runs but the fields are updated with the old value instead of the new. When I change the date a third time, the trigger works perfectly again.
Example:
Date Change to 7/1
Old field value 1000, Formula value change to 1100, New field value 1100
Date Change to 7/2
Old field value 1100, Formula value change to 1200, New field value 1100
Date Change 7/3
Old field value 1100, Formula value change to 1300, New field value 1300
etc. ect.
I thought it was a problem with my trigger being a before update trigger but if that was the issue, why does it work evey other time? My trigger is below.
trigger CopyFieldsonOutstanding on Outstanding_Balance__c (before insert, before update) {
for(Outstanding_Balance__c outstanding: trigger.new) {
//Field that you want to be copied by other field keep those on left side
outstanding.Days_Drop__c = outstanding.Days__c;
outstanding.A_Interest_Past_Roll__c = outstanding.Accrued_Interest_Past__c;
outstanding.Accrued_Interest_Drop__c = outstanding.Accrued_Interest__c;
outstanding.Drop_Min__c = outstanding.MinorMax__c;
outstanding.Filter_Days_Drop__c = outstanding.Days_Filtered__c;
outstanding.Filtered_Earned_Rollup__c = outstanding.Filtered_Earned_Income__c;
outstanding.Test_Interest_Income_Rollup__c = outstanding.Test_Interest_Income__c;
outstanding.Interest_Income_Rollup__c = outstanding.Interest_Income__c;
outstanding.Interest_Past_Roll__c = outstanding.Interest_Income_Past__c;
outstanding.Rate_Drop__c = outstanding.Interest_Rate__c;
outstanding.Today__c = outstanding.Between__c;
}
}
Is something wrong with my trigger? If I need to change it to after update, can you please tell me how to test the trigger because the tests I have tried to run on it (when I tried to change it to an after update trigger) keep failing.
Thanks!
Example:
Date Change to 7/1
Old field value 1000, Formula value change to 1100, New field value 1100
Date Change to 7/2
Old field value 1100, Formula value change to 1200, New field value 1100
Date Change 7/3
Old field value 1100, Formula value change to 1300, New field value 1300
etc. ect.
I thought it was a problem with my trigger being a before update trigger but if that was the issue, why does it work evey other time? My trigger is below.
trigger CopyFieldsonOutstanding on Outstanding_Balance__c (before insert, before update) {
for(Outstanding_Balance__c outstanding: trigger.new) {
//Field that you want to be copied by other field keep those on left side
outstanding.Days_Drop__c = outstanding.Days__c;
outstanding.A_Interest_Past_Roll__c = outstanding.Accrued_Interest_Past__c;
outstanding.Accrued_Interest_Drop__c = outstanding.Accrued_Interest__c;
outstanding.Drop_Min__c = outstanding.MinorMax__c;
outstanding.Filter_Days_Drop__c = outstanding.Days_Filtered__c;
outstanding.Filtered_Earned_Rollup__c = outstanding.Filtered_Earned_Income__c;
outstanding.Test_Interest_Income_Rollup__c = outstanding.Test_Interest_Income__c;
outstanding.Interest_Income_Rollup__c = outstanding.Interest_Income__c;
outstanding.Interest_Past_Roll__c = outstanding.Interest_Income_Past__c;
outstanding.Rate_Drop__c = outstanding.Interest_Rate__c;
outstanding.Today__c = outstanding.Between__c;
}
}
Is something wrong with my trigger? If I need to change it to after update, can you please tell me how to test the trigger because the tests I have tried to run on it (when I tried to change it to an after update trigger) keep failing.
Thanks!
All Answers
Original Values
API names in order, labels in picture below): Filtered_Earned_Income__c (Formula)>Filtered_Earned_Rollup__c, Test_Interest_Income__c (Formula)>Test_Interest_Income_Rollup__c
Change First Time
Debug Log (part for record that I need updated)
(1833551807)|VARIABLE_ASSIGNMENT|[2]|outstanding|{"LastModifiedDate":"2018-05-29T17:57:20.000Z","Between__c":1.50,"Accrued_Interest__c":120575.00,"Interest_Rate__c":0.0325,"Outstanding_Principl (12 more) ...":1050000.00,"Conga_Filter__c":false,"Test_Interest_Income (3 more) ...":17631.25,"Filter_Past_Days__c":69387.50,"End_Date__c":"2018-06-30T00:00:00.000Z","Days_Filtered__c":186,"Name":"A-01022","Accrued_Interest_Pas (4 more) ...":69387.50,"Accrued_Interest_Dro (4 more) ...":120575.00,"Days_Drop__c":1272.00,"Before_Filter__c":false,"Time__c":"2018-05-29","Investment_Name__c":"a0AU000000er3SZMAY","CreatedById":"0050B000006mBBGQA2","Interest_Rate_Change (3 more) ...":"Interest End 1 / Int (14 more) ...","Scheduled_Action_Tim (4 more) ...":"2018-04-09T18:31:26.000Z","Outstanding_Balance_ (10 more) ...":1050000.00,"Interest_Past_Roll__ (1 more) ...":69387.50,"A_Interest_Past_Roll (3 more) ...":69387.50,"Start_Date__c":"2014-12-18T00:00:00.000Z","Days__c":1272,"IsDeleted":false,"Filtered_Earned_Inco (5 more) ...":17631.25,"Test_Interest_Income (10 more) ...":17536.46,"Today__c":1.50,"Interest_Income__c":120575.00,"Filter_Days_Drop__c":186.00,"Between_Dates__c":false,"Between_Ending_Dates (3 more) ...":false,"SystemModstamp":"2018-05-29T17:57:20.000Z","Interest_Income_Roll (5 more) ...":120575.00,"Drop_Min__c":1.500,"Outstanding_Principl (4 more) ...":1050000.00,"Rate_Drop__c":0.0325,"Interest_Income_Past (3 more) ...":69387.50,"CreatedDate":"2018-04-09T17:28:42.000Z","MinorMax__c":1.50,"Id":"a2j0B0000012VuEQAU","Filtered_Earned_Roll (5 more) ...":17536.46,"LastModifiedById":"0050B000006mBBGQA2"}|0x4323111e
11:57:37.827 (1833586687)|STATEMENT_EXECUTE|[2]
11:57:37.827 (1833589620)|STATEMENT_EXECUTE|[4]
11:57:37.827 (1833768528)|VARIABLE_ASSIGNMENT|[4]|this.Days_Drop__c|1272|0x4323111e
11:57:37.827 (1833777909)|STATEMENT_EXECUTE|[5]
11:57:37.827 (1833931201)|VARIABLE_ASSIGNMENT|[5]|this.A_Interest_Past_Roll__c|69387.50|0x4323111e
11:57:37.827 (1833939457)|STATEMENT_EXECUTE|[6]
11:57:37.827 (1834056769)|VARIABLE_ASSIGNMENT|[6]|this.Accrued_Interest_Drop__c|120575.00|0x4323111e
11:57:37.827 (1834065117)|STATEMENT_EXECUTE|[7]
11:57:37.827 (1834174591)|VARIABLE_ASSIGNMENT|[7]|this.Drop_Min__c|1.50|0x4323111e
11:57:37.827 (1834182057)|STATEMENT_EXECUTE|[8]
11:57:37.827 (1834288701)|VARIABLE_ASSIGNMENT|[8]|this.Filter_Days_Drop__c|186|0x4323111e
11:57:37.827 (1834296145)|STATEMENT_EXECUTE|[9]
11:57:37.827 (1834399671)|VARIABLE_ASSIGNMENT|[9]|this.Filtered_Earned_Rollup__c|17631.25|0x4323111e
11:57:37.827 (1834406876)|STATEMENT_EXECUTE|[10]
11:57:37.827 (1834510075)|VARIABLE_ASSIGNMENT|[10]|this.Test_Interest_Income_Rollup__c|17631.25|0x4323111e
11:57:37.827 (1834517670)|STATEMENT_EXECUTE|[11]
11:57:37.827 (1834621301)|VARIABLE_ASSIGNMENT|[11]|this.Interest_Income_Rollup__c|120575.00|0x4323111e
11:57:37.827 (1834628253)|STATEMENT_EXECUTE|[12]
11:57:37.827 (1834730867)|VARIABLE_ASSIGNMENT|[12]|this.Interest_Past_Roll__c|69387.50|0x4323111e
11:57:37.827 (1834737938)|STATEMENT_EXECUTE|[13]
11:57:37.827 (1834839923)|VARIABLE_ASSIGNMENT|[13]|this.Rate_Drop__c|0.0325|0x4323111e
11:57:37.827 (1834846982)|STATEMENT_EXECUTE|[14]
11:57:37.827 (1834968164)|VARIABLE_ASSIGNMENT|[14]|this.Today__c|1.50|0x4323111e
11:57:37.827 (1834993815)|HEAP_ALLOCATE|[2]|Bytes:5
11:57:37.827 (1835014266)|HEAP_ALLOCATE|[2]|Bytes:4
Change second time
Debug Log (part for record I need updated)
(1971167638)|VARIABLE_ASSIGNMENT|[2]|outstanding|{"LastModifiedDate":"2018-05-29T17:57:39.000Z","Between__c":1.50,"Accrued_Interest__c":120575.00,"Interest_Rate__c":0.0325,"Outstanding_Principl (12 more) ...":1050000.00,"Conga_Filter__c":false,"Test_Interest_Income (3 more) ...":17631.25,"Filter_Past_Days__c":69387.50,"End_Date__c":"2018-06-30T00:00:00.000Z","Days_Filtered__c":187,"Name":"A-01022","Accrued_Interest_Pas (4 more) ...":69387.50,"Accrued_Interest_Dro (4 more) ...":120575.00,"Days_Drop__c":1272.00,"Before_Filter__c":false,"Time__c":"2018-05-29","Investment_Name__c":"a0AU000000er3SZMAY","CreatedById":"0050B000006mBBGQA2","Interest_Rate_Change (3 more) ...":"Interest End 1 / Int (14 more) ...","Scheduled_Action_Tim (4 more) ...":"2018-04-09T18:31:26.000Z","Outstanding_Balance_ (10 more) ...":1050000.00,"Interest_Past_Roll__ (1 more) ...":69387.50,"A_Interest_Past_Roll (3 more) ...":69387.50,"Start_Date__c":"2014-12-18T00:00:00.000Z","Days__c":1272,"IsDeleted":false,"Filtered_Earned_Inco (5 more) ...":17631.25,"Test_Interest_Income (10 more) ...":17631.25,"Today__c":1.50,"Interest_Income__c":120575.00,"Filter_Days_Drop__c":186.00,"Between_Dates__c":false,"Between_Ending_Dates (3 more) ...":false,"SystemModstamp":"2018-05-29T17:57:39.000Z","Interest_Income_Roll (5 more) ...":120575.00,"Drop_Min__c":1.500,"Outstanding_Principl (4 more) ...":1050000.00,"Rate_Drop__c":0.0325,"Interest_Income_Past (3 more) ...":69387.50,"CreatedDate":"2018-04-09T17:28:42.000Z","MinorMax__c":1.50,"Id":"a2j0B0000012VuEQAU","Filtered_Earned_Roll (5 more) ...":17631.25,"LastModifiedById":"0050B000006mBBGQA2"}|0x6a812a79
12:04:15.965 (1971210622)|STATEMENT_EXECUTE|[2]
12:04:15.965 (1971213220)|STATEMENT_EXECUTE|[4]
12:04:15.965 (1971448109)|VARIABLE_ASSIGNMENT|[4]|this.Days_Drop__c|1272|0x6a812a79
12:04:15.965 (1971461492)|STATEMENT_EXECUTE|[5]
12:04:15.965 (1971625969)|VARIABLE_ASSIGNMENT|[5]|this.A_Interest_Past_Roll__c|69387.50|0x6a812a79
12:04:15.965 (1971635530)|STATEMENT_EXECUTE|[6]
12:04:15.965 (1971777894)|VARIABLE_ASSIGNMENT|[6]|this.Accrued_Interest_Drop__c|120575.00|0x6a812a79
12:04:15.965 (1971789732)|STATEMENT_EXECUTE|[7]
12:04:15.965 (1971926484)|VARIABLE_ASSIGNMENT|[7]|this.Drop_Min__c|1.50|0x6a812a79
12:04:15.965 (1971934361)|STATEMENT_EXECUTE|[8]
12:04:15.965 (1972070442)|VARIABLE_ASSIGNMENT|[8]|this.Filter_Days_Drop__c|187|0x6a812a79
12:04:15.965 (1972078886)|STATEMENT_EXECUTE|[9]
12:04:15.965 (1972210814)|VARIABLE_ASSIGNMENT|[9]|this.Filtered_Earned_Rollup__c|17631.25|0x6a812a79
12:04:15.965 (1972218581)|STATEMENT_EXECUTE|[10]
12:04:15.965 (1972371926)|VARIABLE_ASSIGNMENT|[10]|this.Test_Interest_Income_Rollup__c|17631.25|0x6a812a79
12:04:15.965 (1972380853)|STATEMENT_EXECUTE|[11]
12:04:15.965 (1972510143)|VARIABLE_ASSIGNMENT|[11]|this.Interest_Income_Rollup__c|120575.00|0x6a812a79
12:04:15.965 (1972523948)|STATEMENT_EXECUTE|[12]
12:04:15.965 (1972654918)|VARIABLE_ASSIGNMENT|[12]|this.Interest_Past_Roll__c|69387.50|0x6a812a79
12:04:15.965 (1972663224)|STATEMENT_EXECUTE|[13]
12:04:15.965 (1972799566)|VARIABLE_ASSIGNMENT|[13]|this.Rate_Drop__c|0.0325|0x6a812a79
12:04:15.965 (1972807870)|STATEMENT_EXECUTE|[14]
12:04:15.965 (1972935062)|VARIABLE_ASSIGNMENT|[14]|this.Today__c|1.50|0x6a812a79
12:04:15.965 (1972962626)|HEAP_ALLOCATE|[2]|Bytes:5
12:04:15.965 (1972983977)|HEAP_ALLOCATE|[2]|Bytes:4
(1971167638)|VARIABLE_ASSIGNMENT|[2]|outstanding|{"LastModifiedDate":"2018-05-29T17:57:39.000Z","Between__c":1.50,"Accrued_Interest__c":120575.00,"Interest_Rate__c":0.0325,"Outstanding_Principl (12 more) ...":1050000.00,"Conga_Filter__c":false,"Test_Interest_Income (3 more) ...":17631.25,"Filter_Past_Days__c":69387.50,"End_Date__c":"2018-06-30T00:00:00.000Z","Days_Filtered__c":187,"Name":"A-01022","Accrued_Interest_Pas (4 more) ...":69387.50,"Accrued_Interest_Dro (4 more) ...":120575.00,"Days_Drop__c":1272.00,"Before_Filter__c":false,"Time__c":"2018-05-29","Investment_Name__c":"a0AU000000er3SZMAY","CreatedById":"0050B000006mBBGQA2","Interest_Rate_Change (3 more) ...":"Interest End 1 / Int (14 more) ...","Scheduled_Action_Tim (4 more) ...":"2018-04-09T18:31:26.000Z","Outstanding_Balance_ (10 more) ...":1050000.00,"Interest_Past_Roll__ (1 more) ...":69387.50,"A_Interest_Past_Roll (3 more) ...":69387.50,"Start_Date__c":"2014-12-18T00:00:00.000Z","Days__c":1272,"IsDeleted":false,"Filtered_Earned_Inco (5 more) ...":17631.25,"Test_Interest_Income (10 more) ...":17631.25,"Today__c":1.50,"Interest_Income__c":120575.00,"Filter_Days_Drop__c":186.00,"Between_Dates__c":false,"Between_Ending_Dates (3 more) ...":false,"SystemModstamp":"2018-05-29T17:57:39.000Z","Interest_Income_Roll (5 more) ...":120575.00,"Drop_Min__c":1.500,"Outstanding_Principl (4 more) ...":1050000.00,"Rate_Drop__c":0.0325,"Interest_Income_Past (3 more) ...":69387.50,"CreatedDate":"2018-04-09T17:28:42.000Z","MinorMax__c":1.50,"Id":"a2j0B0000012VuEQAU","Filtered_Earned_Roll (5 more) ...":17631.25,"LastModifiedById":"0050B000006mBBGQA2"}|0x6a812a79
12:04:15.965 (1971210622)|STATEMENT_EXECUTE|[2]
12:04:15.965 (1971213220)|STATEMENT_EXECUTE|[4]
12:04:15.965 (1971448109)|VARIABLE_ASSIGNMENT|[4]|this.Days_Drop__c|1272|0x6a812a79
12:04:15.965 (1971461492)|STATEMENT_EXECUTE|[5]
12:04:15.965 (1971625969)|VARIABLE_ASSIGNMENT|[5]|this.A_Interest_Past_Roll__c|69387.50|0x6a812a79
12:04:15.965 (1971635530)|STATEMENT_EXECUTE|[6]
12:04:15.965 (1971777894)|VARIABLE_ASSIGNMENT|[6]|this.Accrued_Interest_Drop__c|120575.00|0x6a812a79
12:04:15.965 (1971789732)|STATEMENT_EXECUTE|[7]
12:04:15.965 (1971926484)|VARIABLE_ASSIGNMENT|[7]|this.Drop_Min__c|1.50|0x6a812a79
12:04:15.965 (1971934361)|STATEMENT_EXECUTE|[8]
12:04:15.965 (1972070442)|VARIABLE_ASSIGNMENT|[8]|this.Filter_Days_Drop__c|187|0x6a812a79
12:04:15.965 (1972078886)|STATEMENT_EXECUTE|[9]
12:04:15.965 (1972210814)|VARIABLE_ASSIGNMENT|[9]|this.Filtered_Earned_Rollup__c|17631.25|0x6a812a79
12:04:15.965 (1972218581)|STATEMENT_EXECUTE|[10]
12:04:15.965 (1972371926)|VARIABLE_ASSIGNMENT|[10]|this.Test_Interest_Income_Rollup__c|17631.25|0x6a812a79
12:04:15.965 (1972380853)|STATEMENT_EXECUTE|[11]
12:04:15.965 (1972510143)|VARIABLE_ASSIGNMENT|[11]|this.Interest_Income_Rollup__c|120575.00|0x6a812a79
12:04:15.965 (1972523948)|STATEMENT_EXECUTE|[12]
12:04:15.965 (1972654918)|VARIABLE_ASSIGNMENT|[12]|this.Interest_Past_Roll__c|69387.50|0x6a812a79
12:04:15.965 (1972663224)|STATEMENT_EXECUTE|[13]
12:04:15.965 (1972799566)|VARIABLE_ASSIGNMENT|[13]|this.Rate_Drop__c|0.0325|0x6a812a79
12:04:15.965 (1972807870)|STATEMENT_EXECUTE|[14]
12:04:15.965 (1972935062)|VARIABLE_ASSIGNMENT|[14]|this.Today__c|1.50|0x6a812a79
12:04:15.965 (1972962626)|HEAP_ALLOCATE|[2]|Bytes:5
12:04:15.965 (1972983977)|HEAP_ALLOCATE|[2]|Bytes:4
It makes sense that before triggers do not have access to the new values of formula fields however that brings about the questions of why my trigger can pull from the formula fields (even though it is a before trigger) sometimes but not others.
When I tried to create an after trigger it did not work. How can I rewrite my above trigger to an after trigger and what should the test look like (sorry I am a newbie who is horrible at apex).
here is the trigger
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, CopyFieldsonOutstanding3: execution of AfterInsert caused by: System.DmlException: Update failed. First exception on row 0 with id a2j0B0000012YyRQAU; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, CopyFieldsonOutstanding3: maximum trigger depth exceeded Outstanding_Balance trigger event AfterInsert Outstanding_Balance trigger event AfterUpdate Outstanding_Balance trigger event AfterUpdate Outstanding_Balance trigger event AfterUpdate Outstanding_Balance trigger event AfterUpdate Outstanding_Balance trigger event AfterUpdate Outstanding_Balance trigger event AfterUpdate Outstanding_Balance trigger event AfterUpdate Outstandi
Stack Trace: Class.CopyFieldsonOutstandingtest.testTrigger: line 24, column 1
Sorry, to keep asking things. I really appreciate the help!
trigger CopyFieldsonOutstanding3 on Outstanding_Balance__c (after insert, after update) {
Set<Id> ids = trigger.newMap.keyset();
// query for the records since trigger.new records are not directly editable
List<Outstanding_Balance__c> balances = [SELECT Id,
Days_Drop__c,
Days__c,
Filter_Days_Drop__c,
Days_Filtered__c,
Interest_Income_Rollup__c,
Interest_Income__c,
Interest_Past_Roll__c,
Interest_Income_Past__c,
Rate_Drop__c,
Interest_Rate__c,
Today__c,
Between__c,
A_Interest_Past_Roll__c,
Accrued_Interest_Past__c,
Accrued_Interest_Drop__c,
Accrued_Interest__c,
Drop_Min__c,
MinorMax__c,
Test_Interest_Income_Rollup__c,
Test_Interest_Income__c,
Filtered_Earned_Rollup__c,
Filtered_Earned_Income__c
FROM
Outstanding_Balance__c
WHERE
Id IN :ids];
//list of balances to be updated
List<Outstanding_Balance__c> balances2update = new List<Outstanding_Balance__c>();
for(Outstanding_Balance__c outstanding: balances){
//check if the records need updating,
if (outstanding.Days_Drop__c != outstanding.Days__c || outstanding.Filter_Days_Drop__c != outstanding.Days_Filtered__c || outstanding.Rate_Drop__c != outstanding.Interest_Rate__c) {
//Field that you want to be copied by other field keep those on left side
outstanding.Days_Drop__c = outstanding.Days__c;
outstanding.Rate_Drop__c = outstanding.Interest_Rate__c;
outstanding.Filter_Days_Drop__c = outstanding.Days_Filtered__c;
outstanding.Interest_Income_Rollup__c = outstanding.Interest_Income__c;
outstanding.Accrued_Interest_Drop__c = outstanding.Accrued_Interest__c;
outstanding.Interest_Past_Roll__c = outstanding.Interest_Income_Past__c;
outstanding.A_Interest_Past_Roll__c = outstanding.Accrued_Interest_Past__c;
outstanding.Test_Interest_Income_Rollup__c = outstanding.Test_Interest_Income__c;
outstanding.Filtered_Earned_Rollup__c = outstanding.Filtered_Earned_Income__c;
outstanding.Drop_Min__c = outstanding.MinorMax__c;
outstanding.Today__c = outstanding.Between__c;
balances2update.add(outstanding);
}
}
if (!balances2update.isEmpty()){
update balances2update;
}
}
However, it is still not working.I think it might still be a problem with the formulas not updating during the trigger. If this is the problem, how could I get around this? If it's not the issue, do you have any idea what could be?
Thanks.