I have the following Trigger, Class & Unit Test
trigger WE_MasterOpportunityTrigger on Opportunity (
before insert, after insert,
before update, after update,
before delete, after delete)
{
if (Trigger.IsAfter) {
if (Trigger.isInsert) {
// class needs to execute in After context so that values are still populated when
// an Opportunity is created through Lead conversion
WE_OppUpdates oppPreparation = new WE_OppUpdates(Trigger.oldMap, Trigger.newMap);
oppPreparation.updateOpportunities();
}
if (Trigger.isUpdate) {
WE_OppUpdates oppPreparation = new WE_OppUpdates(Trigger.oldMap,Trigger.newMap);
oppPreparation.updateOpportunities();
}
}
}
public class WE_OppUpdates {
// variables
Map<Id, Opportunity> oldOpps;
Map<Id, Opportunity> newOpps;
Date cd;
Opportunity oldOpp;
List<Opportunity> oppsToUpdate = new List<Opportunity>();
// constructor
public WE_OppUpdates(
Map<Id, Opportunity> oldTriggerOpps,
Map<Id, Opportunity> newTriggerOpps) {
oldOpps = oldTriggerOpps;
newOpps = newTriggerOpps;
}
public void updateOpportunities() {
if (Trigger.isInsert){
List<Opportunity> insertedOpps = [SELECT Id, CloseDate, Ramp_Profile__c FROM Opportunity
WHERE Id IN :newOpps.keySet()];
for (Opportunity o : insertedOpps){
cd = o.CloseDate;
if (o.Ramp_Profile__c == 'Immediate'){
o.Implementation_Revenue__c = cd.addDays(15);
o.Revenue_Commencement__c = o.Implementation_Revenue__c.addMonths(1).toStartOfMonth();
// etc
} else if (o.Ramp_Profile__c == 'Standard'){
// set different values
}
}
update oppsToUpdate;
}
if (Trigger.isUpdate){
List<Opportunity> updatedOpps = [SELECT Id, RecordTypeId, CloseDate, Ramp_Profile__c FROM Opportunity
WHERE Id IN :newOpps.keySet()];
for (Opportunity o : updatedOpps){
cd = o.CloseDate;
Opportunity oldOpp = oldOpps.get(o.Id);
system.debug('oldOpp Ramp Profile is ' + oldOpp.Ramp_Profile__c);
system.debug('newOpp Ramp Profile is ' + o.Ramp_Profile__c);
system.debug('oldOpp Close Date is ' + oldOpp.CloseDate);
system.debug('newOpp Close Date is ' + o.CloseDate);
if (oldOpp.CloseDate != o.CloseDate
||
oldOpp.Ramp_Profile__c != o.Ramp_Profile__c){
if (o.Ramp_Profile__c == 'Immediate'){
o.Implementation_Revenue__c = cd.addDays(15);
}
oppsToUpdate.add(o);
} else if (o.Ramp_Profile__c == 'Medium'){
// set different values
}
}
}
update oppsToUpdate;
}
}
@isTest
public class WE_OppUpdatesTest{
@testSetup
static void dataSetup() {
List<Opportunity> opportunities1 = WE_TestDataUtility.createOpportunities(
25, // noOpps
'<*>', // identifier
date.today().addMonths(1), // closeDate
'Standard'); // rampProfile
insert Opportunities1;
}
static testMethod void testOppUpdates(){
List <Opportunity> opportunities = [SELECT Id,RecordTypeId,Name,CloseDate,Ramp_Profile__c FROM Opportunity WHERE Name LIKE '%<*>%'];
Test.startTest();
for(Opportunity o : opportunities){
o.CloseDate = o.CloseDate.addmonths(1);
o.Ramp_Profile__c = 'Immediate';
}
update opportunities;
Test.stopTest();
}
}
When I run the test my debug lines in the isUpdate
section show that the values for the Ramp_Profile__c
& CloseDate
fields are unchanged.
My understanding is that the two maps should contain the old & new values so perhaps I'm not passing the values through to my Class correctly or is this issue occurring because of my SOQL query or something else?
update Trigger.new.deepClone(true);
update Trigger.new.deepClone(true);
as an answer in the context of the rest of my code. I've not used the deepClone function before so I'm not sure how that would change what I have already..