+1 vote
14 views
by (163k points)

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:

  1. Territory Name - Name
  2. Zip Code - Zip_Code__c(Text)
  3. 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

1 Answer

+1 vote
by (163k points)
 
Best answer

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

This should be a trigger on the Account object, not on the Territory object. In rough outline, you would need to write a trigger on Account insert and update that follows a classic "iterate-query-update" pattern:

  1. Iterate over the records in Trigger.new to locate Accounts with changed ZIP codes, and build a Set<String> of those ZIP codes.
  2. Query for Territory__c records matching that set of ZIP codes, and store the query results in a Map<String, Territory__c> keyed on the ZIP code.
  3. Iterate over the Accounts again. For those Accounts with changed ZIP codes, look up the associated Territory via the Map and assign the Account's OwnerId field to the value of that Territory's Owner__c field.

You will find lots of examples of this pattern in trigger-related questions throughout SFSE. The Apex Triggers module is also a good place to start with trigger basics.


                for(Territory__c newZipCode : Trigger.new) {
                    Territory__c zipCodeTable = new Territory__c();
                        zipCodeTable.Name = 'Lakeland';
                        newTerritory.add(zipCodeTable);
            }
            insert newTerritory;

The salient part of the error message isn't included in your post, but if you succeeded in inserting Territory records in a Territory insert trigger, you would cause infinite trigger recursion, which the system will ultimately stop with an exception. It's not clear what this code is trying to do; it does not accrue towards your stated objective and will not work.

Welcome to Memory Exceeded, where you can ask questions and receive answers from other members of the community.
...