Using system.debug I have found that my code does everything I expect it to, up until the very last line of the trigger.

I created a list specifically for updating (OppUpdate). My system debug immediately before update shows that OppUpdate contains the information I expect it to contain. But the debug aftward (in the class) says it fails.

It looks like update OppUpdate isn't working, and I cannot figure out why. I don't have any other triggers running on this custom object. I feel like I am missing something very simple.


trigger StudentNoUpdate on Class_Size__c (after insert, after update)
//trigger runs whenever Class_Size__c is updated or inserted
//trigger updates Opportunity.Number_of_Students with most recent Class_Size__c.Number_of_students_CO__c 
//when Opportunity is master of each Class_Size__c
List<Opportunity> OppUpdate = new List<Opportunity>();    //list for updating

List<ID> OppID = new List<ID>();         //list of interested Opportunity IDs
for (Class_Size__c CS: trigger.new){     

List<Opportunity> OppL = new List<Opportunity>   //Interested Opportunities with all relevant information
    ([select id, Number_of_students__c          
    from Opportunity                            
    where id in: OppID]);                       

List<Class_Size__c> CSL = new List<Class_Size__c>([           //List of Interested CS object being uploaded with relevant information
    select id, Number_of_students_CO__c, Opportunity__c     
    from Class_Size__c                                      
    where id in: trigger.new]);                             

//For every interested opportunity, check every CS Object Opportunity__c field 
//to see if master.detail. If yes, then update the number of students field
//appropriately and add to list to update.
for(Opportunity o : OppL){                      
    for(Class_Size__c cs : CSL){                
        if (o.Id == cs.Opportunity__c){         
            System.debug('Number of students Middle1 Update: ' + o.Number_of_students__c);
            o.Number_of_students__c = cs.Number_of_students_CO__c;    
            System.debug('Number of students Middle2 Update: ' + o.Number_of_students__c);


//Update list
update OppUpdate; 

Test Class:

public class TestStudentNoUpdate{
static testmethod void validateStudentNoUpdate(){
    Account a = new Account(
        Name='Denver University',
    insert a;

    Contact c = new Contact(
    insert c;

    Opportunity o = new Opportunity(
        //Account='Denver University', 
        StageName='Confirm Adoption Won',
    insert o;        
    System.debug('Number of students BEFORE Update: ' + o.Number_of_students__c);

    Class_Size__c cs = new Class_Size__c(

    insert cs;
    System.debug('Number of students AFTER Update: ' + o.Number_of_students__c);

    System.assertEquals(10, o.Number_of_students__c);


2 Answers 2


Your code is mostly likely doing what you expect it to do. Your test, however, is not doing what you are expecting. You need to requery the Opportunity at the end of your test so that you can get the updated Number_of_students__c value.

The last line of your test should be changed to:

Opportunity opp = [select Number_of_students__c from Opportunity where Id = :o.Id];
System.assertEquals(10, opp.Number_of_students__c);

Or if you wanted to do it all inline without creating a variable you could also do:

System.assertEquals(10, [select Number_of_students__c from Opportunity where Id = :o.Id].Number_of_students__c);

The update is actually happening. The problem is that your test class isn't requerying the record after the update. Anything that happens in the trigger doesn't automatically get passed back to the class doing the update. You need to query the record again to get the new values.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .