I have to write a trigger that will automate each sales rep's territory. In this process the Account Owners field is set to sales rep's User record when the Account's Zip code belongs to the sales rep. For this purpose I have created a custom Territory object. This object has following fields:
- Territory Name - Name
- Zip Code - Zip_Code__c(Text)
- Owner - Owner__c(Lookup User)
My trigger is as follows:
trigger zipCode on Territory__c (before insert, before update) {
List<Territory__c> newTerritory = new List<Territory__c>();
if(Trigger.isBefore) {
if(Trigger.isInsert) {
for(Territory__c newZipCode : Trigger.new) {
Territory__c zipCodeTable = new Territory__c();
zipCodeTable.Name = 'Lakeland';
newTerritory.add(zipCodeTable);
}
insert newTerritory;
}
}
}
As I am new I am not sure if I am doing the right way because when I execute the trigger it gives me an error as follows:
Apex trigger zipCode caused an unexpected exception, contact your administrator: zipCode: execution of BeforeInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error:
Any help will be appreciated