function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
JBerndJBernd 

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');
}
}

 

Best Answer chosen by Admin (Salesforce Developers) 
jungleeejungleee

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!!

 

trigger conZipcodeAcc on Account (before insert, before update) {
   set<string> postalCOde = new set<string>();
   //Get all the postal Code from the account
   for(account a : trigger.new){
		postalCode.add(a.BillingPostalCode);
	}
	
	//Get all the associated County
	list<ZipCounty_Chart_del__c> zipCode = [select id,county__c,Postal_Code__c from ZipCounty_Chart_del__c  where Postal_Code__c IN :postalCode];
	
	
   for(Account a : Trigger.new){
        for(ZipCounty_Chart_del__c zip: zipCode){
            if(a.BillingPostalCode == zip.Postal_Code__c){
                a.County__c = String.valueOf(zip.County__c);
            }
        }
    }
}

 

@isTest
private class TestConzipcodeAcc{
	static testMethod void TestConzipcodeAcc (){
		//create test  ZipCounty_Chart_del__c record
		ZipCounty_Chart_del__c zip = new ZipCounty_Chart_del__c();
		zip.county__c = 'Blah';
		zip.postal_code__c='123456';
		insert zip;
		
		//create a test account
		account acc = new account ();
		acc.name ='Test account';
		acc.billingPostalCode = '123456';//this is same as the one above inserted
		insert acc; // make sure you have inserted all the mandatory fields as well.
	}
}

 

 

 

All Answers

jungleeejungleee

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!!

 

trigger conZipcodeAcc on Account (before insert, before update) {
   set<string> postalCOde = new set<string>();
   //Get all the postal Code from the account
   for(account a : trigger.new){
		postalCode.add(a.BillingPostalCode);
	}
	
	//Get all the associated County
	list<ZipCounty_Chart_del__c> zipCode = [select id,county__c,Postal_Code__c from ZipCounty_Chart_del__c  where Postal_Code__c IN :postalCode];
	
	
   for(Account a : Trigger.new){
        for(ZipCounty_Chart_del__c zip: zipCode){
            if(a.BillingPostalCode == zip.Postal_Code__c){
                a.County__c = String.valueOf(zip.County__c);
            }
        }
    }
}

 

@isTest
private class TestConzipcodeAcc{
	static testMethod void TestConzipcodeAcc (){
		//create test  ZipCounty_Chart_del__c record
		ZipCounty_Chart_del__c zip = new ZipCounty_Chart_del__c();
		zip.county__c = 'Blah';
		zip.postal_code__c='123456';
		insert zip;
		
		//create a test account
		account acc = new account ();
		acc.name ='Test account';
		acc.billingPostalCode = '123456';//this is same as the one above inserted
		insert acc; // make sure you have inserted all the mandatory fields as well.
	}
}

 

 

 

This was selected as the best answer
JBerndJBernd

Thanks - you are a life saver:) Hope I can return the favor one day when I've taught myself more:)