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
Sharon CrenshawSharon Crenshaw 

Apex Test Class Variable Setting

I am getting a null, for the variable, setUsrCat after a call to the Apex Class. SetUsrCat should not be Sminull90. I was hoping to get SmOR90. My Apex class does it correctly, but  when I run the test class I get the null problem.

I have tried a number of things in the test class, but the null remains. I am assuming there is something in the test class that needs to be set that is not. If you can please help me in this matter it will be appreciated.
 
My code is as follows:
 
Apex Class:

public  class NewUtilStaffProcess {
@future
public static void createUtlUser(Id recid) {
      
     Contact gcon = [SELECT Email, Lastname, Firstname, Middlename, AccountId
                      FROM   Contact
                      WHERE  Id = :recid];
     
     Profile p = [SELECT Id FROM Profile WHERE Name='HAVNA City Public Utility Group'];
     
     Id acctsTypeId = [SELECT Id
                      FROM   RecordType
                      WHERE  SObjectType = 'Account'
                      AND Name = 'Account Source'].Id;
     
     String groleId = [SELECT Id FROM UserRole WHERE Name = 'Utility Staff' LIMIT 1].Id;
     
     
     String formatUsrName;
     String setUsrLoc;
     
     Account retAcct = [SELECT Id, HAVNA_State_Country_State__r.HAVNA_Letter_Code__c, Hav_Locality__c, Hav_Street__c    
                         FROM   Account
                         WHERE  Id = :gcon.AccountId
                         AND recordTypeId = :acctsTypeId
                         LIMIT 1];
     
     if (retAcct.Hav_Street__c == 'Local/Surveyor')
        setUsrLoc = retAcct.HAVNA_State_and_Country_ID__r.HAVNA_Letter_Code__c+retAcct.Hav_Locality__c;
 
     if (retAcct.Hav_Street__c == 'City/Surveyor')
        setUsrLoc = retAcct.HAVNA_State_and_Country_ID__r.HAVNA_Letter_Code__c+'C1';
   
     if (retAcct.Hav_Street__c == 'State/Surveyor')
        setUsrLoc = retAcct.HAVNA_State_and_Country_ID__r.HAVNA_Letter_Code__c+'C2';
 
       
     if (gcon.middlename == null) {
         formatUsrName = gcon.Lastname.deleteWhitespace().toLowercase().trim()+'.'+gcon.Firstname.deleteWhitespace().toLowercase().trim()+'.cnastaff'+setUsrLoc+'@HAVNA.gmail';
     } else {
         Integer midSize = gcon.Middlename.length();
         if (midSize == 1) {
             formatUsrName = gcon.Lastname.deleteWhitespace().toLowercase().trim()+'.'+gcon.Firstname.deleteWhitespace().toLowercase().trim()+'.'+gcon.middlename.deleteWhitespace().toLowercase().trim()+'.cnastaff'+setUsrLoc+'@HAVNA.gmail';
         } else {
             formatUsrName = gcon.Lastname.deleteWhitespace().toLowercase().trim()+'.'+gcon.Firstname.deleteWhitespace().toLowercase().trim()+'.'+gcon.middlename.deleteWhitespace().trim().substring(0,1).toLowercase()+'.cnastaff'+setUsrLoc+'@HAVNA.gmail';
         }    
     }
     System.debug('Formatted Username: '+formatUsrName);
   
     User u   = new User(Alias = gcon.Lastname.deleteWhitespace().trim().length() > 4 ? gcon.Lastname.deleteWhitespace().trim().substring(0,2)+setUsrLoc: gcon.lastname+setUsrLoc,
                   Email          = gcon.Email,
                   EmailEncodingKey   = 'UTF-8',
                   LastName       = gcon.Lastname,
                   FirstName      = gcon.Firstname,
                   MiddleName     = gcon.Middlename,     
                   LanguageLocaleKey  = 'en_US',
                   LocaleSidKey   = 'en_US',
                   ProfileId      = p.Id,
                   UserRoleId     = groleid,      
                   TimeZoneSidKey = 'America/New_York',
                   UserName       = formatUsrName);
    
     Insert(u);
     System.debug(u.id);
     
     UtilStaffPermSet.assgnPermissionSet(recId, u.Id);  
     System.resetPassword(u.Id, True);
   }
}

=====================================================
 
Apex Test Class:
 
@isTest
public class NewUtilStaffProcessTest {
   public static testMethod void InitialTest() {
  /* Get Record Id Type */   
  Id conTypeId;
  Id acctTypeId;
  String formatUsrName;
  String setUsrLoc;
 
  conTypeId = [SELECT Id  
               FROM   RecordType
               WHERE  SObjectType = 'Contact'
               AND Name = 'Utility Staffer'].Id;  
    
  Id acctsTypeId = [SELECT Id
                FROM   RecordType
                WHERE  SObjectType = 'Account'
                    AND Name = 'Account Source'].Id;
  Profile p = [SELECT Id FROM Profile WHERE Name='HAVNA City Public Utility Group'];
  String groleId = [SELECT Id FROM UserRole WHERE Name = 'Utility Staff' LIMIT 1].Id;
  Integer midsize;
    
  //Create new State record; initialize required field(s), then insert
  HAV_State_and_Country__c state  = new HAV_State_and_Country__c();
  state.Name            = 'Oregon';
  state.HAVNA_Letter_Code__c = 'OR';
  state.Hav_Type__c    = 'State';
  Insert state;
 
  //Create new Account record; initialize required field(s), then insert
  Account acct = new Account(Name='Oregon Local 7');
  acct.Hav_Locality__c = '90';
  acct.Hav_Street__c = 'Local/Surveyor';
  acct.RecordTypeId =  AcctTypeId;
  acct.HAVNA_State_and_Country_ID__c = state.id;

insert acct;
  //Create new Contact record; initialize required field(s), then insert
  Contact con  = new Contact();   
  con.FirstName   = 'Ralph';  
  con.LastName    = 'Smith';
  con.Middlename  = 'Otis';
  con.RecordTypeId =  conTypeId;
  con.email       = 'rsmith8@verizon.com';
  con.AccountId      = acct.id;
    
  //Insert Contact
  Insert con ;
    
    System.debug(con.id);
 User thisUser = [SELECT Id FROM User WHERE Id = :UserInfo.getUserId()];
 system.runAs(thisUser){
  Test.startTest();
    
  NewUtilStaffProcess.createUtlUser(con.id);
 
  Test.stopTest();
  system.debug('Done');
}
  }
}
AnudeepAnudeep (Salesforce Developers) 
Hi Sharon, 

Null pointer exceptions are usually thrown by a line of code that is trying to use an object that has not been instantiated, or an object's attribute that has not been initialized.

I see you have a bunch of lists in the code. Please check for the presence of values before performing the query. You can use the isEmpty() attribute for both list and map

See the documentation to learn more

Let me know if this helps, if it does, please close the query by marking it as solved. It may help others in the community. Thank You!

Anudeep
Sharon CrenshawSharon Crenshaw
Thanks, but I am not getting a null exception message. My test runs are successful,  but the variable, setUsrLoc,  is always null. There should be a value there. From Sinull90 to SiCO90.   I seem to be missing something in my tesr class.