+ Start a Discussion
Aaron Persich 8Aaron Persich 8 

How to build a test class for my trigger

Hello,

I am very new to triggers and code and will need some help with a test class.  I have found the below trigger on the web and added it to my sandbox.  I have tested it and it works great.  The trigger updates a "Contract Status" checkbox on the account if there is an active contract associated to it.  Additionally, it will set the checkbox to false if there are no active contracts. 

I am now trying to use a changset to push this into production but I think I need to create a test class to validate it in Prod.  I could be wrong with this assumption.  Can someone please help create a test class or guide me through the process to push this into production?
 
trigger AaronPTest on Contract (after insert, after Update) {
 
    Set<Id> setAccountId = new Set<Id>();  
 
    List<Account> lstToUpdate = new List<Account>();
 
    for(Contract rec : Trigger.New)
 
    {
 
        setAccountId.add(rec.AccountId);
 
    }
 
    List<Account> lstAccount = [SELECT Id,(SELECT Id FROM Contracts WHERE Contract_Status__c = true),Contract_Status__c FROM Account Where Id IN:setAccountId];
 
    for(Account acc : lstAccount)
 
    {
 
        if(acc.Contracts.size() > 0 && !acc.Contract_Status__c){
 
            acc.Contract_Status__c = true;
 
            lstToUpdate.add(acc);
 
        }
 
        else if(acc.Contracts.size() == 0 && acc.Contract_Status__c)
 
        {
 
            acc.Contract_Status__c = false;
 
            lstToUpdate.add(acc);
 
        }
 
    }
 
         Database.update(lstToUpdate);
}
 
 
Thanks,
 
Aaron
 
 
Best Answer chosen by Aaron Persich 8
Nagendra Prasad ChNagendra Prasad Ch
Hi Aaron,
Here is the test class for ur trigger, and will cover most of ur code. I coded it in note pad and didn't execut it, so do corrections to ur self if small syntax errors r there.
 
public class contractTest{
static testmethod void testContractTrigger(){
Account account1 = new Account(Name = 'Test Acc1',Contract_Status__c=True);//Add required fields to Account if any
insert account1;

Account account2 = new Account(Name = 'Test Acc2',Contract_Status__c=false);//Add required fields to Account if any
insert account2;

contract cntr1 = new contract(AccountId=account1.Id,Contract_Status__c=True);//Add required fields to contrat if any
insert cntr1;

contract cntr2 = new contract(AccountId=account2.Id,Contract_Status__c=false);//Add required fields to contrat if any
insert cntr2;

}
}

Thanks,
Nagendra

All Answers

Nagendra Prasad ChNagendra Prasad Ch
Hi Aaron,
Here is the test class for ur trigger, and will cover most of ur code. I coded it in note pad and didn't execut it, so do corrections to ur self if small syntax errors r there.
 
public class contractTest{
static testmethod void testContractTrigger(){
Account account1 = new Account(Name = 'Test Acc1',Contract_Status__c=True);//Add required fields to Account if any
insert account1;

Account account2 = new Account(Name = 'Test Acc2',Contract_Status__c=false);//Add required fields to Account if any
insert account2;

contract cntr1 = new contract(AccountId=account1.Id,Contract_Status__c=True);//Add required fields to contrat if any
insert cntr1;

contract cntr2 = new contract(AccountId=account2.Id,Contract_Status__c=false);//Add required fields to contrat if any
insert cntr2;

}
}

Thanks,
Nagendra
This was selected as the best answer
Aaron Persich 8Aaron Persich 8
Thanks Nagendra,

Do i need to have asserts to make sure my test method is functioning properly?  If so can you help me out with that as well?
Nagendra Prasad ChNagendra Prasad Ch
As u have already tested your trigger and is working as expected you no need to put assert statements in test class. Just run the test class and check the code coverage of the trigger. 
If it is working as expected, select above one as best answer.
Aaron Persich 8Aaron Persich 8
Thanks for the help Nagendra.  The test class worked perfectly.