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
GailGail 

Test Class not updating opp?

I'm writing a test class for a trigger that runs on after insert or after update on Opps. My after insert test is working fine but I can't seem to get the update test to work. 

// Create the new Opportunity record
        Account a = new Account(~sets fields~);
        Opportunity opp = new Opportunity(CurrencyIsoCode = 'EUR', License_Amount__c = double.valueOf('1000.00') ~sets other required fields~);
        insert opp;

        test.startTest();
        
        opp.License_Amount__c = double.valueOf('2000.00');
        update opp;
        
        test.stopTest();
        
        //query the updated opp
        Opportunity oppRecordUpdated = [SELECT Amount_USD__c, Amount FROM Opportunity WHERE ID = :opp.ID];
                 
        // If the trigger works, then the opp's Amount_USD__c field should now be 2000 EUR converted to USD
        System.assertEquals(Math.roundtolong(oppRecordUpdated.Amount_USD__c), Math.roundtolong(oppRecordUpdated.Amount / EUR.ConversionRate));
        

        }

}

 what's happening is Amount USD is not getting updated - the number returned is based on what happened on the insert, not on the update. When I check my code coverage on the trigger, it shows the update trigger as NOT having coverage. How do I ensure that update trigger is run? Thought I had it with the starttest statement but apparently not. 

Best Answer chosen by Admin (Salesforce Developers) 
sandeep1989sandeep1989
Update your test class to set the recursion field to false before you update the record

Hope this helps !!!!!!!

Sent from my iPhone

All Answers

sandeep1989sandeep1989
can you post your trigger code.
GailGail
trigger OpportunityAfterInsertUpdate on Opportunity (after insert, after update) {

if (!Recursive.isWorking()) {

    Recursive.setworking(); //class used to prevent recursion
    Set<Id> oppIDs = new Set<Id>();

    //Identify the records where amount has changed.
    for(Opportunity opp : Trigger.new) {
        if(Trigger.isInsert) {
            oppIDs.add(opp.Id);
        }
        else if(opp.Total_Amount__c != Trigger.oldMap.get(opp.Id).Total_Amount__c && (!opp.IsClosed || 
                (!Trigger.oldMap.get(opp.Id).IsClosed && opp.IsClosed))
                 || (Trigger.oldMap.get(opp.Id).IsClosed && !opp.IsClosed)) {
            oppIDs.add(opp.Id);
            system.debug('went through updated code - ' + opp.Id);
        }
    }
    
    
    //This avoids infinite loop when opps are updated later.
    if(!oppIDs.isEmpty()) {
        Opportunity[] opps = [select convertCurrency(Total_Amount__c) from Opportunity where Id in :oppIDs];
        for(Opportunity opp : opps) {
            opp.Amount_USD__c = opp.Total_Amount__c;
            system.debug('opp amount = ' + opp.Total_Amount__c);
        }
        update opps;
    }
}
}

 

Marko LamotMarko Lamot

 

I believe update does not pass your if:

 

if(opp.Total_Amount__c != Trigger.oldMap.get(opp.Id).Total_Amount__c && (!opp.IsClosed || 
                (!Trigger.oldMap.get(opp.Id).IsClosed && opp.IsClosed))
                 || (Trigger.oldMap.get(opp.Id).IsClosed && !opp.IsClosed))

 

 

it seems that condition is true only if "old" or "new" opportunity is Closed. In your test class you don't set the opportunity to close before update statement.....

sandeep1989sandeep1989
Update your test class to set the recursion field to false before you update the record

Hope this helps !!!!!!!

Sent from my iPhone
This was selected as the best answer
GailGail

Thanks so much, Sandeep! If you're at dreamforce, I'll buy you a beer :)

sandeep1989sandeep1989
I wish to be there but might not be. Anyways thanks for the beer. give you me a kudos if it helps

Sent from my iPhone