You need to sign in to do that
Don't have an account?
Unit Test a little help needed for simple trigger
Hi
I'm new to Apex and teaching myself. I've got a simple trigger that works in a sandbox, but to deploy it to production I need code coverage with a unit test. Here's where the problem is.
This is where I got so far. (Trigger takes Billiing Zip off Account object - looks up a related object - finds County - returns to County__c field on Account object)
This is the working trigger
trigger conZipcodeAcc on Account (before insert, before update) {
for(Account a : Trigger.new){
List<ZipCounty_Chart_del__c> zcc = [select t.id,t.county__c,t.Postal_Code__c from ZipCounty_Chart_del__c t where t.Postal_Code__c =: a.BillingPostalCode ];
if(!zcc.isEmpty()){
for(ZipCounty_Chart_del__c t : zcc){
a.County__c = String.valueOf(t.County__c);
}
}
}
}
Here is where I've got with my unit test, but I get a compile error (Error: Compile Error: expecting right curly bracket, found 'EOF' at line 0 column -1).
@isTest
private class TestConzipcodeAcc{
static testMethod void TestConzipcodeAcc (){
//Set up the account record
List<Account> accounts = new List<Account>{};
For (Integer x=0; x<200;x++)
{
//This iterates and creates 200 Accounts with a postal code of 19001 (something known).
Account a = new Account (Test= 'School' + x, BillingPostalCode = '19001');
accounts.add(a);
}
test.startTest();
insert accounts;
test.stopTest();
For (Account a : accounts) {
System.AssertEquals (County__c = 'Montgomery');
}
}
Your trigger was not bulkified as it there was a SOQL inside a FOR loop which is not recommended. I have bulkified the code. Hope it helps!!
All Answers
Your trigger was not bulkified as it there was a SOQL inside a FOR loop which is not recommended. I have bulkified the code. Hope it helps!!
Thanks - you are a life saver:) Hope I can return the favor one day when I've taught myself more:)