[SalesForce] Getting error message when trying to execute trigger stating incorrect type

Error:

Apex trigger ContactTerritoryUpdate caused an unexpected exception,
contact your administrator: ContactTerritoryUpdate: data changed by
trigger for field Territory: id value of incorrect type:
001J0000015kbtkIAA

trigger code is simply call to class:

ContactTerritoryUpdateClass.setTerritoryValues(Trigger.new);

Here is the code of the class executed before update or insert of the contact:

public class ContactTerritoryUpdateClass {
/***************
 * When an Admissions Contact is created or updated, need to evaluate the 
 * information provided to determine the appropriate territory assigned.
 ****************/
public static void setTerritoryValues(Contact[] contacts){

    Id terrOut;
    Id terrIn;
    String mailState;
    String mailCounty;
    String mailSubCounty;
    String terrName;

    for (Contact c:contacts){
        terrIn = c.Territory__c;
        mailState = c.MailingState;
        mailCounty = c.Current_County_Code__c;
        mailSubCounty = c.Sub_county_Code__c;

        if (c.Domestic_Or_International__c == 'International') {
            Territory__c[] records = [Select t.Id From Territory__c t where t.Name = 'International' limit 1];
            If (!records.isEmpty()) {
                terrOut = records[0].id;
            }
            if (terrIn <> terrOut) {
                c.Territory__c = terrOut;
            }
        } else if (c.Student_Type__c == 'Freshman') {
            if (c.Home_Schooled__c) {
                if (c.MailingState == 'WI') {
                    terrName = mailState + '-' + mailCounty;
                    system.debug('WI home school terrName coming back with - ' + terrName);
                    Territory__c[] records = [Select t.Id From Territory__c t where t.Name = :terrName limit 1];
                    If (!records.isEmpty()) {
                        terrOut = records[0].id;
                    }
                    if (terrIn <> terrOut) {
                        c.Territory__c = terrOut;
                    }
                } else if (c.MailingState == 'IL') {
                    if (c.Sub_county_Code__c != null) {
                        terrName = mailState + '-' + mailSubCounty;
                        system.debug('IL home school terrName coming back with - ' + terrName);
                        Territory__c[] records = [Select t.Id From Territory__c t where t.Name = :terrName limit 1];
                        If (!records.isEmpty()) {
                            terrOut = records[0].id;
                        }
                        if (terrIn <> terrOut) {
                            c.Territory__c = terrOut;
                        }
                    } else {
                        terrName = mailState + '-' + mailCounty;
                        system.debug('IL home school terrName coming back with - ' + terrName);
                        Territory__c[] records = [Select t.Id From Territory__c t where t.Name = :terrName limit 1];
                        If (!records.isEmpty()) {
                            terrOut = records[0].id;
                        }
                        if (terrIn <> terrOut) {
                            c.Territory__c = terrOut;
                        }
                    }
                } else {
                    terrName = mailState;
                    system.debug('Other home school terrName coming back with - ' + terrName);
                    Territory__c[] records = [Select t.Id From Territory__c t where t.Name = :terrName limit 1];
                    If (!records.isEmpty()) {
                        terrOut = records[0].id;
                    }
                    if (terrIn <> terrOut) {
                        c.Territory__c = terrOut;
                    }
                }
            } else {
                Account[] records = [Select a.Territory__c From Account a where a.Type = 'High School' and a.id = :c.High_School__c limit 1];
                If (!records.isEmpty()) {
                    terrOut = records[0].id;
                }
                if (terrIn <> terrOut) {
                    c.Territory__c = terrOut;
                }
            } 
        } else if (c.Student_Type__c == 'Transfer') {
            Territory__c[] records = [Select t.Id From Territory__c t where t.Name = 'Transfer' limit 1];
            If (!records.isEmpty()) {
                terrOut = records[0].id;
            }
            if (terrIn <> terrOut) {
                c.Territory__c = terrOut;
            }
            }
        }
    }

}

Best Answer

The key to solving your bug is right in the error message:

data changed by trigger for field Territory: id value of incorrect type: 001J0000015kbtkIAA

Salesforce IDs starting with 001 are always Account objects, so this error is indicating that somewhere in your code, you're assigning a territory lookup field to point to an Account object, which is invalid.

A quick skim of your code shows the culprit:

Account[] records = [Select a.Territory__c From Account a where a.Type = 'High School' and a.id = :c.High_School__c limit 1];
If (!records.isEmpty()) {
    terrOut = records[0].id;
}
if (terrIn <> terrOut) {
    c.Territory__c = terrOut;
}

... you're querying for an Account record, then using it as the value for the Territory__c field.

However, a more serious issue with your code is that it's not bulk safe. Salesforce triggers can be run with up to 200 records passed in at a time, but you're only allowed a maximum of 100 SOQL queries per transaction. In your code, you're running a SOQL query for every single contact record that is passed in - if there are 200 Contact records, you'll be trying to run 200 SOQL queries, and your code will fail.

To make your code safe, you need to write it in three phases:

  1. Iterate through all of the Contact records and determine the criteria to query for all of the territory records you could possibly need.

  2. Write a single SOQL query to get all of the Territory records

  3. Iterate through the Contact records again, assigning each one to the appropriate Territory as queried in step 2.

Read more here or Google for salesforce bulk trigger

Related Topic