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
Jaime Soo SeeJaime Soo See 

How can I get isAccessible() on Campaign.Parent field

When I try to perform the following line of code:

Schema.sObjectType.Campaign.fields.Parent.isAccessible();

I get the following error: "parent is not a field of Campaign". However, when I look at the standard object in my org, I can see that there is a standard field "Parent". Is there a way to check the CRUD/FLS on this Parent field? 

Saurabh BSaurabh B

HI Jaime,
Just using 'Parent' will not work. According to Salesforce object reference guide, it should be used as 'ParentId'. Below line of code will work fine,
 
Schema.sObjectType.Campaign.fields.ParentId.isAccessible();

Please mark as Best Answer if it helped you.
 
Jaime Soo SeeJaime Soo See
Hi Saurabh, 

Thanks for your quick response. I understand that I can check the ParentId field for accessibility, but does that mean it includes proper checking for the Parent field? (Tough way to word the question I know)

Here's an example:
[select c.Id, c.Parent.ParentId, c.ParentId from Campaign c]
Does your example of checking the ParentId field for isAccessible() cover what I'm doing in the above SOQL query? i.e. I don't need to actually check for accessibility on the Parent field and the ParentID field?

Thanks!
Saurabh BSaurabh B
Jaime,
I`m not quite sure I understand your question. Are you doing this to check if user can access the Object fields? Also, keep in mind  ' isAccessible() ' is a boolean response so it will either return True or False based upon user`s accessiblity of that object. For example, if user cannot access that object or field, it will return as False. See example below,

To check the field-level read permission of the contact's email field before querying for this field:
if (Schema.sObjectType.Contact.fields.Email.isAccessible()) {
 Contact c = [SELECT Email FROM Contact WHERE Id= :Id];
}

Thanks!
Jaime Soo SeeJaime Soo See
Saurabh,

Yes, you're correct. Before making a query on a field of an object, Salesforce requires an isAccessible() check on those fields first to ensure that you have the proper access.

So, before making a SOQL query on a set of fields, we must first check the accessibility of those fields. So, I have a SOQL query that does this:
[SELECT c.Parent.ParentId, c.ParentId FROM Campaign];
I would need to check accessibility as you mentioned:
if (Schema.sObjectType.Campaign.fields.Parent.isAccessible() && Schema.sObjectType.Campaign.fields.ParentId.isAccessible()) {
 Campaign campaignObject = [SELECT c.Parent.ParentId, c.ParentId FROM Campaign WHERE Id= :Id];
}
However, the check on the Parent is what fails with the above error.

Any help on how to check the acceissbility of just the Parent field? Or is checking the ParentId the same as checking the Parent field too? I hope that makes more sense.

 
Jaime Soo SeeJaime Soo See
Saurabh, 

Just wanted to follow up in case you had more input, it would be greatly appreciated.

Thanks!