Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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:

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){     
        OppID.add(CS.Opportunity__c);}            

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);
            OppUpdate.add(o);                   

        }
    }
}           

//Update list
System.debug(OppUpdate);
update OppUpdate; 
}

Test Class:

@isTest
public class TestStudentNoUpdate{
static testmethod void validateStudentNoUpdate(){
    Account a = new Account(
        Name='Denver University',
        Website='test.com',
        Phone='5555555555',
        Industry='Other');
    insert a;

    Contact c = new Contact(
        LastName='Greene',
        Subject__c='Psychology',
        AccountId=a.id);
    insert c;

    Opportunity o = new Opportunity(
        Name='Denvertest', 
        //Account='Denver University', 
        Type='New_Business',
        Subject__c='Psychology',
        CloseDate=system.today(),
        StageName='Confirm Adoption Won',
        Contact__c=c.id,
        Number_of_students__c=4);
    insert o;        
    System.debug('Number of students BEFORE Update: ' + o.Number_of_students__c);

    Class_Size__c cs = new Class_Size__c(
        Opportunity__c=o.Id,
        Number_of_students_CO__c=10);        

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

    System.assertEquals(10, o.Number_of_students__c);

}
}
share|improve this question

2 Answers 2

up vote 5 down vote accepted

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);
share|improve this answer
    
Thanks, that did the trick. –  denvergreene Jun 30 at 17:54

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.