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
Tanner RussellTanner Russell 

Why is this field showing null even after adding a reference to the id

Account la_test = new Account(Name='Test');
        insert la_test;
	Contact lcon_test = new Contact(LastName='Test', Accountid=la_Test.id, Email='test@test.com');
        insert lcon_test;
        location__C lloc_test = new Location__C(name = locName, site_wf__c = site);
        insert lloc_test;
        RecordType rec = [SELECT Id,Name FROM RecordType WHERE SobjectType='Case' and name = :record];
        Case lc_test = new Case(Subject='This is a test ', Accountid=la_Test.id, Status='New', RecordTypeId=rec.id, Contactid=lcon_test.id, Location__c= lloc_test.id,  Notify_contact__c = true); 
        //for trigger testing

        insert lc_test;
If I do a lookup query on the location id being passed into case its name value is populated but when I do lc_test.location__r.name it shows as null (location__c being a lookup field) does anyone know what im doing wrong?
Best Answer chosen by Tanner Russell
Amit Chaudhary 8Amit Chaudhary 8

        // You need to query related field to get value like below

        Case caseObj = [select id, location__r.name ,location__r.site_wf__c  from Case where id =:lc_test.id ];    
        System.debug(caseObj.location__r.name);
        System.debug(caseObj.location__r.site_wf__c);



Please try below test class
@isTest
public class EmailOnWorkOrderClosed_Test {
    public static void Setup(String site, string record, string locName){
         
        Account la_test = new Account(Name='Test');
        insert la_test;
		Contact lcon_test = new Contact(LastName='Test', Accountid=la_Test.id, Email='test@test.com');
        insert lcon_test;
        location__C lloc_test = new Location__C(name = locName, site_wf__c = site);
        insert lloc_test;
        RecordType rec = [SELECT Id,Name FROM RecordType WHERE SobjectType='Case' and name = :record];
        Case lc_test = new Case(Subject='This is a test ', Accountid=la_Test.id, Status='New', RecordTypeId=rec.id, Contactid=lcon_test.id, Location__c= lloc_test.id,  Notify_contact__c = true); 
        insert lc_test;

		// You need to query related field to get value like below

		Case caseObj = [select id, location__r.name ,location__r.site_wf__c  from Case where id =:lc_test.id ];	
		System.debug(caseObj.location__r.name);
		System.debug(caseObj.location__r.site_wf__c);

		lc_test.Status = 'Closed';
        update lc_test;
       
        
    }
    
    //test the email on insert/update
    @isTest
    public static void EmailOnInsertUpdate_Test(){
        EmailOnWorkOrderClosed_Test.Setup('London', 'RR Case', 'test');
        EmailOnWorkOrderClosed_Test.Setup('London', 'RR Case', 'West 5');
        EmailOnWorkOrderClosed_Test.Setup('Guelph', 'RR Case', 'test');
        EmailOnWorkOrderClosed_Test.Setup('Brantford', 'RR Case', 'test');
        EmailOnWorkOrderClosed_Test.Setup('London', 'CP Case', 'test');
    }
 
    
    
}

 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Can you please post your full code and let us know in which line you are getting null value ?
Tanner RussellTanner Russell
site, locname and record are all passed in strings
Tanner RussellTanner Russell
@isTest
public class EmailOnWorkOrderClosed_Test {
    public static void Setup(String site, string record, string locName){
         
        Account la_test = new Account(Name='Test');
        insert la_test;
		Contact lcon_test = new Contact(LastName='Test', Accountid=la_Test.id, Email='test@test.com');
        insert lcon_test;
        location__C lloc_test = new Location__C(name = locName, site_wf__c = site);
        insert lloc_test;
        RecordType rec = [SELECT Id,Name FROM RecordType WHERE SobjectType='Case' and name = :record];
        Case lc_test = new Case(Subject='This is a test ', Accountid=la_Test.id, Status='New', RecordTypeId=rec.id, Contactid=lcon_test.id, Location__c= lloc_test.id,  Notify_contact__c = true); 
        //for trigger testing

        insert lc_test;
        lc_test.Status = 'Closed';
        update lc_test;
       
        
    }
    
