in Execute Anonymous, executing the following code to test the class:
List<Contact> contacts = [Select c.Territory__c, c.Sub_county_Code__c, c.Student_Type__c, c.Domestic_Or_International__c, c.Recruiter_Code__c, c.Name, c.MailingState, c.Home_Schooled__c, c.High_School__c, c.Current_County_Code__c From Contact c where c.Name = 'Glenn Bargo'];
system.debug('******************************Before execution of assigning territory values: ');
ContactTerritoryUpdateClass.setTerritoryValues(contacts);
system.debug('******************************After execution of assigning territory values: ');
The class looks like this:
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;
Set<ID> schoolIds = new Set<Id>();
for (Contact co:contacts){
if (co.High_School__c != NULL) {
schoolIds.add(co.High_School__c);
}
}
// Currently, there are less than 300 territories and not much chance of growing
Map<string, Territory__c> allTerr = new Map<string, Territory__C>(
[select Name from Territory__c]);
Map<ID, Account> allSchools = new Map<ID, Account>(
[select Territory__c from Account
where Type = 'High School'
and Id in :schoolIds]);
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') {
terrOut = allTerr.get(c.Domestic_Or_International__c).Id;
} else if (c.Student_Type__c == 'Transfer') {
terrOut = allTerr.get(c.Student_Type__c).Id;
} else if (c.Student_Type__c == 'Freshman') {
if (c.High_School__c != NULL){
terrOut = allSchools.get(c.High_School__c).Territory__c;
} else if (c.Home_Schooled__c) {
if (c.MailingState == 'WI') {
terrName = mailState + '-' + mailCounty;
system.debug('WI home school terrName coming back with - ' + terrName);
terrOut = allTerr.get(terrName).Id;
} else if (c.MailingState == 'IL') {
if (mailCounty == 'Cook') {
terrName = mailState + '-Cook2';
system.debug('IL home school terrName coming back with - ' + terrName);
terrOut = allTerr.get(terrName).Id;
} else if (mailCounty == 'De Kalb') {
terrName = mailState + '-De Kalb2';
system.debug('IL home school terrName coming back with - ' + terrName);
terrOut = allTerr.get(terrName).Id;
} else if (mailCounty == 'du Page') {
terrName = mailState + '-du Page1';
system.debug('IL home school terrName coming back with - ' + terrName);
terrOut = allTerr.get(terrName).Id;
} else if (mailCounty == 'Kane') {
terrName = mailState + '-Kane1';
system.debug('IL home school terrName coming back with - ' + terrName);
terrOut = allTerr.get(terrName).Id;
} else {
terrName = mailState + '-' + mailCounty;
system.debug('IL home school terrName coming back with - ' + terrName);
terrOut = allTerr.get(terrName).Id;
}
} else {
//terrName = mailState;
system.debug('Other home school terrName coming back with - ' + mailState);
terrOut = allTerr.get(mailState).Id;
}
}
}
if (terrIn <> terrOut) {
c.Territory__c = terrOut;
}
}
}
}
The log looks like this:
DEBUG LOG
28.0 APEX_CODE,FINE;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;VALIDATION,INFO;WORKFLOW,INFO
Execute Anonymous: List<Contact> contacts = [Select c.Territory__c,
c.Sub_county_Code__c, c.Student_Type__c,
c.Domestic_Or_International__c, c.Recruiter_Code__c, c.Name,
c.MailingState, c.Home_Schooled__c, c.High_School__c,
c.Current_County_Code__c From Contact c where c.Name = 'Glenn Bargo'];
Execute Anonymous: system.debug('******************************Before
execution of assigning territory values: '); Execute Anonymous:
ContactTerritoryUpdateClass.setTerritoryValues(contacts); Execute
Anonymous: system.debug('******************************After execution
of assigning territory values: '); 13:03:39.059
(59199000)|EXECUTION_STARTED 13:03:39.059
(59211000)|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex
13:03:39.060 (60283000)|SOQL_EXECUTE_BEGIN|[1]|Aggregations:0|select
c.Territory__c, c.Sub_county_Code__c, c.Student_Type__c,
c.Domestic_Or_International__c, c.Recruiter_Code__c, c.Name,
c.MailingState, c.Home_Schooled__c, c.High_School__c,
c.Current_County_Code__c from Contact c where c.Name = 'Glenn Bargo'
13:03:39.086 (86191000)|SOQL_EXECUTE_END|[1]|Rows:1 13:03:39.086
(86390000)|USER_DEBUG|[2]|DEBUG|******************************Before
execution of assigning territory values: 13:03:39.096
(96456000)|METHOD_ENTRY|[1]|01pG00000034mOr|ContactTerritoryUpdateClass.ContactTerritoryUpdateClass()
13:03:39.096 (96482000)|METHOD_EXIT|[1]|ContactTerritoryUpdateClass
13:03:39.096
(96543000)|METHOD_ENTRY|[3]|01pG00000034mOr|ContactTerritoryUpdateClass.setTerritoryValues(LIST<Contact>)
13:03:39.096 (96662000)|SYSTEM_CONSTRUCTOR_ENTRY|[14]|<init>(Integer)
13:03:39.096 (96730000)|SYSTEM_CONSTRUCTOR_EXIT|[14]|<init>(Integer)
13:03:39.097 (97507000)|SOQL_EXECUTE_BEGIN|[22]|Aggregations:0|select
Name from Territory__c 13:03:39.103
(103670000)|SOQL_EXECUTE_END|[22]|Rows:238 13:03:39.106
(106133000)|SOQL_EXECUTE_BEGIN|[25]|Aggregations:0|select Territory__c
from Account where (Type = 'High School' and Id = :tmpVar1)
13:03:39.112 (112164000)|SOQL_EXECUTE_END|[25]|Rows:0 13:03:39.112
(112716000)|USER_DEBUG|[81]|DEBUG|Other home school terrName coming
back with - TN 13:03:39.112
(112891000)|EXCEPTION_THROWN|[82]|System.NullPointerException: Attempt
to de-reference a null object 13:03:39.113
(113020000)|METHOD_EXIT|[3]|01pG00000034mOr|ContactTerritoryUpdateClass.setTerritoryValues(LIST<Contact>)
13:03:39.113 (113165000)|FATAL_ERROR|System.NullPointerException:
Attempt to de-reference a null object
Class.ContactTerritoryUpdateClass.setTerritoryValues: line 82, column
1 AnonymousBlock: line 3, column 1 AnonymousBlock: line 3, column 1
Territory is custom object and the value TN is in that object. Any help would be greatly appreciated.
Thank you.