You need to sign in to do that
Don't have an account?

before insert trigger not updating record in tests
Hello,
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 : Trigger.new) {
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.
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 : Trigger.new) {
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.
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 Trigger.new array
you can traverse the array with an index, something like this :
for ( integer i =0 ; i< trigger.new.size(); i++ )
{ Opportunity salesItem = trigger.new[i];
then you can make the assignment
trigger.new[i].probability = prob;
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 way...do you have any other ideas as to why it isn't persisting the update?
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