+ Start a Discussion
sssteelesssteele 

Help with test class for child creation trigger

Hi, all,

I'm just starting to tinker around with the development side of SFDC and I've created a trigger that works fine, but I can't seem to ever get my test class to assert true. I've included both below. Any assistance you can provide is greatly appreciated (even in my trigger or best practices). Forgive my stray comments inline.

Trigger:

trigger populateOpptyChildRecords on Opportunity (before update) {
for( Opportunity parent: Trigger.new)
{
 
List<Ad_Campaign__c> children = [ SELECT Id, Opportunity_Id__c, Opportunity_OwnerID__c from 
Ad_Campaign__c where Opportunity__c = :parent.Id];
 
List<Ad_Campaign__c> childrenToUpdate = new List<Ad_Campaign__c>();
 
for(Ad_Campaign__c thischild: children )
{
   if( thischild.Opportunity_OwnerID__c !=  parent.OwnerID)
   {
 //      thischild.Opportunity_OwnerId__c =  parent.OwnerId;
       thischild.Parent_Updated__c = TRUE;
       thischild.Parent_Updated__c = FALSE;
      childrenToUpdate.add(thischild);
   }
}
 
//if( !childrenToUpdate.isEmpty)
//{
   update childrenToUpdate;
//}
 
}
}

 



/=========================================

Test Class:

@isTest
 
private class testPopulateOpptyChildRecords{
static testMethod void runPositiveTestCases(){
        //Set up new account, related oppty, and two users
        
        Account acct = new Account(Name='test account');
            insert acct;
        Account a=[SELECT Id from Account WHERE Name = 'test account' LIMIT 1];
        Opportunity o = new Opportunity(Name='test opportunity', AccountID = acct.Id, StageName='Prospecting', CloseDate=system.Today());
        insert o;
        Ad_Campaign__c ac1 = new Ad_Campaign__c(Opportunity__c=o.Id);
        insert ac1;
        Profile p=[SELECT id FROM Profile p WHERE p.name = 'System Administrator'];
        User u1=new User(
            FirstName='Test', 
            LastName='User', 
            Username='test@test.com.damp', 
            Email='test@test.com', 
            Alias='test1', 
            CommunityNickname='test1', 
            TimeZoneSidKey='America/Los_Angeles', 
            LocaleSidKey='en_US', 
            EmailEncodingKey='ISO-8859-1', 
            ProfileId=p.id, 
                LanguageLocaleKey='en_US');
        insert u1;
    // Get the value for Owner from the Oppty
        Opportunity o1=[SELECT Id, OwnerID from Opportunity WHERE Name = 'test opportunity' LIMIT 1];
        Ad_Campaign__c ac2 =[SELECT Id, Opportunity_OwnerID__c from Ad_Campaign__c WHERE Opportunity__c IN (SELECT Id from Opportunity WHERE Name = 'test opportunity')];
        User u2=[SELECT Id, Name FROM User WHERE Name = 'Test User'];
    //Check to see if the Owner1 field is filled out on the ad campaign
    //System.Assert(ac1.Opportunity_Owner1__c != ''); -- nevermind
    //Update a field on the opportunity to start the trigger
        u2.Id=o1.OwnerId;
        System.Assert(ac1.Opportunity_OwnerID__c==o1.OwnerId);
    }
}

 


//Thanks again
Best Answer chosen by Admin (Salesforce Developers) 
Jake GmerekJake Gmerek

u2.Id=o1.OwnerId;
System.Assert(ac1.Opportunity_OwnerID__c==o1.OwnerId);

 

Should become:

 

o1.ownerId = u1.id;

 

//You have to actually update the Opportunity with a DML statement for the trigger to fire, also the starttest and stoptest give you a new set of governor limits

test.startTest();

update u2;

test.stopTest();


System.Assert(ac1.Opportunity_OwnerID__c==o1.OwnerId);

 

 

Also just some thoughts:

 

1.

 

Opportunity o1=[SELECT Id, OwnerID from Opportunity WHERE Name = 'test opportunity' LIMIT 1];
Ad_Campaign__c ac2 =[SELECT Id, Opportunity_OwnerID__c from Ad_Campaign__c WHERE Opportunity__c IN (SELECT Id from Opportunity WHERE name = 'test opportunity')];
User u2=[SELECT Id, Name FROM User WHERE Name = 'Test User'];

 

Should become:

 

Opportunity o1=[SELECT Id, OwnerID from Opportunity WHERE id =: o.id LIMIT 1];
Ad_Campaign__c ac2 =[SELECT Id, Opportunity_OwnerID__c from Ad_Campaign__c WHERE Opportunity__c =: o.id];
User u2=[SELECT Id, Name FROM User WHERE id =: u1.id];

The Ids are unique and so are much more stable and your test could even run faster.

 

2.

 

In general, you should write your test code assuming batches of 200 records.  If in the future you ever want to do a mass upload or update of opportunities, they will come in batches of 200 and your code needs to handle this while staying under all the governor limits.

 

Hope this helps, let me know if you have questions.

 

 

All Answers

Jake GmerekJake Gmerek

u2.Id=o1.OwnerId;
System.Assert(ac1.Opportunity_OwnerID__c==o1.OwnerId);

 

Should become:

 

o1.ownerId = u1.id;

 

//You have to actually update the Opportunity with a DML statement for the trigger to fire, also the starttest and stoptest give you a new set of governor limits

test.startTest();

update u2;

test.stopTest();


System.Assert(ac1.Opportunity_OwnerID__c==o1.OwnerId);

 

 

Also just some thoughts:

 

1.

 

Opportunity o1=[SELECT Id, OwnerID from Opportunity WHERE Name = 'test opportunity' LIMIT 1];
Ad_Campaign__c ac2 =[SELECT Id, Opportunity_OwnerID__c from Ad_Campaign__c WHERE Opportunity__c IN (SELECT Id from Opportunity WHERE name = 'test opportunity')];
User u2=[SELECT Id, Name FROM User WHERE Name = 'Test User'];

 

Should become:

 

Opportunity o1=[SELECT Id, OwnerID from Opportunity WHERE id =: o.id LIMIT 1];
Ad_Campaign__c ac2 =[SELECT Id, Opportunity_OwnerID__c from Ad_Campaign__c WHERE Opportunity__c =: o.id];
User u2=[SELECT Id, Name FROM User WHERE id =: u1.id];

The Ids are unique and so are much more stable and your test could even run faster.

 

2.

 

In general, you should write your test code assuming batches of 200 records.  If in the future you ever want to do a mass upload or update of opportunities, they will come in batches of 200 and your code needs to handle this while staying under all the governor limits.

 

Hope this helps, let me know if you have questions.

 

 

This was selected as the best answer
sssteelesssteele

Jake, that was the ticket. Thanks!