    //test the email on insert/update
    @isTest
    public static void EmailOnInsertUpdate_Test(){
        EmailOnWorkOrderClosed_Test.Setup('London', 'RR Case', 'test');
        EmailOnWorkOrderClosed_Test.Setup('London', 'RR Case', 'West 5');
        EmailOnWorkOrderClosed_Test.Setup('Guelph', 'RR Case', 'test');
        EmailOnWorkOrderClosed_Test.Setup('Brantford', 'RR Case', 'test');
        EmailOnWorkOrderClosed_Test.Setup('London', 'CP Case', 'test');
    }
 
    
    
}

 
Tanner RussellTanner Russell
Null value when trying to get the lc_test.location__r.name or lc_test.location__r.site_wf__c this doesnt really make sense to me since it normally works
Tanner RussellTanner Russell
No line in paticular I had some system.debugs on line 14 for both fields since I need them for the class
Amit Chaudhary 8Amit Chaudhary 8
Where you are reffereing lc_test.location__r.name  field ?
Tanner RussellTanner Russell
I am referencing the location__r.name and the location__r.site_wf__C in the actual class this test class calls the trigger that calls the class that loops through the case objects in the trigger and says if(case.location__r.name ='West 5'){do something...} but the issue is in this test class beacuse I am not able to get the value from the object. 
Tanner RussellTanner Russell
@isTest
public class EmailOnWorkOrderClosed_Test {
    public static void Setup(String site, string record, string locName){
         
        Account la_test = new Account(Name='Test');
        insert la_test;
		Contact lcon_test = new Contact(LastName='Test', Accountid=la_Test.id, Email='test@test.com');
        insert lcon_test;
        location__C lloc_test = new Location__C(name = locName, site_wf__c = site);
        insert lloc_test;
        RecordType rec = [SELECT Id,Name FROM RecordType WHERE SobjectType='Case' and name = :record];
        Case lc_test = new Case(Subject='This is a test ', Accountid=la_Test.id, Status='New', RecordTypeId=rec.id, Contactid=lcon_test.id, Location__c= lloc_test.id,  Notify_contact__c = true); 
        //for trigger testing
//this
System.debug(lc_test.location__r.name);
System.debug(lc_test.location__r.site_wf__c);
        insert lc_test;
        lc_test.Status = 'Closed';
        update lc_test;
       
        
    }
    
    //test the email on insert/update
    @isTest
    public static void EmailOnInsertUpdate_Test(){
        EmailOnWorkOrderClosed_Test.Setup('London', 'RR Case', 'test');
        EmailOnWorkOrderClosed_Test.Setup('London', 'RR Case', 'West 5');
        EmailOnWorkOrderClosed_Test.Setup('Guelph', 'RR Case', 'test');
        EmailOnWorkOrderClosed_Test.Setup('Brantford', 'RR Case', 'test');
        EmailOnWorkOrderClosed_Test.Setup('London', 'CP Case', 'test');
    }
 
    
    
}

I added the debug lines to clear things up
Amit Chaudhary 8Amit Chaudhary 8

        // You need to query related field to get value like below

        Case caseObj = [select id, location__r.name ,location__r.site_wf__c  from Case where id =:lc_test.id ];    
        System.debug(caseObj.location__r.name);
        System.debug(caseObj.location__r.site_wf__c);



Please try below test class
@isTest
public class EmailOnWorkOrderClosed_Test {
    public static void Setup(String site, string record, string locName){
         
        Account la_test = new Account(Name='Test');
        insert la_test;
		Contact lcon_test = new Contact(LastName='Test', Accountid=la_Test.id, Email='test@test.com');
        insert lcon_test;
        location__C lloc_test = new Location__C(name = locName, site_wf__c = site);
        insert lloc_test;
        RecordType rec = [SELECT Id,Name FROM RecordType WHERE SobjectType='Case' and name = :record];
        Case lc_test = new Case(Subject='This is a test ', Accountid=la_Test.id, Status='New', RecordTypeId=rec.id, Contactid=lcon_test.id, Location__c= lloc_test.id,  Notify_contact__c = true); 
        insert lc_test;

		// You need to query related field to get value like below

		Case caseObj = [select id, location__r.name ,location__r.site_wf__c  from Case where id =:lc_test.id ];	
		System.debug(caseObj.location__r.name);
		System.debug(caseObj.location__r.site_wf__c);

		lc_test.Status = 'Closed';
        update lc_test;
       
        
    }
    
    //test the email on insert/update
    @isTest
    public static void EmailOnInsertUpdate_Test(){
        EmailOnWorkOrderClosed_Test.Setup('London', 'RR Case', 'test');
        EmailOnWorkOrderClosed_Test.Setup('London', 'RR Case', 'West 5');
        EmailOnWorkOrderClosed_Test.Setup('Guelph', 'RR Case', 'test');
        EmailOnWorkOrderClosed_Test.Setup('Brantford', 'RR Case', 'test');
        EmailOnWorkOrderClosed_Test.Setup('London', 'CP Case', 'test');
    }
 
    
    
}

 
This was selected as the best answer
Tanner RussellTanner Russell
Thank you for the help! your example of doing a new query helped me solve my issue I relized that I was not doing a re query in the class which was presenting null values