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
Diane CochranDiane Cochran 

before insert trigger not updating record in tests


I have a simple trigger created to update data on insert or update. However, even though the data seems to be updated internally to the trigger, the test reports that the field value is still null.

Here is the trigger:

trigger SalesProcessOpportunityMatrix on Opportunity (before insert, before update) {
Map> matrix = SalesMatrix.getMatrix();

for (Opportunity salesItem : {

if (!matrix.containsKey(salesItem.financing_status__c)) {
//We don't have an entry for the financing status
salesItem.addError('Financing Status unrecognized!');

} else if (!matrix.get(salesItem.financing_status__c).containsKey(salesItem.stagename)) {
//We don't have an entry for the sales stage
salesItem.addError('Sales stage unrecognized!');

} else {
//Get the percentage
Integer prob = matrix.get(salesItem.financing_status__c).get(salesItem.stagename);
System.debug('Probability is ' + prob);
salesItem.probability = prob;


All of the values seem to be fine internally, but if I assert the probability after an insert it is null instead of the value of prob.
Ron HessRon Hess
salesItem is a local variable,
so i can understand that back in the testmethod the opportunity does not have any changed data.

you need to write the updated data into the array

you can traverse the array with an index, something like this :

for ( integer i =0 ; i<; i++ )
{ Opportunity salesItem =[i];

then you can make the assignment[i].probability = prob;

Diane CochranDiane Cochran
Nope, I'm still having the same problem.

Also, I don't think that would have an effect on the operation of the system unless the assignment to salesItem was by value instead of by reference...which I know Java doesn't do and based on the errors while trying to find different ways of setting this to work the way I need it to, neither does Apex.

Thank you for your help, by the you have any other ideas as to why it isn't persisting the update?
Ron HessRon Hess
all testmethod's , when you run a test, are designed to roll back all changes when it exits, perhaps this is what you are seeing?
Diane CochranDiane Cochran
Perhaps, as when I do manual tests everything seems to be working correctly, but it still presents a problem. Unit tests that perform little more than a runs/doesn't run don't do much to insure the stability of a system...are there any other methods to run tests against triggers that will provide accurate results?
Can you post your test code ? you'll need to query back the row after the insert/update in the test to see what changes were made to the row.

e.g. if your test is something like
Opportunity o = makeOppty();
update o
system.assert o.probability = 50

it needs to be
Opportunity o = makeOppty();
update o
Opportunity o2 = [select id, probability from opportunity where id = :o.Id];
system.assert o2.probability = 50