9

This looks like a Winter 16 issue, but would appreciate any other thoughts. Queries of tooling api objects (such a FieldDefinition) return no results when run in a trigger or a VF page, but they do return results when run from Anonymous Apex. The same queries work in Summer 15 orgs.

Here's a simple set of repro steps:

  1. Prove this works in Summer 15. In a Summer 15 dev org (or sandbox), run the following anonymous apex via developer console:

    list<FieldDefinition> defs = [select QualifiedApiName from FieldDefinition where EntityDefinition.QualifiedApiName = 'Account'];
    system.debug('defs: ' + defs);
    

    Inspect the log; confirm that the debug output includes a list of Account Fields.

    Assertion: Tooling API Query works in Anonymous Apex context in Summer 15.

  2. While still in your Summer 15 dev org (or sandbox): Create a trigger on Account:

    trigger ToolingTest on Account (before update) {
        list<FieldDefinition> defs = [select QualifiedApiName from FieldDefinition where EntityDefinition.QualifiedApiName = 'Account'];
         system.debug('defs: ' + defs);
    }
    

    Make sure Developer Console is still open in, and edit and save any account. Review the log; confirm that the debug output includes a list of Account Fields.

    Assertion: Tooling API Query works in Apex in trigger context in Summer 15.

  3. In a Winter 16 org (preview org or Sandbox), repeat step 1. Confirm that the debug output includes a list of Account Fields.

    Assertion: Tooling API Query works in Anonymous Apex context in Winter 16

  4. In a Winter 16 org (preview org or Sandbox), repeat step 2 Confirm that the debug output includes an EMPTY list.

    Assertion: Tooling API Query does not work in Apex in trigger context in Winter 16. This has also been tested in a Visualforce page with the same results.

  • Maybe it's a newly introduced permissions issue? – Adrian Larson Oct 7 '15 at 19:43
  • @AdrianLarson I wondered about perms as well. I haven't found such a new perm yet (checking the UI and the release notes); also, my testing from Anonymous Apex was running as the same user as my trigger tests and my VF tests. – Jason Clark Oct 7 '15 at 19:54
  • I've repeated the test and got the same result. Are you able to raise this as a bug to support? – Daniel Ballinger Oct 8 '15 at 8:12
  • Unfortunately no, I do not have access to a support plan for this project. I have submitted via partner portal (closed) and in the Developer forum (partner support suggestion). Hopefully that will get picked up. – Jason Clark Oct 8 '15 at 14:26
  • @JasonClark Is this still a problem? I'll push it to Premier support if you want. – Daniel Ballinger Oct 14 '15 at 0:15
2

This has all the signs of being a bug in Winter 16. I've raised this as support case 12634999.

In terms of short term workarounds, you could try moving the SOQL query into a future or queueable apex. Might help to get out of the trigger context.


Updates from support:

10/15/2015 08:40:52 AM PDT

Please note that [I] will escalate the case to Tier 3 support and keep you updated with the progress.

10/19/2015 11:22:44 AM PDT and
10/20/2015 07:54:02 AM PDT and
10/21/2015 06:51:20 AM PDT

Please note that Tier 3 support is looking into the case and i will keep you updated with the progress again tomorrow morning.

10/22/2015 05:19:48 PM PDT

Please note that RnD has acknowledged this issue as bug and are working on it (W-2795216)

10/27/2015 01:24:54 PM PDT

Please find below the link for Known Issue.
https://success.salesforce.com/issues_view?id=a1p30000000eNxN

05/10/2016

We are pleased to notify you that the following Known Issue has been resolved: Query against FieldDefinition in Apex Trigger/Apex Class context returns no rows.
Thank you for being patient while we worked to address this issue. Please feel free to reach out to Customer Support if you have further questions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.