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
Jose ZunigaJose Zuniga 

Using Id in select clauses

I have the next code:

--------------------------------------------------------
 for (User U: newUsersData)
      {

            .............

      
              List <employee__c> LEmp =  [select   ID, 
                                  Email__c,
                                First_Name__c,
                                Last_Name__c,
                                User_Record__c,
                                Title__c,
                                MSC__c,
                                Global_Department__c
                             
                                          from employee__c 
                                          where User_Record__c = :U.Id                                                        
                                   ];
                   ....

--------------------------------------------------

User_Record__c is a lookup over User. The object  employee__c   has User_Record__c  as a field. Whenever I run the previous Select query LEmp becomes empty. But, if I write at the query console:

select   ID, Email__c, First_Name__c,
         Last_Name__c,  User_Record__c,
         Title__c,  MSC__c, Global_Department__c
 from employee__c 
 where User_Record__c = :'SOMEUSERID'

where SOMEUSERID  represents the ID of the user I am looking for then I get the row I need. 
Is there something wrong with my syntax?
Thanks
snehal surti 3snehal surti 3
Have you checked if your variable newuserdata has the  user data and it enters into loop by putting debugs? Your code looks ok but its possible that there is no matching user id or code never enters into for loop. Also i notice that you got SOQL within for loop which is not right practice. 
Jose ZunigaJose Zuniga
All that was checked. However, I must mention that this code is being executed in test mode. So, the employee__c table might not be visible. We tried executing a query like  [select Id from employee__c] instead and an empty list for LEmp was returned. Could this be also permissions? If this is the case, which ones could be?