function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
jonpilarski1.3914448382645588E12jonpilarski1.3914448382645588E12 

Can not see Account field value in Contact trigger in test method

Hi !

I have a simple test method which creates an Account and a Contact. A trigger on Contact which sets a field based on the Account's BillingState. Problem is when the trigger fires the Billing state evelautes to null. I cannot understand as to why. Here is test method setting these up.

test method:

static testMethod void validate() {
     
        Test.startTest();
         
        account[] accounts = new account[]{
        new account(name='highschool',BillingState='CA',BillingPostalCode='92100') };
 
        insert accounts;

        contact[] contacts = new contact[] {
        new contact(LastName='Sand10',TargetX_SRMb__Student_Type__c='New Freshman',accountId=accounts[0].id)};
 
        insert contacts;
        Test.stopTest();

        List<contact> con = [select id, LastName, FirstName,TargetX_SRMb__Student_Type__c,TargetX_SRMb__College__c,account.BillingState, account.billingPostalCode,Territory_Assignments__c from Contact];
         
        for(Contact e: con) {
    
        System.debug('Last name.......... '+e.lastname+'  State '+e.account.BillingState);   // this actually DOES return BillingState
}

In trigger logic:

...
if(String.isNotBlank(c.TargetX_SRMb__Student_Type__c) ){ 
     system.debug('lastname in trigger: '+c.lastname+' state is '+c.account.billingState);    // this shows null for BillingState
        //look at territory state
        if(String.isNotBlank(c.Account.BillingState)){
              system.debug('not blank');                                    // never get here
.....    
Thanks,
Jon
Vinnie BVinnie B
This is a typical problem I've seen in test classes.  While you did indeed create the account and inserted it, it's not yet available for use by the other code.  Try adding this line to the code after inserting the account:

  ID anAccount = accounts[0].id;

Then use 'anAccount' when specifying the account ID when creating the account.

I think that'll work.  Anyway, it's easy to try.  :)
jonpilarski1.3914448382645588E12jonpilarski1.3914448382645588E12
Thanks Vinnie. That did not work though. I did this which is what I believe you suggested:
account[] accounts = new account[]{new account(name='highschool7',BillingState='CA',BillingPostalCode='91900')};
 
    insert accounts;
    ID anAccount0 = accounts[0].id;
    
        contact[] contacts = new contact[] {
        new contact(LastName='Sand10',TargetX_SRMb__Student_Type__c='New Freshman',accountId=anAccount0 )
        };
  
    insert contacts;
Vinnie BVinnie B
Hmmm.  That's definitely worked for me in the past.  The only thing I could think of is to try it without the arrays.

In looking back at your original post, I'm unclear what the problem is.  My problem was often that it simply wouldn't test as much code as I'd like because the object wasn't getting created properly (i.e. the lookup field was blank).  I can't see why the Billing State would show up in one part of the debug code but not the other.  If it is there in the first debug statement then I'd say the problem has to be with the second debug statement as opposed to the code creating the account and contact.
jonpilarski1.3914448382645588E12jonpilarski1.3914448382645588E12
Hi Vinnie. The problem is that I have a before insert trigger on "Contact". Here I evaluate the "Account".BillingState field of the contact. For example:
if(c.Account.BillingState == 'AK') // do something  (c is reference to Contact)

So in my test I first create Accounts. I can loop through and see their IDs in test class no problem. Then , using these Account IDs I create Contacts. Here I too can loop through list of contact just created and see the account IDs attached. Then the trigger fires and debug here displays c.Account.BillingState = null. ??? The trigger cannot see the Account values, but it can see the Contact values, ie Name, etc. ???? 

I tried an After insert trigger but got expected Read Only error.

Jon
Vinnie BVinnie B
I know that test data is deleted is soon as the test is done.  Could you try moving the code to stop the test to after you've done the debugging?
jonpilarski1.3914448382645588E12jonpilarski1.3914448382645588E12
I have Test.startTest(); and Test.stopTest(); as first and last lines of test method.
Vinnie BVinnie B
It sounds like the problem is in the trigger code itself of which you haven't provided any details.  The test code appears to be working and properly setting the values but the trigger itself isn't setting them.  Is that correct? 
jonpilarski1.3914448382645588E12jonpilarski1.3914448382645588E12
Hi Vinnie. Here is trigger. Note, first debug statement always is null.

trigger USD_setTerritory on Contact (before insert, before update) {


if(Trigger.isInsert )  {

  for (Contact c: Trigger.new) {
 
   System.debug('state is '+ c.account.billingState);   // this is always null...

       if(c.Account.name == 'Univ San Diego'){
     
           c.Territory_Assignments__c = 'Univ San Diego';
        }else if ( c.TargetX_SRMb__Student_Type__c == 'New Graduate' && String.isNotBlank(c.TargetX_SRMb__Level__c) && (c.TargetX_SRMb__Level__c == 'Law' || c.TargetX_SRMb__Level__c == 'Law Graduate') ){   
           c.Territory_Assignments__c = 'School of Law';  
        }else if ( c.TargetX_SRMb__Student_Type__c == 'New Graduate' && String.isBlank(c.TargetX_SRMb__College__c) && String.isBlank(c.TargetX_SRMb__Anticipated_Major__c) ){   
           c.Territory_Assignments__c = 'Unknown Graduate';  
        }else if ( c.TargetX_SRMb__Student_Type__c == 'New Graduate' && String.isBlank(c.TargetX_SRMb__College__c) ){   
           c.Territory_Assignments__c = 'Unknown Graduate';     
        }else if ( c.TargetX_SRMb__Student_Type__c == 'New Graduate' &&  String.isNotBlank(c.TargetX_SRMb__College__c) && c.TargetX_SRMb__College__c != 'School of Business Admin' ){         
           c.Territory_Assignments__c = c.TargetX_SRMb__College__c;
        }else if ( c.TargetX_SRMb__Student_Type__c == 'First-Year Law' && (c.TargetX_SRMb__College__c != 'School of Law' || String.isBlank(c.TargetX_SRMb__College__c))){    
           c.Territory_Assignments__c = null;
        }else if ( c.TargetX_SRMb__Student_Type__c == 'New Graduate' && String.isBlank(c.TargetX_SRMb__College__c)){    
           c.Territory_Assignments__c = null;       
        }else if ( c.TargetX_SRMb__Student_Type__c == 'New Graduate' && c.TargetX_SRMb__College__c == 'Franciscan School of Theology'){    
           c.Territory_Assignments__c = null;
        }else if ( c.TargetX_SRMb__Student_Type__c == 'New Transfer'){    
           c.Territory_Assignments__c = 'New Transfer';              
        }else if ( c.TargetX_SRMb__Student_Type__c == 'New Graduate' && c.TargetX_SRMb__College__c == 'School of Business Admin' ){
           if(c.TargetX_SRMb__Anticipated_Major__c ==  'MS in Supply Chain Management'){
              c.Territory_Assignments__c = 'MSSCM';
           }else if(c.TargetX_SRMb__Anticipated_Major__c ==  'MS in Real Estate'){
              c.Territory_Assignments__c = 'MSRE';
           }else if(c.TargetX_SRMb__Anticipated_Major__c ==  'MS in Global Leadership'){
              c.Territory_Assignments__c = 'MSGL';          
           }else if(c.TargetX_SRMb__Anticipated_Major__c ==  'MS in Executive Leadership' || c.TargetX_SRMb__Anticipated_Major__c ==  'MBA, Corporate Counsel' ){
              c.Territory_Assignments__c = 'MSEL';                
           }else if(c.TargetX_SRMb__Anticipated_Major__c ==  'MS in Accountancy' || c.TargetX_SRMb__Anticipated_Major__c ==  'MS in Taxation'){
              c.Territory_Assignments__c = 'MACC/MTAX';
           }else if(c.TargetX_SRMb__Anticipated_Major__c ==  'MBA, International Business Track' ||
                    c.TargetX_SRMb__Anticipated_Major__c ==  'MBA, Evening Program'              ||
                    c.TargetX_SRMb__Anticipated_Major__c ==  'MBA, Corporate Counsel'            ||
                    c.TargetX_SRMb__Anticipated_Major__c ==  'MBA, General Management Track'      ){
              c.Territory_Assignments__c = 'MBA/IMBA';   
           }else if (c.TargetX_SRMb__Student_Type__c == 'New Graduate') {
              c.Territory_Assignments__c = 'Unknown Graduate';
           }     
        }else {
         
        }
  
     if(String.isNotBlank(c.TargetX_SRMb__Student_Type__c) && c.TargetX_SRMb__Student_Type__c == 'New Freshman'){  
     system.debug('lastname in trigger: '+c.lastname+' state is '+c.Account.BillingState);
        //look at territory state
        if(String.isNotBlank(c.Account.BillingState)){
              system.debug('not blank');   
         if(c.Account.BillingState == 'AK' ||
           c.Account.BillingState == 'ID' ||
           c.Account.BillingState == 'MT' ||
           c.Account.BillingState == 'OR' ||
           c.Account.BillingState == 'WA' ||
           c.Account.BillingState == 'WY' ){
          
        
              c.Territory_Assignments__c = 'Area 1 - Imperial & NorthWest'; 
        }else if (c.Account.BillingState == 'AZ' ||
           c.Account.BillingState == 'CO' ||
           c.Account.BillingState == 'NM' ||
           c.Account.BillingState == 'NV' ||
           c.Account.BillingState == 'UT' ){
              c.Territory_Assignments__c = 'Area 2 - North Cal & SouthWest'; 
        }else if (c.Account.BillingState == 'CT' ||
           c.Account.BillingState == 'NJ' ||
           c.Account.BillingState == 'DC' ||
           c.Account.BillingState == 'NY' ||
           c.Account.BillingState == 'DE' ||
           c.Account.BillingState == 'PA' ||
           c.Account.BillingState == 'MA' ||
           c.Account.BillingState == 'RI' ||
           c.Account.BillingState == 'MD' ||
           c.Account.BillingState == 'VA' ||
           c.Account.BillingState == 'ME' ||
           c.Account.BillingState == 'VT' ||
           c.Account.BillingState == 'NH' ||
           c.Account.BillingState == 'WV'  ){
              c.Territory_Assignments__c = 'Area 3 - Mid-Atlantic & NorthEast'; 

        }else if (c.Account.BillingState == 'AL' ||
           c.Account.BillingState == 'MS' ||
           c.Account.BillingState == 'AR' ||
           c.Account.BillingState == 'NC' ||
           c.Account.BillingState == 'FL' ||
           c.Account.BillingState == 'OK' ||
           c.Account.BillingState == 'GA' ||
           c.Account.BillingState == 'SC' ||
           c.Account.BillingState == 'KY' ||
           c.Account.BillingState == 'TN' ||
           c.Account.BillingState == 'LA' ||
           c.Account.BillingState == 'TX'  ){
              c.Territory_Assignments__c = 'Area 4 - Orange Cty & SouthEast'; 

        }else if (c.Account.BillingState == 'IA' ||
           c.Account.BillingState == 'MN' ||
           c.Account.BillingState == 'IL' ||
           c.Account.BillingState == 'ND' ||
           c.Account.BillingState == 'IN' ||
           c.Account.BillingState == 'NE' ||
           c.Account.BillingState == 'KS' ||
           c.Account.BillingState == 'OH' ||
           c.Account.BillingState == 'MI' ||
           c.Account.BillingState == 'SD' ||
           c.Account.BillingState == 'MO' ||
           c.Account.BillingState == 'WI'  ){
              c.Territory_Assignments__c = 'Area 5 - Central Cal & MidWest'; 
        }else if (c.Account.BillingState == 'AA' ||
           c.Account.BillingState == 'AE' ||
           c.Account.BillingState == 'AP' ||
           c.Account.BillingState == 'AS' ||
           c.Account.BillingState == 'FM' ||
           c.Account.BillingState == 'GU' ||
           c.Account.BillingState == 'MH' ||
           c.Account.BillingState == 'MP' ||
           c.Account.BillingState == 'PR' ||
           c.Account.BillingState == 'VI' ||
           c.Account.BillingState == 'PW'  ){
              c.Territory_Assignments__c = 'Area 9 - Military (states AA, AE, & AP)';             
        }else if (c.Account.BillingState == 'HI'  ){
              c.Territory_Assignments__c = 'Area 10 - Hawaii';             
        }
       
      }// if state not blank test
     }// student type new freshman test
    
    
    if(String.isNotBlank(c.TargetX_SRMb__Student_Type__c) && c.TargetX_SRMb__Student_Type__c == 'New Freshman'){    
      if(String.isNotBlank(c.Account.BillingPostalCode)){ // && c.Account.BillingState == 'CA' ){
       
        try{      
        if(integer.valueOf(c.Account.BillingPostalCode) >= 92100 && integer.valueOf(c.Account.BillingPostalCode) <= 92199) {
              c.Territory_Assignments__c = 'Area 1 - Imperial & NorthWest'; 
        }else if ((integer.valueOf(c.Account.BillingPostalCode) >= 94900 && integer.valueOf(c.Account.BillingPostalCode) <= 94999) ||
           (integer.valueOf(c.Account.BillingPostalCode) >= 95500 && integer.valueOf(c.Account.BillingPostalCode) <= 95599) ||
           (integer.valueOf(c.Account.BillingPostalCode) >= 95900 && integer.valueOf(c.Account.BillingPostalCode) <= 95999) ||
           (integer.valueOf(c.Account.BillingPostalCode) >= 96000 && integer.valueOf(c.Account.BillingPostalCode) <= 96199) ){
              c.Territory_Assignments__c = 'Area 2 - North Cal & SouthWest'; 
        }else if (integer.valueOf(c.Account.BillingPostalCode) >= 91900 && integer.valueOf(c.Account.BillingPostalCode) <= 92099)  {
              c.Territory_Assignments__c = 'Area 3 - Mid-Atlantic & NorthEast'; 

        }else if (integer.valueOf(c.Account.BillingPostalCode) >= 92600 && integer.valueOf(c.Account.BillingPostalCode) <= 92899) {
              c.Territory_Assignments__c = 'Area 4 - Orange Cty & SouthEast'; 

        }else if ((integer.valueOf(c.Account.BillingPostalCode) >= 95200 && integer.valueOf(c.Account.BillingPostalCode) <= 95399) ||
           (integer.valueOf(c.Account.BillingPostalCode) >= 95600 && integer.valueOf(c.Account.BillingPostalCode) <= 95899) ||
           (integer.valueOf(c.Account.BillingPostalCode) >= 93200 && integer.valueOf(c.Account.BillingPostalCode) <= 93399) ||
           (integer.valueOf(c.Account.BillingPostalCode) >= 93500 && integer.valueOf(c.Account.BillingPostalCode) <= 93899) ||
           (integer.valueOf(c.Account.BillingPostalCode) >= 92300 && integer.valueOf(c.Account.BillingPostalCode) <= 92599) ){
              c.Territory_Assignments__c = 'Area 5 - Central Cal & MidWest'; 

        }else if ((integer.valueOf(c.Account.BillingPostalCode) >= 90000 && integer.valueOf(c.Account.BillingPostalCode) <= 91899) ||
           (integer.valueOf(c.Account.BillingPostalCode) >= 92200 && integer.valueOf(c.Account.BillingPostalCode) <= 92299) ||
           (integer.valueOf(c.Account.BillingPostalCode) >= 93000 && integer.valueOf(c.Account.BillingPostalCode) <= 93199) ){
              c.Territory_Assignments__c = 'Area 6 - Los Angeles Cty'; 
        }else if ((integer.valueOf(c.Account.BillingPostalCode) >= 93900 && integer.valueOf(c.Account.BillingPostalCode) <= 94899) ||
           (integer.valueOf(c.Account.BillingPostalCode) >= 95000 && integer.valueOf(c.Account.BillingPostalCode) <= 95199) ||
           (integer.valueOf(c.Account.BillingPostalCode) >= 93400&& integer.valueOf(c.Account.BillingPostalCode) <= 93499) ){
              c.Territory_Assignments__c = 'Area 7 - San Fran Cty & Cal Coast'; 
        }
        }catch(TypeException te){
            c.Territory_Assignments__c = 'Unknown Territory';
        }
       
      }// if postal not blank test

  
  
      if ((String.isNotBlank(c.Account.BillingCountry) && c.Account.BillingCountry <> 'United States of America') ||
        (String.isNotBlank(c.USD_Residence__c) && c.USD_Residence__c == 'International')){
        c.Territory_Assignments__c = 'Area 8 - International';
      }
              
      // if still not set, set unknown
      if(String.isBlank(c.Territory_Assignments__c) ){
         c.Territory_Assignments__c = 'Unknown Territory';
      }
   }// student type new freshman test  
  
 
  }

}

}