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
Rajnish Bishnoi 13Rajnish Bishnoi 13 

Not getting record in query in Test class with a third reference

Hi ,

I am not able to get record in query in test class if i am providing the 3rd reference filter in query.
But if i am not providing the filter in query then i am able to get the record and i can access thet 3rd reference data also.
Not able to understand what is the issue with query filter.... please help.
My query is on User and i am providing filter on Custom object which is related to user through Contact Object.

Thanks,
Rajnish
pconpcon
Can you please provide the code that you are running as well as what line you are seeing the error on and what you are expecting to see?

NOTE: When adding code please use the "Add a code sample" button (icon <>) to increase readability and make it easier to reference.
Rajnish Bishnoi 13Rajnish Bishnoi 13
I am trying to access below query:-
List<User> usr=new List<User>([Select Id, Employee_Type__c, Employment_Status__c,email,Contact.COS_LOS_Users__c, Contact.COS_Hostid_Users__c, Contact.Reference_Key__r.Name,Contact.Reference_Key__r.End_Date__c  from User where ((Employee_Type__c!='Employee' OR Employment_Status__c!='Available') and Contact.COS_LOS_Users__c=true AND Contact.Reference_Key__c!=null AND Contact.Reference_Key__r.Name!=null)]);

The bold condition in the query creating the problem. If i am using this Contact.Reference_Key__r.Name!=null filter in query then i am getting 0 record. But without this condition i am getting the record, and with that record i am able to access usr[0].Contact.Reference_Key__r.Name.

So i am unable to understand that why the filter in query not providing the record.
pconpcon
Just to reiterate your issue, with the query below:
 
List<User> usr = new List<User>([
    select Employee_Type__c,
        Employment_Status__c,
        Email,
        Contact.COS_LOS_Users__c,
        Contact.COS_Hostid_Users__c,
        Contact.Reference_Key__r.Name,
        Contact.Reference_Key__r.End_Date__c
    from User
    where (
        (
            Employee_Type__c != 'Employee' or
            Employment_Status__c != 'Available'
        ) and
        Contact.COS_LOS_Users__c = true and
        Contact.Reference_Key__c != null and
        Contact.Reference_Key__r.Name != null
    )
]);

If you include line number 17 you get no results returned.  But if you remove line 17 you get a result back that has the Contact.Reference_Key__r.Name populated?  Are you creating the User record during your test?
 
Rajnish Bishnoi 13Rajnish Bishnoi 13
Yes, you are right.
i am getting the result after removing the line 17 and getting the reference key name as well.
Yes i am creating the User record during test.
pconpcon
Can you please include the test code that you are using to create the user as well as how you are calling the code that instatiates this query?  What API version is your test and all your related classes / triggers?
Rajnish Bishnoi 13Rajnish Bishnoi 13
I can not share everything here.
pconpcon
Can you write a simple reproducer class and test that shows the issue?