+ Start a Discussion
PS81PS81 

Using hidden fields in APEX query

Hi

I need to have a dynamic query to retrieve the fields in Campaign object (keeping aside the governace limit) but when i execute a sample query with all the fields in query editor I see an error "No such column 'hierarchyactualcost' on entity 'Campaign'."

when i set this field as visible for profile and then execute the query i do not see this error. Any ways on how to fix this so that i can perform the query without any errors?

the code to get the fields from the object is:

        Map <String, Schema.SObjectField> objectFields = Campaign.getSobjectType().getDescribe().fields.getMap();
        for (SObjectField fld : objectFields.values()) {
            query += ' ' + fld + ', ';
        }

is there a way to see if the field is set as not visible and if so then do not consider the field in the query?
Best Answer chosen by PS81
Prabhat Kumar12Prabhat Kumar12
@Prabhu... I got the answer after some research. As the documentation says 
The only exceptions to this rule are Apex code that is executed with the executeAnonymouscall and Chatter in Apex.

You are querying from query editor which is part of excuteAnoymouscall, so apex is applying object and field level permission.

I did same from Setup>Apex Class > New and included folllowing code in the editor and i was able to query the field.
 
Public class checkCompaign{

List<Campaign> query = [SELECT hierarchyactualcost FROM Campaign];

Public checkCompaign(){



}


}
So you will be able to get the value in apex class as it runs in system context but you won't be able to display on VF pages.

Hope this will help. :)

All Answers

Sanpreet SainiSanpreet Saini
Go to campaing object ==> fields 
then go to "'hierarchyactualcost"
click on that link and then click the button "Set Field-Level Security"

Check if your profile has access to the field. 

Regards, 
Sanpreet
Prabhat Kumar12Prabhat Kumar12
Hi Prabhu,

You can check if field is accessible by login user you can difine your query based on that.
 
if (Schema.sObjectType.Compaign.fields.hierarchyactualost.isAccessible()) {
   
//Include field  hierarchyactualost in query 

}else{

//Do not include the field hierarchyactualost in query

}

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_perms_enforcing.htm

 
PS81PS81
Thanks Prabat, but since my code is dynamic how can i enforce this rule unlike hardocoding the field name as in ur post?
PS81PS81
the documentation says apex does work on system context and not on users permission set, fls or sharing rules.....but still why it is that i cannot have this query run with this field?

Apex generally runs in system context; that is, the current user's permissions, field-level security, and sharing rules aren’t taken into account during code execution.​ The only exceptions to this rule are Apex code that is executed with the executeAnonymouscall and Chatter in Apex. executeAnonymous always executes using the full permissions of the current user. For more information on executeAnonymous, see Anonymous Blocks.
Prabhat Kumar12Prabhat Kumar12
@Prabhu... I got the answer after some research. As the documentation says 
The only exceptions to this rule are Apex code that is executed with the executeAnonymouscall and Chatter in Apex.

You are querying from query editor which is part of excuteAnoymouscall, so apex is applying object and field level permission.

I did same from Setup>Apex Class > New and included folllowing code in the editor and i was able to query the field.
 
Public class checkCompaign{

List<Campaign> query = [SELECT hierarchyactualcost FROM Campaign];

Public checkCompaign(){



}


}
So you will be able to get the value in apex class as it runs in system context but you won't be able to display on VF pages.

Hope this will help. :)
This was selected as the best answer
PS81PS81
Thanks Prabat...just noticed it and works fine