0

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
  • Please do not create new SFSE accounts each time you post. Please use the Contact Us link to reach out to the Community team and have your numerous accounts merged.
    – David Reed
    Commented Sep 24, 2020 at 0:02

1 Answer 1

1

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .