+ Start a Discussion
dizondizon 

SOQL not retrieving all fields (depending on profile)

One of my classes are acting quite strangely and not retrieving all fields depending on the profile that is currently running the apex. However, AFAIK - profiles should not affect the retrieval of fields as apex is executed in system mode. On further inspection and use of debug logs, it seems that my system administrator profile can return all fields (as expected), however when changing to the Human Resources profile, it can retrieve 1 lookup field, however the rest can not be seen.

After checking field level security, all the object's fields are visible to the profile.

If anyone can shed some light on this issue, that would be great.

 

Here are snippets from the debug log:

11:03:19.206|SOQL_EXECUTE_BEGIN|[155,49]|Aggregations:2| SELECT ID,
Name,
Approval_Status__c,
Employee__c,
Employee__r.Name,
Managers_Manager__c,
Manager_On_Leave__c,
Employee__r.Leave_Manager__c,
Employee__r.Leave_Manager__r.User__c,
Employee__r.Timesheet_Manager__r.User__c,
Employee__r.Employee_ID__c,
Employee__r.Timesheet_Manager__c,
Manager__c,
End_Date__c,
Start_Date__c,
Total_Hours__c,
Payslip__c,
BASEURL__c,
Reference__c,
(SELECT ID,
Name,
Comments__c,
Timesheet__c,
Project__c,
Task__r.Name,
Project__r.Name,
Task__c
FROM Timesheet_Items__r
ORDER BY Name ASC),
(SELECT ID,
Date__c,
Hours__c,
Timesheet_Item__c,
Timesheet__c
FROM Timesheet_Entries__r)
FROM Timesheet__c
WHERE Approval_Status__c =: classGlobalVariables.LBL_APPROVED
AND Managers_Manager__c =: UserInfo.getUserId()
ORDER BY Name
11:03:19.211|METHOD_ENTRY|[192,42]|UserInfo.getUserId()

 

11:03:19.223|USER_DEBUG|[602,5]|DEBUG|########TIMESHEET######## SHRM__Timesheet__c:{SHRM__Manager__c=00580000003I3NgAAK, Name=TS-0542, SHRM__Approval_Status__c=Approved, SHRM__Start_Date__c=2009-12-18 00:00:00, SHRM__Manager_on_Leave__c=false, SHRM__baseURL__c=https://shrm.na6.visual.force.com, SHRM__Total_Hours__c=14.000, Id=a0B80000005cxiREAQ, SHRM__End_Date__c=2009-12-18 00:00:00}

 Manager__c as you can see is a lookup field, however the other lookupfields (e.g. my Employee__c  field) is not returned, though it is queried in my soql. 

 

 

However, same method, but sys admin profile:

 

11:06:37.831|USER_DEBUG|[602,5]|DEBUG|########TIMESHEET######## SHRM__Timesheet__c:{SHRM__Approval_Status__c=Approved, SHRM__Start_Date__c=2010-02-11 00:00:00, SHRM__Total_Hours__c=6.000, SHRM__End_Date__c=2010-02-17 00:00:00, Name=TS-0936, SHRM__Manager__c=00580000003HmenAAC, SHRM__Employee__c=a0380000007Rd9AAAS, SHRM__Manager_on_Leave__c=false, SHRM__baseURL__c=https://shrm.na6.visual.force.com, Id=a0B80000006c3lNEAQ}

 

... seems to retrieve all fields.

 

Regards,

David Dizon

Best Answer chosen by Admin (Salesforce Developers) 
Nick34536345Nick34536345

A lookup field will get set to null if the parent record is deleted. Try a before delete trigger.

All Answers

ColinKenworthy2ColinKenworthy2
Are you using any "with sharing" options in your code? [please ignore - this isn't a cause of what you are seeing]
Message Edited by ColinKenworthy2 on 02-17-2010 02:17 AM
Nick34536345Nick34536345

Hi,

 

The examples you give are not the same records. Are the lookup fields you are missing not just empty/null on that record?

Jeremy.NottinghJeremy.Nottingh

I have experienced this exact same situation with a SOQL query running within a Trigger on custom object Opportunity_Bundle__c. Custom lookup field  Opportunity_Bundle__c on OpportunityLineItem will not be returned, although it does have a value. Here is my query:

list<Opportunity_Bundle__c> obs = Trigger.old;

set<id> oppids = new set<id>();

for (Opportunity_Bundle__c ob : obs) oppids.add(ob.Opportunity__c);

list<Opportunity> opps = [select id, (select OpportunityID, Opportunity_Bundle__c, PricebookentryID, Entry_Type__c, Quantity, UnitPrice, Corrected_Entry_Date__c from OpportunityLineItems )

from Opportunity where id in :oppids ];

 

This trigger should run After Delete, and find all OpportunityLineItems with Opportunity_Bundle__c matching ids in Trigger.old. However, the field Opportunity_Bundle__c is not returned. I can look at this field in Excel Connector, and it's on the Page Layout for that object. The Apex trigger just acts as though it's null.

 

I am running this from System Administrator, and there are no limitations on Field Level Security for this field. I even created a new lookup field to the same object, and have had the same trouble with it. Please help!

 

Jeremy

Nick34536345Nick34536345

A lookup field will get set to null if the parent record is deleted. Try a before delete trigger.

This was selected as the best answer
Jeremy.NottinghJeremy.Nottingh

That was the solution to my problem: The field wasn't getting returned on a Delete Trigger because it had already be cleared out. I changed the Trigger to Before Delete from After Delete, and it worked perfectly! Thanks for your help.

 

Jeremy