You need to sign in to do that
Don't have an account?
Aaron 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
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
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.
Thanks,
Nagendra
All Answers
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.
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?
If it is working as expected, select above one as best answer.